设计模式与面向对象设计原则

  使用设计模式是为了可重用代码,让代码更容易被他人理解,保证代码可靠性,设计模式使代码编制真正工程化。

一、分解与抽象

1、分解-分而治之

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

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

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

2、抽象-面向对象

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

// 定义一个抽象基类
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);
}

二、设计模式基本原则

1、依赖倒置原则(DIP)

  高层模块(稳定)不应该依赖于低层模块(变化),二者都应该依赖于抽象(稳定)抽象(稳定)不应该依赖于实现细节(变化),实现细节应该依赖于抽象(稳定)
在这里插入图片描述

2、开放封闭原则(OCP)

  对扩展开放,对更改封闭类模块应该是可拓展的,但是不可修改类的改动是通过增加代码进行的,而不是修改源代码
在这里插入图片描述

3、单一职责原则(SRP)

  • 类的职责要单一,对外只提供一种功能,而引起类变化的原因只有一个

  • 一个类变化的方向隐含着类的责任

  • 如果一个类承担的职责过多,就等于把这些职责耦合在一起了。一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力。这种耦合会导致脆弱的设计,当发生变化时,设计会遭受到意想不到的破坏。而如果想要避免这种现象的发生,就要尽可能的遵守单一职责原则。此原则的核心就是解耦和增强内聚性

4、里氏替换原则(LSP)

  • 任何抽象类出现的地方,都可以用他的实现类进行替换(多态)。实际就是虚拟机制,语言级别实现面向对象功能。

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

  • 子类可以扩展父类的功能,但不能改变父类原有的功能

5、接口隔离原则(ISP)

  • 不应该强迫客户程序依赖它们不用的方法

  • 接口应该小而完备

  • 一个接口应该只提供一种对外功能,不应该把所有操作都封装到一个接口中去。

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

  • 类继承通常为“白箱复用”,对象组合通常为“黑箱复用” 。

  • 继承在某种程度上破坏了封装性,子类父类耦合度高

  • 而对象组合则只要求被组合的对象具有良好定义的接口,耦合度低

7、封装变化点

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

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

  • 不将变量类型声明为某个特定的具体类,而是声明为某个接口

  • 客户程序无需获知对象的具体类型,只需要知道对象所具有的接口

  • 减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值