策略模式(Strategy)
意图
策略模式是一种行为型模式,它能让你定义一系列算法,并将每种算法分别放入独立的类中,以使算法的对象能够相互替换。
问题
一天,你打算为游客们创建一款导游程序。该程序的核心功能是提供美观的地图,以帮助用户在任何城市中快速定位。
用户期待的程序新功能是自动路线规划:他们希望输入地址后就能在地图上看到前往目的地的最快路线。
程序的首个版本只能规划公路路线。驾车旅行的人们对此非常满意。但很显然,并非所有人都会在度假时开车。因此你在下次更新时添加了规划步行路线的功能。此后,你又添加了规划公共交通路线的功能。
而这只是个开始。不久后,你又要为骑行者规划路线。又过了一段时间,你又要为游览城市中的所有景点规划路线。
尽管从商业角度来看,这款应用非常成功,但其技术部分却让你非常头疼:每次添加新的路线规划算法后,导游应用中主要类的体积就会增加一倍。终于在某个时候,你觉得自己没法继续维护这堆代码了。
无论是修复简单缺陷还是微调街道权重,对某个算法进行任何修改都会影响整个类,从而增加在已有正常运行代码中引入错误的风险。
此外,团队合作将变得低效。如果你在应用成功发布后招募了团队成员,他们会抱怨在合并冲突的工作上花费了太多时间。在实现新功能的过程中,你的团队需要修改同一个巨大的类,这样他们所编写的代码相互之间就可能会出现冲突。
解决方案
策略模式建议找出负责用许多不同方式完成特定任务的类,然后将其中的算法抽取到一组被称为策略的独立类中。
名为上下文的原始类必须包含一个成员变量来存储对于每种策略的引用。上下文并不执行任务,而是将工作委派给已连接的策略对象。
上下文不负责选择符合任务需要的算法——客户端会将所需策略传递给上下文。实际上,上下文并不十分了解策略,它会通过同样的通用接口与所有策略进行交互,而该接口只需暴露一个方法来触发所选策略中封装的算法即可。
因此,上下文可独立于具体策略。这样你就可在不修改上下文代码或其他策略的情况下添加新算法或修改已有算法了。
在导游应用中,每个路线规划算法都可被抽取到只有一个 buildRoute生成路线方法的独立类中。该方法接收起点和终点作为参数,并返回路线中途点的集合。
即使传递给每个路径规划类的参数一模一样,其所创建的路线也可能完全不同。主要导游类的主要工作是在地图上渲染一系列中途点,不会在意如何选择算法。该类中还有一个用于切换当前路径规划策略的方法,因此客户端(例如用户界面中的按钮)可用其他策略替换当前选择的路径规划行为。
结构
- 上下文(Context)维护指向具体策略的引用,且仅通过策略接口与该对象进行交流。
- 策略(Strategy)接口是所有具体策略的通用接口,它声明了一个上下文用于执行策略的方法。
- 具体策略(Concrete Strategies)实现了上下文所用算法的各种不同变体。
- 当上下文需要运行算法时,它会在其已连接的策略对象上调用执行方法。上下文不清楚其所涉及的策略类型与算法的执行方式。
- 客户端(Client)会创建一个特定策略对象并将其传递给上下文。上下文则会提供一个设置器以便客户端在运行时替换相关联的策略。
实现方式
- 从上下文类中找出修改频率较高的算法(也可能是用于在运行时选择某个算法变体的复杂条件运算符)。
- 声明该算法所有变体的通用策略接口。
- 将算法逐一抽取到各自的类中,它们都必须实现策略接口。
- 在上下文类中添加一个成员变量用于保存对于策略对象的引用。然后提供设置器以修改该成员变量。上下文仅可通过策略接口同策略对象进行交互,如有需要还可定义一个接口来让策略访问其数据。
- 客户端必须将上下文类与相应策略进行关联,使上下文可以预期的方式完成其主要工作。
代码演示
using System;
using System.Collections.Generic;
namespace RefactoringGuru.DesignPatterns.Strategy.Conceptual
{
// The Context defines the interface of interest to clients.
class Context
{
// The Context maintains a reference to one of the Strategy objects. The
// Context does not know the concrete class of a strategy. It should
// work with all strategies via the Strategy interface.
private IStrategy _strategy;
public Context()
{ }
// Usually, the Context accepts a strategy through the constructor, but
// also provides a setter to change it at runtime.
public Context(IStrategy strategy)
{
this._strategy = strategy;
}
// Usually, the Context allows replacing a Strategy object at runtime.
public void SetStrategy(IStrategy strategy)
{
this._strategy = strategy;
}
// The Context delegates some work to the Strategy object instead of
// implementing multiple versions of the algorithm on its own.
public void DoSomeBusinessLogic()
{
Console.WriteLine("Context: Sorting data using the strategy (not sure how it'll do it)");
var result = this._strategy.DoAlgorithm(new List<string> { "a", "b", "c", "d", "e" });
string resultStr = string.Empty;
foreach (var element in result as List<string>)
{
resultStr += element + ",";
}
Console.WriteLine(resultStr);
}
}
// The Strategy interface declares operations common to all supported
// versions of some algorithm.
//
// The Context uses this interface to call the algorithm defined by Concrete
// Strategies.
public interface IStrategy
{
object DoAlgorithm(object data);
}
// Concrete Strategies implement the algorithm while following the base
// Strategy interface. The interface makes them interchangeable in the
// Context.
class ConcreteStrategyA : IStrategy
{
public object DoAlgorithm(object data)
{
var list = data as List<string>;
list.Sort();
return list;
}
}
class ConcreteStrategyB : IStrategy
{
public object DoAlgorithm(object data)
{
var list = data as List<string>;
list.Sort();
list.Reverse();
return list;
}
}
class Program
{
static void Main(string[] args)
{
// The client code picks a concrete strategy and passes it to the
// context. The client should be aware of the differences between
// strategies in order to make the right choice.
var context = new Context();
Console.WriteLine("Client: Strategy is set to normal sorting.");
context.SetStrategy(new ConcreteStrategyA());
context.DoSomeBusinessLogic();
Console.WriteLine();
Console.WriteLine("Client: Strategy is set to reverse sorting.");
context.SetStrategy(new ConcreteStrategyB());
context.DoSomeBusinessLogic();
}
}
}
执行结果:
Client: Strategy is set to normal sorting.
Context: Sorting data using the strategy (not sure how it'll do it)
a,b,c,d,e
Client: Strategy is set to reverse sorting.
Context: Sorting data using the strategy (not sure how it'll do it)
e,d,c,b,a
参考原文:策略设计模式
相关文章
- 二十七、策略模式 ( Strategy Pattern )
- 前端的设计模式系列-策略模式
- JavaScript设计模式—-策略模式[通俗易懂]
- java策略模式例子_java 登录场景 策略模式
- 高频js手写题之实现数组扁平化、深拷贝、总线模式
- 利用策略模式优化过多 if else 代码
- 真香系列:IPSec策略模板模式、模板模式、策略模式的区别
- 设计模式-观察者模式
- Java设计模式之策略模式
- 【数据挖掘】数据挖掘算法 组件化思想 ( 模型或模式结构 | 数据挖掘任务 | 评分函数 | 搜索和优化算法 | 数据管理策略 )
- JAVA SPI设计模式之策略模式文字版主页有视频
- 设计模式之策略模式(行为型)详解架构师
- 微软发布新声明表示默认情况下Windows 11系统将以浅色模式提供
- Linux下网卡工作模式探究(网卡工作模式linux)
- Linux系统教程:单用户模式破解root密码
- 深入了解 Oracle 模式对象,优化数据库管理解决方案(oracle的模式对象)
- Oracle 数据库归档模式开启: 如何优化你的数据备份策略(oracle归档模式开启)
- 启动Redis集群模式实现高可用性(启动redis集群模式)
- 灵活运用Redis转移哨兵模式实现高可用(redis转移哨兵模式)
- php中的MVC模式运用技巧
- 关于.NETFramework中的设计模式--应用策略模式为List排序
- 关于Flyweight模式应用实践的相关介绍
- jQuery的观察者模式详解
- FTP主动模式、被动模式介绍