Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Strategy Pattern with C#?

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.

like image 885
Sergio Tapia Avatar asked Aug 10 '10 13:08

Sergio Tapia


People also ask

Does C have design patterns?

Yes, there are. Lazy initialization, singleton, object pool, object state etc. are easily implemented in pure C.

What is C pattern design?

Design Patterns in the object-oriented world is a reusable solution to common software design problems that occur repeatedly in real-world application development. It is a template or description of how to solve problems that can be used in many situations. "A pattern is a recurring solution to a problem in a context."

What is strategy pattern in C#?

Strategy is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside original context object. The original object, called context, holds a reference to a strategy object. The context delegates executing the behavior to the linked strategy object.


5 Answers

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.

like image 20
cwap Avatar answered Oct 23 '22 04:10

cwap


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.

like image 103
Grozz Avatar answered Oct 23 '22 05:10

Grozz


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.

like image 36
Kevin LaBranche Avatar answered Oct 23 '22 04:10

Kevin LaBranche


  class Character
  {
    private IWeaponBehavior _weapon;

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

    public void WeaponInterface()
    {
      _weapon.UseWeapon();
    }
  }
like image 21
SwDevMan81 Avatar answered Oct 23 '22 05:10

SwDevMan81


public class Character
{
    IWeaponBehavior Weapon;
}

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

public class Thief : Character
{
    Weapon = new Knife();
}
like image 29
Joshua Evensen Avatar answered Oct 23 '22 03:10

Joshua Evensen