2011年4月21日 星期四

GoF Behavioral - Strategy Pattern


  • Intent


To define a family of functionality, encapsulate each one, and make them interchangeable. The Strategy pattern lets the functionality vary independently from the client that use it.


  • Benefits




  1. It provieds a substitute to subclassing.

  2. It defines each behavior within its own class, eliminating the need for conditional statements.

  3. It makes it easier to extend and incorporate behavior without changing the application.




  • 適用情境




  1. Multiple classes differ only in their behaviors. The servlet API is a classic example of this.

  2. Need different variations of an algorithm.

  3. Algorithm uses data that is unknown to the client.




  • Class Diagram




[caption id="attachment_275" align="alignnone" width="539" caption="Strategy Pattern"]Strategy Pattern[/caption]

沒有留言: