I'm sure that you have already heard at least one of the following common design/development tips
- Seperate out parts of code that will be subject to change over time from the rest of your code
- Use has-a relationship instead of is-a relationship where possible, that is prefer composition over inheritance
Strategy pattern helps us to realize these tips in our designs. By applying the Strategy Pattern we move our algorithm implementations, which are possible source of maintainance issues, away from the more stable source code.
Download
GoF_Patterns_Strategy.rar (19,76 kb)
Recommended Reading
Design Patterns For Dummies by Steve Holzner
Example Implementation
In our sample scenario we have different kind of vechicles all supporting some kind of Go behaviour.
001 // Abstract algorithm class
002
003 public abstract class GoAlgorithm
004
005 {
006
007 public abstract string Go();
008
009 }
010
011
012
013 // Concrete implementation of the go algorithm for DRIVING VEHICLES
014
015 public class GoByDrivingAlgorithm : GoAlgorithm
016
017 {
018
019 public override string Go()
020
021 {
022
023 return "I'm driving now.";
024
025 }
026
027 }
028
029
030
031 // Concrete implementation of the go algorithm for FLYING VEHICLES
032
033 public class GoByFlyingAlgorithm : GoAlgorithm
034
035 {
036
037 public override string Go()
038
039 {
040
041 return "I'm flying now.";
042
043 }
044
045 }
046
047
048
049 // Concrete implementation of the go algorithm for FAST FLYING VEHICLES
050
051 public class GoByFlyingFastAlgorithm : GoAlgorithm
052
053 {
054
055 public override string Go()
056
057 {
058
059 return "I'm flying fast now.";
060
061 }
062
063 }
001 //Abstract Vehicle class
002
003 public abstract class Vehicle
004
005 {
006
007 public GoAlgorithm Algorithm { get; set; }
008
009 protected string Description { get; set; }
010
011
012
013 public void Go()
014
015 {
016
017 Console.WriteLine("I'm a " + Description + " and " + Algorithm.Go());
018
019 }
020
021
022
023 public Vehicle(GoAlgorithm algorithm)
024
025 {
026
027 Algorithm = algorithm;
028
029 }
030
031 }
032
033
034
035 //Concrete implementation
036
037 public class Car : Vehicle
038
039 {
040
041 public Car()
042
043 : base(new GoByDrivingAlgorithm())
044
045 {
046
047 Description = "Car";
048
049 }
050
051 }
052
053
054
055 //Concrete implementation
056
057 public class Helicopter : Vehicle
058
059 {
060
061 public Helicopter()
062
063 : base(new GoByFlyingAlgorithm())
064
065 {
066
067 Description = "Helicopter";
068
069 }
070
071 }
072
073
074
075 //Concrete implementation
076
077 public class Jet : Vehicle
078
079 {
080
081 public Jet()
082
083 : base(new GoByFlyingFastAlgorithm())
084
085 {
086
087 Description = "Jet";
088
089 }
090
091 }
001 class Program
002
003 {
004
005 static void Main(string[] args)
006
007 {
008
009 // I'm driving
010
011 Vehicle car = new Car();
012
013 car.Go();
014
015
016
017 // I'm flying
018
019 Vehicle helicopter = new Helicopter();
020
021 helicopter.Go();
022
023
024
025 // Change algorithm at runtime
026
027 Console.WriteLine(String.Empty);
028
029 Console.WriteLine("Change algorithm at runtime...");
030
031 Console.WriteLine(String.Empty);
032
033
034
035 Vehicle jet = new Jet();
036
037 jet.Algorithm = new GoByDrivingAlgorithm();
038
039 jet.Go();
040
041 jet.Algorithm = new GoByFlyingFastAlgorithm();
042
043 jet.Go();
044
045
046
047 Console.Read();
048
049 }
050
051 }
b2e1d107-ce45-47fa-87c2-ba169c092b18|1|4.0