舉個例子,今天我寫了一個壓縮軟體,這個軟體會針對不同的輸入來採用不同的壓縮方法處理。
最基本的架構就是

然後該 compress function 可能長這樣
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public Object compress(Object input){
if(input.type == TYPE1){ } else if (input.type == TYPE2){ } else if (input.type == TYPE3){ } if(input.getEncoding() == TYPEA){ } else if(input.getEncoding() ==TYPEB){ } else if(input.getEncoding() ==TYPEC || input.getEncoding() ==TYPED){ } }
|
這種程式架構再維護上過於麻煩,會有下列問題
如果今天選項夠多(part更多)的話,整個程式會變得很難處理,每次要增加一個新的算法,就要到很多地方去增加對應的code,
在處理上容易出錯且維護也不易。
使用 Stragegy pattern的話,架構如下

設計ㄧ個介面Algorithm, 然後每個算法都實現這個介面,自己去完成自己的算法邏輯
當程式要處理壓縮的時候,就根據輸入物件來產生與之對應的演算法物件,然後去處理。
這樣每個算法都獨立來看,邏輯清楚明瞭,而且要新增加ㄧ個算法的話,只要再寫一個新的物件實現自共同的介面即可。
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
| public class CompressProgram{ public void process(){ Algorithm algorithm = getAlgorithmByType(input); Compressor compressor = new COmpressor(); Object result = compressor.doCompress(input,algorithm); } private Algorithm getAlgorithmByType(Object input){ if(input.type ==TYPEA){ return new AlgorithmA(); } else if(input.type ==TYPEB){ return new AlgorithmB(); } else if(input.type ==TYPEC){ return new AlgorithmC(); } } }
public class Compressor{ public Object doCompress(Object input,Algorithm algorithm){ return algorithm.compress(input); } }
public abstrace class Algorithm{ abstract public Object compress(Object input); }
public class AlgorithmA extends Algorithm{ public Object compress(Object input){ } }
public class AlgorithmB extends Algorithm{ public Object compress(Object input){ } }
public class AlgorithmC extends Algorithm{ public Object compress(Object input){ } }
|
個人資訊
我目前於 Hiskio 平台上面有開設 Kubernetes 相關課程,歡迎有興趣的人參考並分享,裡面有我從底層到實戰中對於 Kubernetes 的各種想法
組合包
https://hiskio.com/packages/7ey2vdnyN
疑難雜症除錯篇
https://hiskio.com/courses/440/about?promo_code=LG28Q5G
單堂(CI/CD)
https://hiskio.com/courses/385?promo_code=13K49YE&p=blog1
基礎概念
https://hiskio.com/courses/349?promo_code=13LY5RE
另外,歡迎按讚加入我個人的粉絲專頁,裡面會定期分享各式各樣的文章,有的是翻譯文章,也有部分是原創文章,主要會聚焦於 CNCF 領域
https://www.facebook.com/technologynoteniu
如果有使用 Telegram 的也可以訂閱下列頻道來,裡面我會定期推播通知各類文章
https://t.me/technologynote
你的捐款將給予我文章成長的動力