0%

Strategy Pattern 筆記

 

工作上遇到的類似情境 , 有多種廠牌需要影印 , 當有新的客戶就會有類似需求
看前人留下的 code 是用一個很肥的超級類別 , 然後每次有新客戶就插入類似的 AsusPrint , ApplePrint 等等 , 然後用 case 切換
大概類似這樣

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
class SuperPrinter
{
public void Print(string cat)
{
switch (cat)
{
case "Asus":
AsusPrint();
break;
case "Apple":
ApplePrint();
break;
default:
break;
}
}

public void ApplePrint()
{
Console.WriteLine("Apple");
}

public void AsusPrint()
{
Console.WriteLine("Asus");
}

//很肥的內部細節...
}

自己研究下發現 , 其實可以套用 Strategy Pattern 讓程式碼更好維護 , 不然真的想死 XD , Strategy Pattern 最大的重點就是可以 抽換演算法 正巧符合這個情境
其他高深寫法可以參考這篇

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
53
54
55
56
57
58
59
60
61
62
Printer printer = new Printer();
printer.Print();

printer.SetStrategy(new AsusStrategy());
printer.Print();

var dict = new Dictionary<string, PrintStrategy>
{
{"Asus", new AsusStrategy() },
{"Apple", new AppleStrategy() },
};
var asus = dict["Asus"];
var apple = dict["Apple"];

printer.SetStrategy(apple);
printer.Print();

printer.SetStrategy(asus);
printer.Print();

Console.ReadLine();

interface PrintStrategy
{
void Print();
}

class AsusStrategy : PrintStrategy
{
public void Print()
{
Console.WriteLine("Asus");
}
}

class AppleStrategy : PrintStrategy
{
public void Print()
{
Console.WriteLine("Apple");
}
}

class Printer
{
private PrintStrategy _printStrategy;
public Printer()
{
_printStrategy = new AppleStrategy();
}

//應該也可以在建構子強制讓人設定策略
public void SetStrategy(PrintStrategy printStrategy)
{
_printStrategy = printStrategy;
}

public void Print()
{
_printStrategy.Print();
}
}
關閉