设计模式简介

目录

 

1. 设计模式简介

课程目标

什么是设计模式

GOF 设计模式

从面向对象谈起

深入理解面向对象

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

如何解决复杂性?

软件设计的目标

2. 面向对象设计原则

面向对象设计,为什么?

重新认识面向对象

八种设计原则:

面向接口设计

将设计原则提升为设计经验

3. 模板方法 Template Method

GOF-23 模式分类

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

重构获得模式 Refactoring to Patterns

推荐图书

重构关键技法


1. 设计模式简介

课程目标

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

 

什么是设计模式

“每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次又一次地使用该方案而不必做重复劳动。”

 

GOF 设计模式

  • 历史性著作《设计模式:可复用面向对象软件的基础》一书中描述了23种经典面向对象设计模式,创立了模式在软件设计中的地位。
    可复用是一个设计目标,面向对象是手法
  • 由于《设计模式》一书确定了设计模式的地位,通常所说的设计模式隐含地表示“面向对象设计模式”。还有其他的模式,架构模式,数据库模式

从面向对象谈起

  • 底层思维:向下,如何把握机器底层从微观理解对象构造
    • 语言构造
    • 编译转换
    • 内存模型
    • 运行时机制
  • 抽象思维:向上,如何将我们的周围世界抽象为程序代码
    • 面向对象
    • 组建封装
    • 设计模式
    • 架构模式

深入理解面向对象

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

 

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

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

如何解决复杂性?

  • 分解
    人们面对复杂性有一个常见的做法:即分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。
  • 抽象
    更高层次来讲,人们处理复杂性有一个通用的技术,即抽象出由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。

第一种分解的设计方法

//Shape.h
class Point{
public:
    int x;
    int y;
};
 
class Line{
public:
    Point start;
    Point end;
 
    Line(const Point& start, const Point& end){
        this->start = start;
        this->end = end;
    }
 
};
 
class Rect{
public:
    Point leftUp;
    int width;
    int height;
 
    Rect(const Point& leftUp, int width, int height){
        this->leftUp = leftUp;
        this->width = width;
        this->height = height;
    }
 
};
 
 

//MainForm.cpp 
class MainForm : public Form {
private:
    Point p1;
    Point p2;
 
    vector<Line> lineVector;
    vector<Rect> rectVector;
 
public:
    MainForm(){
        //...
    }
protected:
 
    virtual void OnMouseDown(const MouseEventArgs& e);
    virtual void OnMouseUp(const MouseEventArgs& e);
    virtual void OnPaint(const PaintEventArgs& e);
};
 
 
void MainForm::OnMouseDown(const MouseEventArgs& e){
    p1.x = e.X;
    p1.y = e.Y;
 
    //...
    Form::OnMouseDown(e);
}
 
void MainForm::OnMouseUp(const MouseEventArgs& e){
    p2.x = e.X;
    p2.y = e.Y;
 
    if (rdoLine.Checked){
        Line line(p1, p2);
        lineVector.push_back(line);
    }
    else if (rdoRect.Checked){
        int width = abs(p2.x - p1.x);
        int height = abs(p2.y - p1.y);
        Rect rect(p1, width, height);
        rectVector.push_back(rect);
    }
 
    //...
    this->Refresh();
 
    Form::OnMouseUp(e);
}
 
void MainForm::OnPaint(const PaintEventArgs& e){
 
    //针对直线
    for (int i = 0; i < lineVector.size(); i++){
        e.Graphics.DrawLine(Pens.Red,
            lineVector[i].start.x, 
            lineVector[i].start.y,
            lineVector[i].end.x,
            lineVector[i].end.y);
    }
 
    //针对矩形
    for (int i = 0; i < rectVector.size(); i++){
        e.Graphics.DrawRectangle(Pens.Red,
            rectVector[i].leftUp,
            rectVector[i].width,
            rectVector[i].height);
    }
 
    //...
    Form::OnPaint(e);
}


