【学习笔记】C++设计模式

mac2022-06-30  23

本博文由 youngpan1101 出品,转载请注明出处。  文章链接:https://blog.csdn.net/youngpan1101/article/details/101942048 作者: 宋洋鹏(youngpan1101)  邮箱: yangpeng_song@163.com

ps:该笔记是基于《C++设计模式 [李建忠]》的学习笔记,视频链接 【Link】,源码和PPT链接 【Link】


 设计模式的定义

每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次又一次地使用该方案而不必做重复劳动。  (by Christopher Alexander)

 GOF 设计模式

历史性著作《设计模式:可复用面向对象软件的基础》,其描述了23种经典面向对象设计模式,创立了模式在软件设计中的地位。

 深入理解面向对象

向下:底层思维,如何把握机器底层从微观理解对象构造,深入理解以下三大面向对象机制:

封装:隐藏内部实现 继承:复用现有代码 多态:改写对象行为 

向上:抽象思维,如何将我们的周围世界抽象为程序代码,深刻把握面向对象机制所带来的抽象意义,理解如何使用这些机制来表达现实世界,掌握什么是“好的面向对象设计”。

软件设计复杂的根本原因

客户需求的变化技术平台的变化开发团队的变化市场环境的变化

如何解决复杂性

分解:将大问题分解为多个小问题,将复杂问题分解为多个简单问题;抽象:由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。

结构化&面向对象的DEMO 

主要是以画图为例来进行DEMO说明,以下代码是伪码:

Shape1.h:

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

 MainForm1.cpp:

C++ Virtual 完美诠释   【Link】

 

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

另外一种是“抽象”的demo,我们构建了shape的父类,

Shape2.h:

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

MainForm2.cpp:

C++中栈和堆的区别及区分对象存储在堆或栈中的方法   【Link】

class MainForm : public Form { private: Point p1; Point p2; //针对所有形状 vector<Shape*> shapeVector; //应对所有的子类,用的是指针(考虑到多态性),存储的可能是Line类或Rect类 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)); //new的对象,是堆对象,析构需要释放该对象 } 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); }

若我们假设是由客户需求的变化,不仅要求实现Line,Rectangle,还要实现圆或者其他多边形,那第二种抽象的方式就更为方便地修改代码了(或者说修改的地方比第一种要少),复用性很高,在大型的工程里将大大减少工作量。

以上的例子说明了我们软件设计的目标:复用!!!

最新回复(0)