C++设计模式 week1 (Boolan)

2017-12-30  本文已影响0人  YPAN

课程目标

推荐书目

深入理解面向对象

软件设计复杂的根本原因 ---- 变化

如何解决复杂性?

重新认识面向对象

面向对象设计原则1

面向对象设计原则2

面向对象设计原则3

面向对象设计原则4

面向对象设计原则5

面向对象设计原则6

面向对象设计原则7

面向对象设计原则8

GOF-23 模式分类

dp1.png

从封装变化角度对模式分类

dp2.png

Template Method

摘自GOF

  • Intent:
    Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
  • Application:
    The Template Method pattern should be used
    • to implement the invariant parts of an algorithm once and leave it up to
      subclasses to implement the behavior that can vary.
    • when common behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is a good example of "refactoring to generalize" as described by Opdyke and Johnson [OJ93]. You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations.
    • to control subclasses extensions. You can define a template method that calls "hook" operations (see Consequences) at specific points, thereby permitting extensions only at those points.
dp_tm.png

Strategy

摘自GOF

  • Intent
    Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
  • Applicability
    Use the Strategy pattern when
    • many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.
    • you need different variants of an algorithm. For example, you might define
      algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms [HO87].
    • an algorithm uses data that clients shouldn't know about. Use the Strategy
      pattern to avoid exposing complex, algorithm-specific data structures.
    • a class defines many behaviors, and these appear as multiple conditional
      statements in its operations. Instead of many conditionals, move related
      conditional branches into their own Strategy class.
dp_s.png

Observer

  • Intent
    Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • Applicability
    Use the Observer pattern in any of the following situations:
    • When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
    • When a change to one object requires changing others, and you don't know how many objects need to be changed.
    • When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.
dp_o.png

Decorator

  • Intent
    Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  • Applicability
    Use Decorator
    • to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
    • for responsibilities that can be withdrawn.
    • when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.
dp_d.png
上一篇 下一篇

猜你喜欢

热点阅读