zoukankan      html  css  js  c++  java
  • 设计模式学习每天一个——Strategy模式

    From http://www.codeproject.com/Articles/889/Applying-Strategy-Pattern-in-C-Applications

    The Strategy Pattern is a design pattern to encapsulate the variants (algorithms) and swap them strategically to alter system behavior without changing its architecture. According to GoF, Strategy Pattern is intended to, Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

    Strategy Pattern has three participants that include Strategy, Concrete Strategy and Context.

    Benefits in using Strategy Pattern

    1. A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.
    2. By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.
    3. The application can switch strategies at run-time.
    4. Strategy enables the clients to choose the required algorithm, without using a "switch" statement or a series of "if-else" statements.
    5. Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.
    6. Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.
    7. The same Strategy object can be strategically shared between different Context objects. However, the sharedStrategy object should not maintain states across invocations.

    Drawbacks in using Strategy Pattern

      1. The application must be aware of all the strategies to select the right one for the right situation.
      2. Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategyfor implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.
      3. Context and the Strategy classes normally communicate through the interface specified by the abstract Strategybase class. Strategy base class must expose interface for all the required behaviors, which some concreteStrategy classes might not implement.
      4. In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.
      5. Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.

    总结:策略模式VS工厂模式

  • 相关阅读:
    我用Python爬虫挣钱的那点事
    猿人学 . 爬虫逆向高阶课
    Python中实用却不常见的小技巧
    Python内存数据序列化到硬盘上哪家强
    利用setuptools发布Python程序到PyPI,为Python添砖加瓦
    配置tmux在机器重启后自动恢复tmux工作现场,告别重启恐惧症
    用python实现新词发现程序——基于凝固度和自由度
    学习笔记:Python序列化常用工具及性能对比
    浅谈自然语言在科技时代的运用
    python学习笔记:建立一个自己的搜索引擎
  • 原文地址:https://www.cnblogs.com/windy86/p/3974465.html
Copyright © 2011-2022 走看看