Design Patterns 1 - 设计模式简介
#B站 李建忠老师的《C++设计模式》课程 GeekBand
第一节 设计模式简介
https://www.bilibili.com/video/BV1kW411P7KS?from=search&seid=7068698946538664421
课程目标:
- 理解松耦合设计思想
- 掌握面向对象设计原则
- 掌握重构技法改善设计
- 掌握GOF核心设计模式
什么是设计模式:
“每一个模式描述了一个在我们周围不断重复发生的问题以及该问题的解决方案的核心。这样,你就能一次又一次地使用该方案而不必做重复的劳动”
——Christopher Alexander
GOF 设计模式:(本课程教材)
历史性著作《设计模式:可复用面向对象软件的基础》——书中描述了23中经典面向对象设计模式,创立了模式在软件设计中的地位。
由于《设计模式》一书确定了设计模式的地位,通常所说的设计模式隐含地表达了“面向对象设计模式”。但这并不意味着“设计模式”就等于“面向对象设计模式”
从面向对象谈起
底层思维 <------------------------------------> 程序员 <-----------------------------> 抽象思维
1, 底层思维:向下,如何把握基础底层,从微观(机器层面)理解对象构造
语言构造
编译转换
内存模型
运行时机制
2, 抽象思维:向上,如何将我们周围世界抽象为程序代码
面向对象
组件封装
设计模式
架构模式
本课程偏重于抽象思维的建立。
深入理解面向对象:
向下:
封装,隐藏内部实现
继承,复用现有代码
多态,改写对象行为
向上:深刻把握面向对象机制所带来的抽象意义,理解如何使用这些机制来表达现实世界,掌握什么是“好的面向对象设计” (后文指出:可复用的设计,是好的面向对象设计)
软件设计固有的复杂性:
建筑商从来不会去想给一栋已建好的100层高的楼房底下再修一个小地下室——这样做花费大而且注定要失败。然而令人惊奇的是,软件系统的用户在要求做出类似改变时却不会仔细考虑,而且他们认为这只是需要简单编程的事。
——Object-Oriented Analysis and Design with Applications, Grady Booch
软件设计负责的根本原因:
变化
客户需求的变化
开发团队的变化
技术平台的变化
市场环境的变化
...
如何解决复杂性:
分解:人们面对复杂性有一个常见的做法:即分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。
抽象:更高层次来讲,人们处理复杂性有一个通用的技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。
不要记代码和技巧,重要的是建立一套思维方式和模型。
关键词:抽象
结构化 VS 面向对象
show me the code (放码过来)
包括前后两个版本:MainForm.cpp Shape.h / MainForm2.cpp Shape2.h
通过添加新的需求Circle来对比两种实现方式的差异,注释有些是自己的感悟。
//Shape.h class Point{ public: //实现细节应该放到private里面 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{ //...点、半径 };
//MainForm.cpp //客户需求变更,增加一个圆形,需要添加、改变 class MainForm : public Form { public: Point p1; Point p2; vector<Line> lineVector; //画线,画矩形 vector<Rect> rectVector; //依赖于细节 //改变,增加一个circle的vector 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 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){ 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); } //改变,增加对circle的处理 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.DrawRectagle(Pens.Red, rectVector[i].leftUp, rectVector[i].width, rectVector[i].height); } //改变 //针对圆形,添加处理 for (int i = 0; i < rectVector.size(); i++){ e.Graphics.DrawCircle(Pens.Red, rectVector[i]); } //... Form::Paint(); }
//Shape2.h class Shape{ public: //抽象,统一处理 virtual void Draw(const Graphics& g) = 0; virtual ~Shape() {} //虚析构函数:子类的虚析构函数可以被调用到 }; class Point{ public: //实现细节应该放到private里面 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, ...); } };
//MainForm2.cpp //考虑添加一个Circle的情况,对比抽象思维和分而治之的区别 //第二种方法,改变很小,重用性得到了提高 //第一种方法,改得很多,这就是加班的原因 class MainForm : public Form { private: Point p1; Point p2; vector<Shape*> shapeVectors; //针对接口编程 //Shape指针:需要多态性,所以使用指针,如果不用指针,会造成对象切割,只有基类的成员 public: MainForm(){ //... } protected: virtual void OnMouseDown(const MouseEventArgs &e); virtual void OnMouseUp(const MouseEventArgs &e); virtual void OnPaint(const PaintEventArgs &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){ shpeVector.push_back(new Line(p1, p2)); } else if (rdoRect.Checked){ int width = abs(p2.x - p1.x); int height= abs(p2.y - p1.y); shpeVector.push_back(new Rect(p1, width, height)); } //改变,增加对circle的处理,如果用工厂设计模式,可以不变 else if(...){ //... circleVector.push_back(circle); } //... this->Refresh(); Form::OnMouseUp(e); } void MainForm::OnPaint(const PaintEventArgs& e){ //针对所有形状,根据实际存储的图形,调用对应的draw, for (int i = 0; i < shapeVectors.size(); i++){ shapeVectors[i].Draw(e.Graphics); //多态调用,各负其责 } //... Form::Paint(); }