結合策略 與 簡單工廠模式
結合策略 與 簡單工廠模式
結合策略 與 簡單工廠模式
結合策略 與 簡單工廠模式
繼https://davidchenblog.com/posts/9ad0ae13/#more
單純只用策略模式的話,會發現必須在client判斷使用哪一種演算法。
但結合策略與簡單工廠模式,就可以在class的建構子傳入要使用哪一種演算法。
Refactor CashContext class
1 | class CashContext |
簡單工廠模式
CashSuper csuper = new CashFactory.createCashAccept(type);
…=csuper.GetResult(…);
結合策略與簡單工廠模式
CashContext csuper = new CashContext(type);
…=csuper.GetResult(…);
簡單工廠模式需讓client使用到兩個class(CashSuper、CashFactory)
結合策略與簡單工廠模式只需要用到CashContext
耦合更低!