视频参照https://www.bilibili.com/video/BV1V5411w7qg?p=1
资料https://github.com/19PDP/Bilibili-plus/tree/master/C%2B%2B-DesignPattern
一.概述
每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次一次的使用该方案而不必做重复劳动。
《设计模式:可复用面向对象软件的基础》
该书有四人编写 gang of four——GOF
软件设计复杂的原因:变化
- 客户需求的变化
- 技术平台的变化
- 开发团队的变化
- 市场环境的变化
如何解决变化的复杂性?
- 分解:分而治之(不易复用)
- 抽象:由于不能掌握全部的复杂对象,我们会选择忽视它的非本质细节,而去处理泛化和理想化了的对象
软件设计的目标:拥有极高的复用性
图形编辑器伪代码:
//分解 当需要添加圆形时改动多
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);
}
//抽象 添加圆形改动小
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.Liskov替换原则LSP
- 子类必须能够替换它们的基类(IS-A)
- 继承表达类型抽象
5.接口隔离原则ISP
- 接口应该小而完备
- 不应该强迫客户程序依赖它们不用的方法
6.优先使用对象组合而不是类继承
- 类继承通常为“白箱复用” 对象组合通常为“黑箱复用”(对象组合class B里面放一个class A对象)
- 继承在某种程度上破坏了封装性,子类父类耦合度高
- 对象组合原则只要求被组合的对象具有良好定义的外部接口,耦合度低
7.封装变化点
- 使用封装来创建对象之间的分解层,让设计者可以在分层的一侧进行修改,而不会对另一侧产生不良影响,从而实现层次间的松耦合
8.针对接口编程,而不是针对实现编程
- 不将变量类型声明为某个特定的具体类,而是声明为某个抽象接口
- 客户程序无需获知对象的具体类型,只需要知道对象所具有的接口
- 减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案
//具体的类
vector<Line> lineVector;
vector<Rect> rectVector;
vector<Circle> circleVector;
//抽象接口
vector<Shape *> shapeVector;
产业强盛的标志:接口标准化
秦统一六国:出土的兵器不论出土地点都有统一的标准
毕升活字印刷:按需取字
设计模式解决变化中的复用性问题
三.分类与技巧
模式分类:
- 从目的来看:
- 创建型:解决对象的创建工作
- 结构型:应对需求变化对对象结构带来的冲击
- 行为型:应对需求变化对多个类的交互的冲击
- 从范围来看:
- 类模式:处理类与子类的静态关系——继承
- 对象模式:处理对象间的动态关系——对象组合
重构获得模式refactoring to patterns
- 好的设计 应对变化提高复用
- 需求的频繁变化 寻找变化点,在变化点处应用设计模式 什么时候、什么地点应用设计模式比设计模式本身更重要 代码的某些部分是稳定的,某些部分是变化的,在变化点使用设计模式
- 没有一步到位的设计模式 refactoring to patterns是目前普遍认为最好的设计模式使用方法(先不使用设计模式,看代码间的关系,有什么缺点然后再提出使用设计模式 一步一步来)
重构关键技巧:
- 静态->动态
- 早绑定->晚绑定
- 继承->组合
- 编译时依赖->运行时依赖
- 紧耦合->松耦合