C++设计模式笔记(01)-设计模式的介绍

时间:2022-07-22
本文章向大家介绍C++设计模式笔记(01)-设计模式的介绍,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

参考书籍:《设计模式:可复用面向对象软件的基础》

参考课程:《C++设计模式》-李建忠

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

1.从面向对象谈起:

▷底层思维:向下,如何把握机器底层从微观理解对象构造

  • 语言转换
  • 编译转换
  • 内存模型
  • 运行机制

▷抽象思维:向上,如何将我们周围的世界抽象为代码

  • 面向对象
  • 组件封装
  • 设计模式
  • 架构模式

良好的底层思维需要做到:要深入的理解三大面向对象机制:封装、继承、多态。这是面向对象的三大特点。三者对应的实质是《封装,隐藏内部实现》、《继承,复用现有代码》、《多态,改写对象行为》。

良好的抽象思维需要做到:深刻把握 面向对象机制 带来的抽象意义,明白如何使用这些机制来表达现实世界,掌握什么是好的面向对象设计。

2.深入理解面向对象

向下:理解三大面向对象机制

  • 封装---隐藏内部实现
  • 继承---复用现有代码
  • 多态---改写对象行为

向上:深刻把握面对像机制所带来的抽象意义,理解如何使用这些机制来表达现实世界,掌握什么是“好的面向对象设计”。

3.软件设计复杂的根本原因:

好的面向对象设计很不容易,因为软件设计是极其复杂的,需求时刻在改变,一个软件产品不可能一成不变,所以在前期的设计中,如果一个设计方法不合理,没有良好的扩展性和可维护性,那这个软件工程终究是失败的,后果就是产品不盈利,然后项目组成员没奖金,没工资,娶不到白富美。

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

4.如何解决复杂性

分解

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

♦以绘图为例子通过分解解决问题:

//分解
//伪代码,未遵循cpp标准规范
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);
}
//分解
//伪代码,未遵循cpp标准规范
//形状类
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{


};

抽象

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

♦以绘图为例子通过抽象解决问题:

//抽象
//伪代码,未遵循cpp标准规范
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);
}
//抽象
//伪代码,未遵循cpp标准规范
  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,
            ...);
    }
};

应对变化,最好的办法是抽象化。在现实生活中,对于一般的事都有通用的规律,软件领域也是,通过归纳出通用的规律,抽象化,忽略研究对象的细节,而追究其核心规律,创造一个理想的对象模型。基于这个对象模型来编程,这样就能够更好的应对变化,因为离开具体细节,抽象化,所以能够复用

5.软件设计的目标

什么是好的软件设计?软件设计的金科玉律:         

复用!