The Strategy Pattern is a proven design construct to vary operations or algorithms independently from the clients that use it. The pattern underwrites the Open / Closed Principle of S.O.L.I.D., stating that a class should be open to extension, but closed to modification. It also keeps a clean separation of concerns.
Lets say we go to our local car wash. We buy a car washing program at the store. We then drive our car inside the washing machine where it starts to execute the washing program. When we apply the Strategy Pattern to this model, we come up with something like shown below.
First we create a washing program strategy interface. We implement the interface to express our different washing programs. In this case we have to flavors; Basic and Deluxe. For simplicity the strategy ‘algorithm’ Wash() only writes a program process description to the console window. Normally we would find fancy calculations of some sort here.
using System;
namespace StrategyPattern
{
public interface ICarWashStrategy
{
void Wash(Car car);
}
public class BasicProgram : ICarWashStrategy
{
#region ICarWashStrategy Members
public void Wash(Car car)
{
Console.WriteLine(
"** Starting Basic Program on {0} **",
car.Name);
Console.WriteLine("Water");
Console.WriteLine("Soap");
Console.WriteLine("Clean");
Console.WriteLine("Dry");
Console.WriteLine("** DONE **");
}
#endregion
}
public class DeluxeProgram : ICarWashStrategy
{
#region ICarWashStrategy Members
public void Wash(Car car)
{
Console.WriteLine(
"** Starting Deluxe Program on {0} **",
car.Name);
Console.WriteLine("Pre Wash");
Console.WriteLine("Adding a lot of Water");
Console.WriteLine("Spraying Hot Soap");
Console.WriteLine("Tire Brush");
Console.WriteLine("Wrap-Around Washers");
Console.WriteLine("Rinse and Wax");
Console.WriteLine("Soft Dry");
Console.WriteLine("** DONE **");
}
#endregion
}
}
The CarWashService takes the washing program (the strategy) we have bought at the shop and initializes the washing machine with it.
namespace StrategyPattern
{
internal class CarWashService
{
private readonly ICarWashStrategy _carWashStrategy;
public CarWashService(ICarWashStrategy carWashStrategy)
{
_carWashStrategy = carWashStrategy;
}
public void Wash(Car car)
{
_carWashStrategy.Wash(car);
}
}
}
Using the CarWasService in an application should be pretty straight forward. We drive in our Opel Manta and start the washing operation by calling the Wash() method.
using System;
namespace StrategyPattern
{
internal class Program
{
private static void Main()
{
var car = new Car { Name = "Opel Manta" };
var washProgramStrategy = new DeluxeProgram();
var carWash = new CarWashService(washProgramStrategy);
carWash.Wash(car);
Console.ReadLine();
}
}
}
We can apply any washing program to the CarWashService we want. If new Washing Programs are added later, we don’t have to modify the CarWashService.
分享到:
相关推荐
strategy-pattern-in-unity
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。...
Head.First设计模式中的The Factory Pattern(工厂模式)ppt
前端大厂最新面试题-Strategy Pattern
定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法的变化可独立于使用它的客户。
the strategy book.epub, 2nd edition by steve fortune,pfizer
设计模式(22)-Strategy Pattern 设计模式(21)-Template Method Pattern 设计模式(20)-Visitor Pattern 设计模式(19)-Observer Pattern 设计模式(18)-Command Pattern 设计模式(17)-Chain of ...
主要介绍了C#策略模式(Strategy Pattern),以一个简单的实例讲述了C#策略模式的实现方法,包括策略模式的用途以及具体实现方法,需要的朋友可以参考下
The Swift programming language has transformed the ... The Strategy Pattern Chapter 25. The Visitor Pattern Chapter 26. The Template Method Pattern Part 5 - The MVC Pattern Chapter 27. The MVC Pattern
All Weather Strategy
We start by running you through new features in C# 7, such as tuples, pattern matching, and so on, giving you hands-on experience with them. Moving forward, you will work with generics and the OOP ...
Abstract—The variation of the remanence of the Permanent Magnet (PM), according to operating temperature and manufacturing tolerance, especially in the magnetization of the magnet, results in ...
Each pattern is introduced with a non-technical example or story that illustrates the pattern concept. The details are described with Java code examples and UML diagrams. Each pattern description ...
Communicating the strategy: Convey your technology strategy in a compelling way to a variety of audiences Bringing it all together: Employ patterns individually or in clusters for specific problems;...
关于战略的HBR论文~ Developing_the_Strategy_Vision,Value_Gaps,_and_Analysis
head-first-design-pattern源码,第一章strategy-pattern(策略模式),融入了自己的见解,有一些错误的实现,也有一些标准答案,对照着看更能理解模式的精髓。
备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...
创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4.... 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)
NULL 博文链接:https://bossshit.iteye.com/blog/715672
• Chapter 9: The Strategy Pattern • Chapter 10: The Bridge Pattern • Chapter 11: The Abstract Factory Pattern • Chapter 12: How Do Experts Design? • Chapter 13: Solving The CAD/CAM Problem ...