How to use the Strategy Pattern with C#?

前端 未结 7 728
暗喜
暗喜 2021-01-01 18:29

Here\'s what I have so far:

namespace Strategy
{
    interface IWeaponBehavior
    {
        void UseWeapon();
    }
}

namespace Strategy
{
    class Knife          


        
相关标签:
7条回答
  • 2021-01-01 18:48
      class Character
      {
        private IWeaponBehavior _weapon;
    
        // Constructor
        public Character(IWeaponBehavior strategy)
        {
          this._weapon = strategy;
        }
    
        public void WeaponInterface()
        {
          _weapon.UseWeapon();
        }
      }
    
    0 讨论(0)
  • 2021-01-01 18:49

    Take a look at dependency injection & then also look into Inversion of Control.

    TekPub has a great free episode on these two ideas using a sample very similar to yours. This should help you with a concrete sample. :-)

    http://tekpub.com/view/concepts/1

    Sample code from video:

    class Samurai {
      IWeapon _weapon;
    
      public Samurai() {
       _weapon = new Sword();
      }
    
      public Samurai(IWeapon weapon) {
       _weapon = weapon;
      }
    
      public void Attack(string target) {
        _weapon.Hit(target);
      }
    }
    

    The above is DI (with IWeapon constructor). Now adding Inversion of Control you gain even more control.

    Using an IOC container you will be able to control the IWeapon via injection instead of in the class itself.

    The video uses NInject as it's IOC and shows how you can eliminate the IWeapon Constructor and gain control over the class and all classes needing to use IWeapon to use what you want in one location/configuration area.

    Sample from video:

    class WarriorModule: NinjectModule {
      public override void Load() {
       Bind<IWarrior>().To<Samurai>();
       Bind<IWeapon>().To<Sword>();
    }
    

    The above configuration tells NInject that whenever it encounters an IWarrior to use Samurai and whenever it encourters an IWeapon to use Sword.

    The video goes on to explain how this little change increases your flexibility. It adds another Module for a Samuria who uses a ranged weapon, etc....

    You can change these as needed/wanted.

    0 讨论(0)
  • 2021-01-01 18:53

    There's a few possibilities. What you're really asking is, "how should a specific character know which weapon to use?".

    You could either have a Character factory that would create and inject the right type of weapon into the character (That sounded wrong :) ), or let the constructor of a specific character take care of creating the weapon.

    0 讨论(0)
  • 2021-01-01 18:55

    Here's a very focused article on using this pattern in C#: http://www.lowendahl.net/showShout.aspx?id=115

    0 讨论(0)
  • 2021-01-01 19:01
    public class Character
    {
        IWeaponBehavior Weapon;
    }
    
    public class Princess : Character
    {
        Weapon = new Pan();
    }
    
    public class Thief : Character
    {
        Weapon = new Knife();
    }
    
    0 讨论(0)
  • 2021-01-01 19:14

    Use dependency injection in class Character?

    public class Character
    {
        public Character(IWeaponBehavior weapon) 
        {
            this.weapon = weapon;
        }
    
        public void Attack()
        {
            weapon.UseWeapon();
        }
    
        IWeaponBehavior weapon;
    }
    
    public class Princess: Character
    {
        public Princess() : base(new Pan()) { }
    }
    
    public class Thief: Character
    {
        public Thief() : base(new Knife()) { }
    }
    
    ...
    
    Princess p = new Princess();
    Thief t = new Thief();
    
    p.Attack(); // pan
    t.Attack(); // knife
    

    Edited as requested.

    0 讨论(0)
提交回复
热议问题