How to use the Strategy Pattern with C#?

梦想的初衷 提交于 2019-11-30 08:24:54

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.

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.

  class Character
  {
    private IWeaponBehavior _weapon;

    // Constructor
    public Character(IWeaponBehavior strategy)
    {
      this._weapon = strategy;
    }

    public void WeaponInterface()
    {
      _weapon.UseWeapon();
    }
  }
public class Character
{
    IWeaponBehavior Weapon;
}

public class Princess : Character
{
    Weapon = new Pan();
}

public class Thief : Character
{
    Weapon = new Knife();
}

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.

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

If your characters all have their own specific weapon, you don't actually need to use the strategy pattern. You could use plain polymorphism. But the strategy pattern could be cleaner anyway.

You just need to intitialize the weapon:

public class Character
{
     IWeapenBehavior weapon;

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


public class Thief : Character
{
      Thief()
      {
           weapon = new Knife();
      }
}

In the code that controls your Characters, you can call the Attack() method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!