考慮您要設(shè)計(jì)一個(gè)更換各種符號(hào)的工具類TextCharChange,您是否會(huì)採(cǎi)用這樣的方式:
public void replace() {
switch(getChangeType()) {
case RN_TYPE: replaceRN();
break;
case N_TYPE: replaceN();
break;
case OTHER_TYPE: replaceOTHER():
break;
...
}
}
這麼作的缺點(diǎn)是,日後您要增加更換符號(hào)的策略時(shí),會(huì)有幾個(gè)地方需要修改:增加TYPE常數(shù)、增加TextCharChange中的 replaceXXX()方法、增加 replace()方法中的switch case判斷。
像這種策略採(cǎi)用的情況,可以將策略加以封裝為一個(gè)物件,而不是將策略寫死在某個(gè)類中,如此一來(lái),策略可以獨(dú)立於客戶端,隨時(shí)增加變化、增加或減少策略,即使是修改每個(gè)策略的內(nèi)容,也不會(huì)對(duì)客戶端程式造成影響。
來(lái)舉個(gè)最簡(jiǎn)單的例子,首先要知道Windows與Linux的文字檔案換行符號(hào)是不同的,Windows是 /r/n ,而Linux是 /n,今天您要設(shè)計(jì)一個(gè)文字編輯器,在適當(dāng)?shù)臅r(shí)候,您必須要能隨時(shí)轉(zhuǎn)換這兩種符號(hào),如果不採(cǎi)用上面的策略採(cǎi)用流程的話,要如何設(shè)計(jì):
public abstract class TextStrategy {
protected String text;
public TextStrategy(String text) {
this.text = text;
}
public abstract String replace();
}
public class LinuxStrategy extends TextStrategy {
public LinuxStrategy(String text) {
super(text);
}
public String replace() {
preOperation();
System.out.println(
text = text.replaceAll("@r@n", "@n"));
postOperation();
return text;
}
private void preOperation() {
System.out.println("LinuxStrategy preOperation");
}
private void postOperation() {
System.out.println("LinuxStrategy postOperation");
}
}
public class WindowsStrategy extends TextStrategy {
public WindowsStrategy(String text) {
super(text);
}
public String replace() {
startOperation();
System.out.println(
text = text.replaceAll("@n", "@r@n"));
endOperation();
return text;
}
private void startOperation() {
System.out.println("WindowsStrategy startOperation");
}
private void endOperation() {
System.out.println("WindowsStrategy endOperation");
}
}
public class TextCharChange {
public static void replace(TextStrategy strategy) {
strategy.replace();
}
}
public class Main {
public static void main(String[] args) {
String linuxText =
"This is a test text!!@n Oh! Line Return!!@n";
String windowsText =
"This is a test text!!@r@n Oh! Line Return@r@n";
// load file, suppose it's Linux's text file
// take the WindowsStrategy
// I want to change it to Windows' text file
TextCharChange.replace(
new WindowsStrategy(linuxText));
// such-and-such operation.....
System.out.println();
// load file, suppose it's Windows' text file
// take the LinuxStrategy
// I want to change it to Linux's text file
TextCharChange.replace(
new LinuxStrategy(windowsText));
}
}
為了明顯的秀出結(jié)果,我們使用@n來(lái)表示 '/n' , @r 表示 '/r' 符號(hào),Main中的流程是個(gè)假設(shè)的情況,何時(shí)採(cǎi)用何種策略是隨機(jī)的。
在Strategy模式中,使用一個(gè)公開(kāi)的介面replace(),讓客戶端請(qǐng)求,而在實(shí)作replace()時(shí),可以任意的組合演算策略,程式中的 preOperation()、postOperation()就是用以示意演算的組合概念,Strategy模式封裝了這些演算過(guò)程,使它們易於組合、修改、替換,上面這個(gè)例子的UML 類別結(jié)構(gòu)圖如下所示:
Strategy模式的UML類別結(jié)構(gòu)圖如下:
從行為上來(lái)說(shuō),
State 模式 與Strategy模式是蠻相近的。
State模式:看當(dāng)前是什麼狀態(tài),就採(cǎi)取什麼動(dòng)作。
Strategy模式:看需求(情境)是什麼,採(cǎi)用適當(dāng)?shù)牟呗浴?br>
不過(guò)兩者雖相似,應(yīng)用的場(chǎng)合稍有不同,State模式中有一個(gè)重點(diǎn)在於設(shè)定狀態(tài)變化,就像 Gof 例子中舉的TCP連線;Strategy策略模式則是直接採(cǎi)用適當(dāng)?shù)牟呗缘母杏X(jué),例如Gof中說(shuō)的,採(cǎi)用適當(dāng)?shù)难菟惴▉?lái)作正文換行。