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 模式分类

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容