设计模式---了解

一:课程目标

1.理解松耦合思想

2.掌握面向对象设计原则

3.掌握重构技法改善技术

4.掌握GOF核心设计模式

二:什么是设计模式

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

模式:在一定环境下,用固定的套路解决问题

在一定环境下解决某一个问题的方案,包括三个基本元素:问题,解决方案,环境

设计模式Design pattern:

是一套呗反复使用,多数人知晓的,经过分类编目的,代码设计经验的总结。
使用设计模式是为了可重用代码,让代码更容易被他人理解,保证代码可靠性。
设计模式使代码编制真正工程化

设计模式的使用时机:

设计模式的选取往往是我们经过长时间测试,对项目进行分析,之后决定使用哪个设计模式适用,而不是我们一开始就拿取一个设计模式套入。
我们会用代码容易,难的是知道在什么时机使用设计模式,这是需要长时间的接触,慢慢了解的,所以不要滥用设计模式

三:GOF(Gang of Four)设计模式

历史性著作《设计模式:可复用面向对象软件的基础》一书中描述了23种经典面向对象设计,创立了模式在软件设计中的地位。
注意:由于《设计模式》一书确定了设计模式的地位,通常所说的设计模式隐含地表示为”面向对象设计模式“。但这并不意味”设计模式“就等于”面向对象设计模式“。
我们更加应该注意的是可复用,和面向对象,而不是设计模式。
可复用:设计模式的目标
面向对象:手法,具体方法  

 四:浅谈面向对象

我们要注重两方面:底层思维和抽象思维

(一)底层思维:向下,把握机器底层,从微观理解对象构造

语言构造
编译转换
内存模型
运行时机制

(二)抽象思维: 向上,如何将我们地周围世界抽象为程序代码

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

 五:深入理解面向对象

(一) 向下理解面向对象三大机制(后面会详细回顾)

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

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

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

这里面包括
客户需求的变化、
技术平台的变化、
开发团队的变化、
市场环境的变化
...

七:如何解决复杂性

(一)分解:分而治之

人们面对复杂性有一个常见的做法:即分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。其思想我们在数据结构中有提及
分解主要用于结构化设计思维:像C语言之类,当然我们在C++面向对象中也可以使用,但是我们尽可能使用抽象思想

(二)抽象:面向对象

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


分解和抽象体现在软件设计上分别是结构化设计思维和面向对象设计思维。

八:下面是分解和抽象在软件设计上的比较示例

(Note:这里为了方便使用的是伪码;字段在C++中指的是一个成员,它表示与对象或类关联的变量。):

设计简单的画图工具。需要设计的形状放在shape里面;界面设计在Mainform。假设画图程序一开始只能实现画线和矩形,现在我们要增加画圆的功能,对于结构化设计的代码版本和对于面向对象的代码版本分别该怎么修改?下面的代码用注释// 增加和注释//改变的方式来表示所做的改变,注意体会增加和改变的区别。增加表示的是拓展,而改变是变化。

(一)分解:结构化设计思维

原工具类 :

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 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);
}

 分而治之,不易复用,每添加一个新的功能都要进行相关的功能添加 。

分解法(各种各样的类型;if else if else if;总结:针对...怎么做;针对... 怎么做):
①增加circle类;②再写一个 vector<Circle> circleVector; ③写else if( rdoCir.Check); ④针对圆形 MainForm::OnPaint

(二)抽象法

原工具类

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);
    }

};

注意:虚析构函数是有必要的,在c++中,基类中都要写虚构函数,而且写成虚析构函数,因为我们在多态调用时,会对父类虚构,要去虚构子类数据的,我们只有写成虚析构函数,才可以去调用子类进行析构

原框架类

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));
    }//...
    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);
}
①写一个circle类让其去继承Shape类(抽象类),里面实现自己的draw;②elseif(工厂模式则不需要)。
很明显地,抽象法的重用性得到了很高地提升。

总结:那么软件设计的目标应该是什么?什么是好的软件设计?

软件设计的金科玉律是:复用!
原文地址:https://www.cnblogs.com/ssyfj/p/9521110.html