上面这个伪代码可以用来解决一些图形的绘制,比如矩形,直线和点的形状。但是现在会遇到一个很严重的问题,我们应该如何来绘制一个圆呢,我们需要在多处修改相应的代码

class Point{
public:
    int x;
    int y;
};
 
class Line{
public:
    Point start;
    Point end;
 
    Line(const Point& start, const Point& end){
        this->start = start;
        this->end = end;
    }
 
};
 
class Rect{
public:
    Point leftUp;
    int width;
    int height;
 
    Rect(const Point& leftUp, int width, int height){
        this->leftUp = leftUp;
        this->width = width;
        this->height = height;
    }
 
};
 
//增加
class Circle{
 
 
};
 
 
class MainForm : public Form {
private:
    Point p1;
    Point p2;
 
    vector<Line> lineVector;
    vector<Rect> rectVector;
    //改变
    vector<Circle> circleVector;
 
public:
    MainForm(){
        //...
    }
protected:
 
    virtual void OnMouseDown(const MouseEventArgs& e);
    virtual void OnMouseUp(const MouseEventArgs& e);
    virtual void OnPaint(const PaintEventArgs& e);
};
 
 
void MainForm::OnMouseDown(const MouseEventArgs& e){
    p1.x = e.X;
    p1.y = e.Y;
 
    //...
    Form::OnMouseDown(e);
}
 
void MainForm::OnMouseUp(const MouseEventArgs& e){
    p2.x = e.X;
    p2.y = e.Y;
 
    if (rdoLine.Checked){
        Line line(p1, p2);
        lineVector.push_back(line);
    }
    else if (rdoRect.Checked){
        int width = abs(p2.x - p1.x);
        int height = abs(p2.y - p1.y);
        Rect rect(p1, width, height);
        rectVector.push_back(rect);
    }
    //改变
    else if (...){
        //...
        circleVector.push_back(circle);
    }
 
    //...
    this->Refresh();
 
    Form::OnMouseUp(e);
}
 
void MainForm::OnPaint(const PaintEventArgs& e){
 
    //针对直线
    for (int i = 0; i < lineVector.size(); i++){
        e.Graphics.DrawLine(Pens.Red,
            lineVector[i].start.x, 
            lineVector[i].start.y,
            lineVector[i].end.x,
            lineVector[i].end.y);
    }
 
    //针对矩形
    for (int i = 0; i < rectVector.size(); i++){
        e.Graphics.DrawRectangle(Pens.Red,
            rectVector[i].leftUp,
            rectVector[i].width,
            rectVector[i].height);
    }
 
    //改变
    //针对圆形
    for (int i = 0; i < circleVector.size(); i++){
        e.Graphics.DrawCircle(Pens.Red,
            circleVector[i]);
    }
 
    //...
    Form::OnPaint(e);
}

第二种方法,抽象

class Shape{
public:
    virtual void Draw(const Graphics& g)=0;
    virtual ~Shape() { }
};
 
 
class Point{
public:
    int x;
    int y;
};
 
class Line: public Shape{
public:
    Point start;
    Point end;
 
    Line(const Point& start, const Point& end){
        this->start = start;
        this->end = end;
    }
 
    //实现自己的Draw,负责画自己
    virtual void Draw(const Graphics& g){
        g.DrawLine(Pens.Red, 
            start.x, start.y,end.x, end.y);
    }
 
};
 
class Rect: public Shape{
public:
    Point leftUp;
    int width;
    int height;
 
    Rect(const Point& leftUp, int width, int height){
        this->leftUp = leftUp;
        this->width = width;
        this->height = height;
    }
 
    //实现自己的Draw,负责画自己
    virtual void Draw(const Graphics& g){
        g.DrawRectangle(Pens.Red,
            leftUp,width,height);
    }
 
};
 
 
class MainForm : public Form {
private:
    Point p1;
    Point p2;
 
    //针对所有形状
    vector<Shape*> shapeVector;
 
public:
    MainForm(){
        //...
    }
protected:
 
