Wednesday, June 25, 2014

The Strategy Pattern in C#

The Strategy Pattern is one of the most basic Design Patterns and is largely based on the OOP Principle of favoring Composition over Inheritance.

The Strategy Pattern is officially defined as:

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.  Strategy lets the algorithm vary independently from clients that use it. (Head First Design Patterns-Intro to Design Patterns)

Below is an implementation of the Strategy Pattern in C#:
 
void Main()
{
    Duck mallard = new MallardDuck();
            mallard.performQuack();
            mallard.performFly();
 
            Duck model = new ModelDuck();
            model.performFly();
            model.SetFlyBehavior = new FlyRocketPowered();
            model.performFly();
 
            
}
 
public abstract class Duck
    {
        protected FlyBehavior flyBehavior;
        protected QuackBehavior quackBehavior;
 
        public Duck()
        {
 
        }
 
        public FlyBehavior SetFlyBehavior
        {
            get
            {
                return flyBehavior;
            }
            set
            {
                flyBehavior = value;
            }
 
        }
 
        public QuackBehavior SetQuackBehavior
        {
            get
            {
                return quackBehavior;
            }
            set
            {
                quackBehavior = value;
            }
        }
 
        public abstract void display();
 
        public void performFly()
        {
            flyBehavior.fly();
        }
 
        public void performQuack()
        {
            quackBehavior.quack();
        }
 
        public void swim()
        {
            Console.WriteLine("All ducks float, even decoys!");
        }
 
    }
 
    public class MallardDuck : Duck
    {
        public MallardDuck()
        {
            quackBehavior = new Quack();
            flyBehavior = new FlyWithWings();
 
        }
        public override void display()
        {
            Console.WriteLine("I'm a real Mallard duck");
        }
    }
 
    public class ModelDuck : Duck
    {
        public ModelDuck()
        {
            flyBehavior = new FlyNoWay();
            quackBehavior = new Quack();
        }
        public override void display()
        {
            Console.WriteLine("I'm a model duck");
        }
    }
    
     public interface FlyBehavior
    {
        void fly();
    }
 
 public class FlyWithWings: FlyBehavior
    {
        public void fly()
        {
            Console.WriteLine("I'm flying!");
        }
    }
 
    public class FlyNoWay : FlyBehavior
    {
        public void fly()
        {
            Console.WriteLine("I can't fly");
        }
    }
 
    public class FlyRocketPowered : FlyBehavior
    {
        public void fly()
        {
            Console.WriteLine("I'm flying with a rocket!");
        }
    }
    
    public class Quack: QuackBehavior
    {
        public void quack()
        {
            Console.WriteLine("Quack");
        }
    }
 
    public class MuteQuack : QuackBehavior
    {
        public void quack()
        {
            Console.WriteLine("<<Silence>>");
        }
    }
 
    public class Squeak : QuackBehavior
    {
        public void quack()
        {
            Console.WriteLine("Squeak");
        }
    }
    
    public interface QuackBehavior
    {
        void quack();
    }



No comments:

Post a Comment