文章目录

  • ​​1.面向对象设计目标​​
  • ​​2.面向对象设计原则​​
  • ​​3.将设计原则提升为设计经验​​
  • ​​4.分解的设计思想例子​​
  • ​​5.抽象的设计思想例子​​
  • ​​6.好的软件设计的目标:复用​​

1.面向对象设计目标

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

理解隔离变化

  • 从宏观层面来看,面向对象的构建方式更能适应软件的变化,能将变化所带来的影响减为最小。

各司其职

  • 从微观层面来看,面向对象的方式更强调各个类“责任”。
  • 由于需求变化导致的i性能增类型不应该影响原来类型的实现----是所谓各负其责。

对象是什么?

  • 从语言实现层面来看,对象封装了代码和数据。
  • 从规格层面讲,对象是一系列可被使用的公共接口。
  • 从概念层面讲,对象是某种拥有责任的抽象。

2.面向对象设计原则

依赖倒置原则(DIP)

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

开放封闭原则(OCP)

  • 对扩展开放,对更改封闭
  • 类模块应该是可扩展的,但是不可修改

单一职责原则(SPR)

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

Liskov替换原则(LSP)

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

接口隔离原则(ISP)

  • 不应该强迫客户程序依赖他们不用的方法
  • 接口应该继承而完备

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

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

封装变化点

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

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

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

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

设计习语Design Idioms

  • Design Idioms描述与特定编程语言相关的低层模式,技巧,惯用法

设计模式Design Patterns

  • Design Patterns主要描述的是“类与相互通信的对象之间的组织关系,包括他们的角色、职责、协作方式等方面

架构模式Architectural Patterns

  • Architural Patterns描述系统中与基本结构组织关系密切的高层模式,包括子系统划分,职责,以及如何组织他们之间关系的规则

4.分解的设计思想例子

//Shape.h
class Shape{
public:
int x;
int y;
};

class Line{
public:
Point start;
Point end;

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

};
class Rect{
public:
Point leftUp;
int width;
int height;

Rect(leftUp const& leftUp, int width, int height)
{
this->leftUp = leftUp;
this->width = width;
this->height=heightl
}
};

//MainForm.cc
class MainForm : public Form{
private:
Point p1;
Point p2;

vector<Line> lineVector;
vector<Rect> rectVector;
public:
MainForm(){
...
}
protected:
//鼠标操作
virtual void OnMouseDown(MouseEventArgs const& e);
virtual void OnMouseUp(MouseEventArgs const& e);
virtual void OnPaint(MouseEventArgs const& e);
};

virtual void MainForm::OnMouseDown(MouseEventArgs const& e)
{
p1.x = e.x;
p1.y = e.y;
...
Form::onMouseDown(e);
}
virtual void MainForm::OnMouseUp(MouseEventArgs const& e)
{
p2.x = e.x;
p2.y = e.y;
if (rdoLinux.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();//调用OnPaint
Form::onMouseDown(e);
}

virtual void MainForm::OnPaint(MouseEventArgs const& 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].start.y);
}

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

5.抽象的设计思想例子

//Shape.h
class Shape{
public:
virtual void Draw(Graphics const& g)=0;
virtual ~Shape(){}
};

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


class Line : public Shape{
public:
Point start;
Point end;

Line(Point const& start, Point const& end)
{
this->start = start;
this->end = end;
}
//实现自己的Draw,负责画自己
virtual void Draw(Graphics const& g)
{
g.DrawLine(Pens.Red, start.x, start.y, end.x, end.y);
}
};

class Rect : public Shape{
public:
Point leftUp;
Point width;
Point height;

Rect(Point const& leftUp, int width, int height)
{
this->leftUp = leftUp;
this->width = width;
this->height = height;
}
//实现自己的Draw,负责画自己
virtual void Draw(Graphics const& g)
{
g.DrawRectangle(Pens.Red, leftUp, width, height);
}
};

//MainForm.cc
class MainForm : public Form{
private:
Point p1;
Point p2;

//针对所有形状
vector<Shape*> shapeVector;
public:
MainForm(){}
protected:
virtual void OnMouseDown(MouseEventArgs const& e);
virtual void OnMouseUp(MouseEventArgs const& e);
virtual void OnPaint(MouseEventArgs const& e);
};

virtual void MainForm::OnMouseDown(MouseEventArgs const& e)
{
p1.x = e.x;
p1.y = e.y;
...
Form::onMouseDown(e);
}

virtual void MainForm::OnMouseUp(MouseEventArgs const& e)
{
p2.x = e.x;
p2.y = e.y;
if (rdoLinux.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();//调用OnPaint
Form::onMouseDown(e);
}

virtual void MainForm::OnPaint(MouseEventArgs const& e)
{

for(int i =0;i<lineVector.size(); ++i)
{
shapeVector[i]->Draw(e.Graphics);//多态调用,各负其责
}
///
Form::OnPaint(e);
}

6.好的软件设计的目标:复用

如何解决复杂性?
分解

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

抽象

  • 更高层次来讲,人们处理复杂性有一个通用的技术,即抽象;
    由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。
  • 参考:【录播】聊一聊C++设计模式、函数式编程等(持续更新中),C++设计模式入门,网易C++设计模式笔记(二)面向设计对象的原则