    virtual void OnMouseDown(const MouseEventArgs& e);
    virtual void OnMouseUp(const MouseEventArgs& e);
    virtual void OnPaint(const PaintEventArgs& e);
};
 
 
void MainForm::OnMouseDown(const MouseEventArgs& e){
    p1.x = e.X;
    p1.y = e.Y;
 
    //...
    Form::OnMouseDown(e);
}
 
void MainForm::OnMouseUp(const MouseEventArgs& e){
    p2.x = e.X;
    p2.y = e.Y;
 
    if (rdoLine.Checked){
        shapeVector.push_back(new Line(p1,p2));
    }
    else if (rdoRect.Checked){
        int width = abs(p2.x - p1.x);
        int height = abs(p2.y - p1.y);
        shapeVector.push_back(new Rect(p1, width, height));
    }
    else if (...){
        //...
        shapeVector.push_back(circle);
    }
 
    //...
    this->Refresh();
 
    Form::OnMouseUp(e);
}
 
void MainForm::OnPaint(const PaintEventArgs& e){
 
    //针对所有形状
    for (int i = 0; i < shapeVector.size(); i++){
 
        shapeVector[i]->Draw(e.Graphics); //多态调用,各负其责
    }
 
    //...
    Form::OnPaint(e);
}


如何修改来画圆?

class Shape{
public:
    virtual void Draw(const Graphics& g)=0;
    virtual ~Shape() { }
};
 
 
class Point{
public:
    int x;
    int y;
};
 
class Line: public Shape{
public:
    Point start;
    Point end;
 
    Line(const Point& start, const Point& end){
        this->start = start;
        this->end = end;
    }
 
    //实现自己的Draw,负责画自己
    virtual void Draw(const Graphics& g){
        g.DrawLine(Pens.Red, 
            start.x, start.y,end.x, end.y);
    }
 
};
 
class Rect: public Shape{
public:
    Point leftUp;
    int width;
    int height;
 
    Rect(const Point& leftUp, int width, int height){
        this->leftUp = leftUp;
        this->width = width;
        this->height = height;
    }
 
    //实现自己的Draw,负责画自己
    virtual void Draw(const Graphics& g){
        g.DrawRectangle(Pens.Red,
            leftUp,width,height);
    }
 
};
 
//增加
class Circle : public Shape{
public:
    //实现自己的Draw,负责画自己
    virtual void Draw(const Graphics& g){
        g.DrawCircle(Pens.Red,
            ...);
    }
 
};
 
class MainForm : public Form {
private:
    Point p1;
    Point p2;
 
    //针对所有形状
    vector<Shape*> shapeVector;
 
public:
    MainForm(){
        //...
    }
protected:
 
    virtual void OnMouseDown(const MouseEventArgs& e);
    virtual void OnMouseUp(const MouseEventArgs& e);
    virtual void OnPaint(const PaintEventArgs& e);
};
 
 
void MainForm::OnMouseDown(const MouseEventArgs& e){
    p1.x = e.X;
    p1.y = e.Y;
 
    //...
    Form::OnMouseDown(e);
}
 
void MainForm::OnMouseUp(const MouseEventArgs& e){
    p2.x = e.X;
    p2.y = e.Y;
 
    if (rdoLine.Checked){
        shapeVector.push_back(new Line(p1,p2));
    }
    else if (rdoRect.Checked){
        int width = abs(p2.x - p1.x);
        int height = abs(p2.y - p1.y);
        shapeVector.push_back(new Rect(p1, width, height));
    }
    //改变
    else if (...){
        //...
        shapeVector.push_back(circle);
    }
 
    //...
    this->Refresh();
 
    Form::OnMouseUp(e);
}
 
void MainForm::OnPaint(const PaintEventArgs& e){
 
    //针对所有形状
    for (int i = 0; i < shapeVector.size(); i++){
 
        shapeVector[i]->Draw(e.Graphics); //多态调用,各负其责
    }
 
    //...
    Form::OnPaint(e);
}


