1 Replies - 907 Views - Last Post: 10 August 2010 - 07:42 AM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

How to use the Strategy Pattern with C#?

Posted 10 August 2010 - 07:19 AM

Here's what I have so far:

namespace Strategy
    {
        interface IWeaponbehavior
        {
            void UseWeapon();
        }
    }
    
    namespace Strategy
    {
        class Knife : IWeaponbehavior
        {
            public void UseWeapon()
            {
                Console.WriteLine("You used the knife to slash the enemy! SLASH SLASH!");
            }
        }
    }
    
    namespace Strategy
    {
        class Pan : IWeaponbehavior
        {
            public void UseWeapon()
            {
                Console.WriteLine("You use the pan! 100% Adamantium power! BONG!");
            }
        }
    }


Now if I have a Character.cs superclass. how can that superclass implement a weaponbehavior so that children classes can be more specific.

namespace Strategy
    {
        class Character
        {
            public IWeaponbehavior weapon;
    
            public Character(IWeaponbehavior specificWeapon)
            {
                weapon = specificWeapon;
            }        
        }
    }
    
    namespace Strategy
    {
        class Thief : Character
        {
            
        }
    }


How can I implement this? I'm very confused on what the actual code needs to be.

I know this might be asking too much, but if you could write the actual code so I could study it, that would be very nice of you guys. I learn by seeing code. :P Many people could benefit from this question.

Is This A Good Question/Topic? 0
  • +

Replies To: How to use the Strategy Pattern with C#?

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to use the Strategy Pattern with C#?

Posted 10 August 2010 - 07:42 AM

Something like this:
namespace Strategy {
    class Character {
        private IWeaponbehavior weapon;
        public Character(Iweaponbehavior specificWeapon) {
            weapon = specificWeapon;
        }
    }

    class Thief : Character {
        public Thief(IWeaponbehavior specificWeapon) : base(specificWeapon) {
        }

        public UseWeapon() {
            weapon.UseWeapon();
        }
    }
}


You'd also (most likely) want to include a 'ChangeWeapon' method in the base character class to allow the assigning of different weapons as your game progresses (if that is something that can happen :))

Edit: The method UseWeapon in the Thief class just has the same name as the IWeaponbehavior because it seems like a good name, it could be anything (and probably should be something else just to make it clearer that it isn't an overload/override of the other method).

This post has been edited by Momerath: 10 August 2010 - 07:44 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1