C++设计模式 week1 (Boolan)

课程目标

  • 理解松耦合设计思想
  • 掌握面向对象设计原则
  • 掌握重构技法改善设计
  • 掌握GOF核心设计模式

推荐书目

  • GOF 设计模式: GOF(group of four), 历史性著作"设计模式:可复用面向对象软件的基础"一书中描述了23种经典面向对象设计模式, 创立了模式在软件设计中的地位. 由于"设计模式"一书确定了设计模式的地位,通常所说的设计模式隐含地表示"面向对象设计模式". 但这并不意味"设计模式"就等于"面向对象设计模式".

深入理解面向对象

  • 向下: 理解三大面向对象机制
    • 封装, 隐藏内部实现
    • 继承, 复用现有代码
    • 多态, 改写对象行为
  • 向上: 深刻把握面向对象机制所带来的抽象意义, 理解如何使用这些机制来表达现实世界, 掌握什么是"好的面向对象设计"

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

  • 客户需求的变化
  • 技术平台的变化
  • 开发团队的变化
  • 市场环境的变化
    ……

如何解决复杂性?

  • 分解

    • 人们面对复杂性有一个常见的做法:即分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。
  • 抽象

    • 更高层次来讲,人们处理复杂性有一个通用的技术,即抽象出由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。
      重要的建立一些思想和模型

重新认识面向对象

  • 理解隔离变化
    • 从宏观层面来看,面向对象的构建方式更能适应软件的变化,能将变化所带来的影响减为最小。
  • 各司其职
    • 从微观层面来看,面向对象的方式更强调各类的“责任”
    • 由于需求变化导致的新增类型不应该影响原来类型的实现——是所谓各负其责
  • 对象是什么?
    • 从语言实现层面来看,对象封装了代码和数据。
    • 从规格层面讲,对象是一系列可被使用的公共接口。
    • 从概念层面讲,对象是某种拥有责任的抽象

面向对象设计原则1

  • 依赖倒置原则(DIP)
    • 高层模块(稳定)不应依赖于低层模块(变化),二者应该依赖于抽象(稳定)。
    • 抽象(稳定)不应该依赖于实现细节(变化),实现细节应该依赖于抽象(稳定)。

面向对象设计原则2

  • 开放封闭原则(OCP)
    • 对扩展开放,对更改封闭。
    • 类模块应该是可扩展的,但是不可修改。

面向对象设计原则3

  • 单一职责原则(SRP)
    • 一个类应该仅有一个引起它变化的原因。
    • 变化的方向隐含着类的责任。

面向对象设计原则4

  • Liskov替换原则(LSP)
    • 子类必须能够替换它们的基类(IS-A)。
    • 继承表达类型抽象。

面向对象设计原则5

  • 接口隔离原则(ISP)
    • 不应该强迫客户依赖它们不用的方法。
    • 接口应该小而完备。
      一旦产生依赖,就必须保持稳定。

面向对象设计原则6

  • 优先使用对象组合,而不是类继承
    • 类继承通常为“白箱复用”,对象组合通常为“黑箱复用”。
    • 继承在某种程度上破坏了封装性,子类父类耦合度高。
    • 而对象组合则要求被组合的对象具有良好定义的接口耦合度低。

面向对象设计原则7

  • 封装变化点
    • 使用封装来创建对象之间的分界层,让设计者可以在分界层的一侧进行修改,而不会对另一侧产生不良影响从而实现层次间的松耦合。

面向对象设计原则8

  • 针对接口编程,而不是针对实现变成
    • 不将变量类型声明为某个特定的具体类,而是声明为某个接口。
    • 客户程序无需获知对象的具体类型,只需要知道对象所具有的接口。
    • 减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案。

GOF-23 模式分类

C++设计模式 week1 (Boolan)_第1张图片
dp1.png

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

C++设计模式 week1 (Boolan)_第2张图片
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.
C++设计模式 week1 (Boolan)_第3张图片
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.
C++设计模式 week1 (Boolan)_第4张图片
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.
C++设计模式 week1 (Boolan)_第5张图片
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.
C++设计模式 week1 (Boolan)_第6张图片
dp_d.png

你可能感兴趣的:(C++设计模式 week1 (Boolan))