Thursday, May 29, 2008

Strategy Design Pattern

Strategy Design Pattern:
Have been reading Strategy design pattern from various books including Design Patterns in C# and Design Pattern for dummies.

Use when there is a section of code that will change frequently.
1. Instead of modelling is a relationship

like Class Vehicle
then Class car:Vehicle
        Class plane:Vehicle,

        We do class Vehicle:IDriving;
                so car:Vehicle, IDriving
                and plane:Vehicle, IDriving
           which is not so good because if something changes then it has
           to be changed in each class file.


                               so

1. Create a n interface that the subclasses will use
        in this case

 public Interface Igo{
                        public void  go();
                                              
                        }


2  public class goDriving: Igo
  {

   go(){
   System.Console.Writeline("Driving");
   }

  }
3.
  public class goFlying: Igo
  {

   go(){
   System.Console.Writeline("Flying");
   }

  }


4.
  public abstract class Vehicle
  {
     private Igo goMethod;
          
  }


No comments: