C++ 设计模式 面向对象设计原则

C++ 设计模式 面向对象设计原则

软件设计的金科玉律:复用

面对对象设计最大额优势在于:抵御变化

 

因为"变化"的存在,为软件设计带来了复杂性,那么如何解决?

  1. 分解:分而治之,大问题分解为小问题
  2. 抽象:由于不能掌握全部的复杂对象,选择忽视它的非本质细节,而去处理泛化和理想化的对象模型。

下面以画图为目的(图形工具),进行分解和抽象两种解决方案的演示:

按照C++的编程规范,不同职责的类应该处于不同的文件中,而字段(成员变量)一般都是private类型,此处为了更方便的展示思想,会忽略部分编程规范。

分解版本:

class Point

class Point{
public:
    int x;
    int y;
};

class Line

class Line{
public:
    Point start;
    Point end;

    Line(const Point & start,const Point & end){
        this->start = start;
        this->end = end;
    }
};

class Rect

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 MainFrom

class MianFrom : public Form{
private:
    Point p1;
    Point p2;

    vector<Line> lineVector;
    vector<Rect> rectVector;
public:
    MainForm(){
        //.........
    }
private:
    //具体的鼠标操作
    virtual void OnMouseDown(const MouseEventArgs & e);//鼠标摁下
    virtual void OnMouseUp(const MouseEventArgs & e);//鼠标抬起
    virtual void OnPaint(const MouseEventArgs & e);
};

具体实现:

void MainFrom::OnMouseDown(const MouseEventArgs & e){
    p1.x = e.X;
    p1.y = e.Y;
    //.......
    Form::OnMouseDown(e);
}
void MainFrom::OnMouseUp(const MouseEventArgs & e){
    p2.x = e.X;
    p2.y = e.Y;
    //.......
    if(rdoLine.Checked){
        //如果是划线,那么就把线放到vector中
        Line line(p1,p2);
        lineVector.push_back(line);
    }
    else if(rdoRect.Checked){
        //如果是矩形,那么就把各个点坐标放到vector中
        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::OnMouseDown(e);
}
void MainFrom::OnPaint(const MouseEventArgs & 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.x,);
    }

    //针对矩形
    for(int i = 0;i < lineVector.size();i++){
        e.Graphics.DrawRectangle(Pens.Red,
                            rectVector[i].LeftUp,
                            rectVector[i].width,
                            rectVector[i].height);
    }
    //.......
    Form::OnMouseDown(e);

}

可以看到,在MainFrom中,具体的绘图执行是在OnPaint中执行的,但是目前只是画质矩形和直线,如果形状很多的情况下,显然会出现责任冗余的情况

那么我们更换一种方式:利用抽象来解决这个问题:

抽象版本:

多做一个shape类(抽象类)

class shape{
public:
    shape();
    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;
    }
    //各自形状负责话各自的形状
    virtual void Draw(const Graphics& g){
        g.Graphics.DrawLine(Pens.Red,
                            start.x,start.y,end.x,end.x,);
    }
};
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;
    }
    //各自形状负责话各自的形状
    virtual void Draw(const Graphics& g){
        g.Graphics.DrawRectangle(Pens.Red,
                            LeftUp,width,height);
    }
};

class MainFrom 区别于之前分解版本的Class MainFrom ,vector减少至一个,增加了析构函数中的内容

class MainFrom : public Form{
private:
    Point p1;
    Point p2;

    vector<shape *> shapeVector;//此处进行更改
public:
    MainForm(){
        //.........
    }
    ~MainFrom(){
        for(int i = 0;i<shapeVector.size();++i){
            delete shapeVector[i];
        }
    }
private:
    //具体的鼠标操作
    virtual void OnMouseDown(const MouseEventArgs & e);//鼠标摁下
    virtual void OnMouseUp(const MouseEventArgs & e);//鼠标抬起
    virtual void OnPaint(const MouseEventArgs & e);
};

 具体执行过程

void MainFrom::OnMouseDown(const MouseEventArgs & e){
    p1.x = e.X;
    p1.y = e.Y;
    //.......
    Form::OnMouseDown(e);
}
void MainFrom::OnMouseUp(const MouseEventArgs & e){
    p2.x = e.X;
    p2.y = e.Y;
    //.......
    if(rdoLine.Checked){
        //如果是划线,那么就把线放到vector中
        shapeVector.push_back(new Line(p1,p2));
    }
    else if(rdoRect.Checked){
        //如果是矩形,那么就把各个点坐标放到vector中
        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::OnMouseDown(e);
}
void MainFrom::OnPaint(const MouseEventArgs & e){
    for(int i = 0;i < shapeVector.size();i++){
        shapeVector[i]->Draw(e.Graphics);//多态调用,各司其职
    }
    //.......
    Form::OnMouseDown(e);
}

下面对比核心程序段:

如果单纯的看代码,显然差不多,但是从可扩展性的角度出发,现在不但要画矩形和直线,还要画圆,菱形等等

哪儿种方法的可扩展性更好呢?哪儿个代码的修改成本和测试成本更低呢?,高下立判。

这也是C++多态的核心部分,改变对象的行为。

如果增加需求,那么“抽象”情况下,我们中需要增加一个子类(父类为shape)即可,但是在分解的情况下,增加的可就不止一个地方了,修改的地方需要,开发和测试的成本也就越高。

 

根据上面的例子,我们可以重新认识一下面向对象:

1:理解隔离变化

从宏观层面来看,面向对象的构建方式更能适应软件的变化,能将变化所带来的影响减为最小。class shape就是最好的例子,充分地证明了抽象类能够隔离变化的功能

2:各司其职

从微观层面来看,面向对象的方式更强调各个类的“责任”

由于需求变化导致的新增类型不应该影响原来类型的实现——所谓各负其责

分解的责任是在OnPaint(MainFrom),而在抽象中,责任是在各个子类中进行实现的。

3:对象是什么

从语言实现层面来看,对象封装了代码和数据

从规格层面来看,对象是一系列可被使用的公共接口

从概念层面来看,对象是某种拥有责任的抽象

 

面向对象的设计原则:

1,依赖倒置原则(DIP)

高层模型(稳定)不应该依赖于底层模式(变化),二者都应该依赖于抽象(稳定

抽象(稳定)不应该依赖于实现细节(变化),实现细节应该依赖于抽象(稳定

例子中的shape就是抽象,隔离了变化(隔离了MainFrom和Line和Rect)

2,开放封闭原则(OCP)

对扩展开放,对更改封闭

类模块应该是可扩展的,但是不可修改

例子中,如果存在shape抽象类,那么扩展直接继承shape即可,不需要更改其他部分,反观分解情况,修改的部分很多

3,单一责任原则(SRP)

一个类应该仅有一个引起它变化的原因

4,LisKov替换原则(LSP)

子类必须能够替换他们的父类(IS-A)

继承表达类型抽象

5,接口隔离原则(ISP)

有必要在Public,接口小而完备

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

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

7,封装变化点

使用封装来创建对象之间的分界层,使设计者可以在分界的一侧进行修改而不是干涉另一侧,从而形成一种松耦合。

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

只给客户接口,而不需要让客户知道具体的数据类型

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值