第一种如果要变更,需要在所有涉及的地方修改。
第二种如果要变更,如果使用工厂模式,只需要增加一个子类。重用性得到了很高的提升。

 

软件设计的目标

什么是好的软件设计?软件设计的金科玉律——复用!

 

2. 面向对象设计原则

面向对象设计,为什么?

变化是复用的天敌!
面向对象设计最大的优势在于——抵御变化

重新认识面向对象

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

八种设计原则:

依赖倒置原则(DIP)

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

开放封闭原则(OCP)

  • 对扩展开放,对更改封闭。(改变的代价很高,需要重新编译)
  • 类模块应该是可扩展的,但是不可修改。

单一职责原则(SRP)

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

Liskov替换原则(LSP)

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

接口隔离原则(ISP)

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

优先使用对象组合,而不是类继承

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

封装变化点

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

针对接口编程,而不是针对实现编程

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

面向接口设计

产业强盛的标志——接口标准化!分工协作,通过分工实现复用性。

将设计原则提升为设计经验

  1. 设计习语 Design Idioms
    Design Idioms 描述与特定编程语言相关的低层模式,技巧,惯用法。
  2. 设计模式 Design Patterns
    Design Patterns 主要描述的是“类与相互通信对象之间的组织关系,包括它们的角色、职责、协作方式等方面。
  3. 架构模式 Architectural Patterns
    Architectural Patterns 描述系统中与基本结构组织关系密切的高层模式,包括子系统划分,职责,以及如何组织它们之间关系的规则。

3. 模板方法 Template Method

GOF-23 模式分类

从目的来看:

  • 创建型(Creational)模式:将对象的部分创建工作延迟到子类或者其他对象,从而应对需求变化为对象创建时具体类型实现引来的冲击。
  • 结构型(Structural)模式:通过类继承或者对象组合获得更灵活的结构,从而应对需求变化为对象的结构带来的冲击。
  • 行为型(Behavioral)模式:通过类继承或者对象组合来划分类与对象间的职责,从而应对需求变化为多个交互的对象带来的冲击。

从范围来看:

  • 类模式处理类与子类的静态关系。(继承)
  • 对象模式处理对象间的动态关系。 (组合)

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

组件协作:

  • Template Method
  • Strategy
  • Observer/Event

单一职责:

  • Decorator
  • Bridge

对象创建:

  • Factory Method
  • Abstract Factory
  • Prototype
  • Builder

对象性能:

  • Singleton
  • Flyweight

接口隔离:

  • Facade
  • Proxy
  • Mediator
  • Adapter

状态变化:

  • Memento
  • State

数据结构:

  • Composite
  • Iterator
  • Chain of Responsibility

行为变化:

  • Command
  • Visitor

领域问题:

  • Interpreter

这里的分类只是说某一类模式在某一方面体现的更加明显。

重构获得模式 Refactoring to Patterns

  • 面向对象设计模式是“好的面向对象设计”,所谓“好的面向对象设计”指的是那些可以满足“应对变化,提高复用”的设计。
  • 现代软件设计的特征是“需求的频繁变化”。设计模式的要点是“寻找变化点,然后在变化点处应用设计模式,从而来更好地应对需求的变化”。“什么时候、什么地点应用设计模式”比“理解设计模式结构本身”更为重要。
  • 设计模式的应用不宜先入为主,一上来就使用设计模式是对设计模式的最大误用。没有一步到位的设计模式。敏捷软件开发实践提倡的“Refactoring to Patterns”是目前普遍公认的最好的使用设计模式的方法。

推荐图书

重构——改善既有代码的设计
Refactoring to Patterns 重构与模式

重构关键技法

  • 静态 → 动态
  • 早绑定 → 晚绑定
  • 继承 → 组合
  • 编译时依赖 → 运行时依赖
  • 紧耦合 → 松耦合

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值