Let’s understand the widely accepted definition of strategy pattern which you can find from various resources on net. “Strategy pattern defines a family of algorithms, encapsulates each one of them and makes them interchangeable.” Confused with the definition? let’s try to break it in three parts:-
1) Family of Algorithms- The definition says that the pattern defines the family of algorithms- it means we have functionality (in these algorithms) which will do the same common thing for our object, but in different ways.
2) Encapsulate each one of them- The pattern would force you to place your algorithms in different classes (encapsulate them). Doing so would help us in selecting the appropriate algorithm for our object.
3) Make them interchangeable- The beauty with strategy pattern is we can select at run time which algorithm we should apply to our object and can replace them with one another
For Example
1) Family of Algorithms- The definition says that the pattern defines the family of algorithms- it means we have functionality (in these algorithms) which will do the same common thing for our object, but in different ways.
2) Encapsulate each one of them- The pattern would force you to place your algorithms in different classes (encapsulate them). Doing so would help us in selecting the appropriate algorithm for our object.
3) Make them interchangeable- The beauty with strategy pattern is we can select at run time which algorithm we should apply to our object and can replace them with one another
public interface IWeaponBehavior
{
string UseWeapon();
}
public class KnifeBehavior : IWeaponBehavior
{
public string UseWeapon()
{
return "Knife Used";
}
}
public class AxeBehavior : IWeaponBehavior
{
public string UseWeapon()
{
return "Axe Used";
}
}
public class SwordBehavior : IWeaponBehavior
{
public string UseWeapon()
{
return "Sword Used";
}
}
public class BowAndArrowBehavior : IWeaponBehavior
{
public string UseWeapon()
{
return "Bow And Arrow Used";
}
}
public abstract class Character
{
protected IWeaponBehavior weapon;
public void SetWeapon(IWeaponBehavior w)
{
this.weapon = w;
}
public abstract string fight();
public string performfight()
{
return weapon.UseWeapon();
}
}
public class Queen : Character
{
public Queen()
{
weapon = new BowAndArrowBehavior();
}
public override string fight()
{
return "I am Queen";
}
}
public class King : Character
{
public King()
{
weapon = new SwordBehavior();
}
public override string fight()
{
return "I am King";
}
}
//Client End Calling
Character queen = new Queen();
Console.Write("\n" + queen.fight());
Console.Write("\n" + queen.performfight());
queen.SetWeapon(new SwordBehavior()); // Run time behavior change
Console.Write("\n" + queen.performfight());
Character king = new King();
Console.Write("\n\n" + king.fight());
Console.Write("\n" + king.performfight());
king.SetWeapon(new KnifeBehavior());// Run time behavior change
Console.Write("\n" + king.performfight());
king.SetWeapon(new AxeBehavior());// Run time behavior change
Console.Write("\n" + king.performfight());
Console.Read();
//******************************
// BELOW IS ANOTHER EXAMPLE
// THANKS to head first DP
//******************************
//******Set of behaviors as a family of alogrithms
public interface IFlyBehavior
{
string fly();
}
public class FlyWithWings : IFlyBehavior
{
public string fly()
{
return "fly with wings";
}
}
public class FlyNoWay : IFlyBehavior
{
public string fly()
{
return "Can't fly";
}
}
//************************************************
//******Set of behaviors as a family of alogrithms
public interface IQuackBehavior
{
string quack();
}
public class Quack : IQuackBehavior
{
public string quack()
{
return "Duck Quack";
}
}
public class Squeak : IQuackBehavior
{
public string quack()
{
return "Rubber Duckie Squeak";
}
}
public class MuteQuack : IQuackBehavior
{
public string quack()
{
return "do nothing can't quack";
}
}
//*************************************************
//***********CLIENT END*******************************
public abstract class Duck
{
protected IFlyBehavior FlyBehavior;
protected IQuackBehavior QuackBehavior;
public void setFlyBehavior(IFlyBehavior flybehav)
{
this.FlyBehavior = flybehav;
}
public void setQuackBehavior(IQuackBehavior quackbehav)
{
this.QuackBehavior = quackbehav;
}
public string performQuack()
{
return this.QuackBehavior.quack();
}
public string performFly()
{
return this.FlyBehavior.fly();
}
public abstract void display();
}
public class MallardDuck : Duck
{
public MallardDuck()
{
FlyBehavior = new FlyWithWings();
QuackBehavior = new Quack();
}
public override void display()
{
throw new NotImplementedException();
}
}
//***************************************************
Duck Duck = new MallardDuck();
Console.Write("\n" + Duck.performFly());
Console.Write("\n" + Duck.performQuack());
Duck.setFlyBehavior(new FlyNoWay()); // Run time behaviour change
Duck.setQuackBehavior(new Squeak()); // Run time behaviour change
Console.Write("\n" + Duck.performFly());
Console.Write("\n" + Duck.performQuack());
Console.Read();
No comments:
Post a Comment