策略模式
策略模式一般情况下应用于当一个对象有某个行为,但是在不同的场景有不同的实现方式,例如,旅游的出行方式,我们可以选择骑自行车、也可以选择坐汽车、飞机、轮船等等,每一种旅行方式都是一个策略
如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。
# 优点
算法可以自由切换。
避免使用多重条件判断。
扩展性良好。
# 缺点
策略类会增多。
所有策略类都需要对外暴露。
本文中的代码在Delphi10.4中测试通过,有的朋友版本可能略低,只需要把变量的声明方式改变一下即可
uStrategy:策略模式的核心代码
unit uStrategy;
interface
type
{策略接口}
IStrategy = interface(IUnKnown)
['{E919FE8C-EEF8-4B86-846B-B48D65797DDF}']
//制定策略
function doOperation(num1, num2: Integer): Integer;
end;
//策略的具体实现方式一
TOperationAdd = class(TInterfacedObject, IStrategy)
public
function doOperation(num1, num2: Integer): Integer;
end;
//策略的具体实现方式二
TOperationSubtract = class(TInterfacedObject, IStrategy)
public
function doOperation(num1, num2: Integer): Integer;
end;
//真正使用策略的类
TOperation = class(TObject)
public
//Strategy:策略的类型需要使用接口类型,这样传任意的策略接口实现类即可
function ExecuteStrategy(num1, num2: Integer; Strategy: IStrategy): Integer;
end;
implementation
{ TOperationAdd }
function TOperationAdd.doOperation(num1, num2: Integer): Integer;
begin
Result := num1 + num2;
end;
{ TOperationSubtract }
function TOperationSubtract.doOperation(num1, num2: Integer): Integer;
begin
Result := num1 - num2;
end;
{ TOperation }
function TOperation.ExecuteStrategy(num1, num2: Integer; Strategy: IStrategy): Integer;
begin
Result := Strategy.doOperation(num1, num2);
end;
end.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
测试代码单元
program ProjecttTategy;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
uStrategy in 'uStrategy.pas';
begin
var Operation := TOperation.Create();
//我们只需要更换不同的策略实现对象,即可完成不同的运算,而TOperation类中的代码不需要做任何更改
var res := Operation.ExecuteStrategy(1, 2, TOperationAdd.Create());
writeln(res.ToString);
readln;
end.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
这个例子我以加法和减法运算作为不同的实现的方式,目的是降低设计模式的学习难度,本来写了个结合泛型来实现数组排序的,但是会让策略模式变的难以理解,所以被我废弃了,后面有机会再和大家分享