繼上一篇Strategy Pattern 討論到程式碼並不完美,主要缺失必須要瞭解每個算法與Context才能自在使用。比較好處理方式可將Context包裝成CompressionFactory或CompressionService方便其他人呼叫使用,但如何去建構?
自行撰寫雖然也不是問題,但似乎太浪費時間在處理此細節。
Tapestry IoC framework提供 StrategyBuilder 容易建立Strategy Pattern
A service implementation builder that operates around a StrategyRegistry, implementing a version of the Gang of Four Strategy pattern. The constructed service is configured with a number of adapters (that implement the same service interface). Method invocations on the service are routed to one of the adapters.