当前位置: 首页 > news >正文

理解为什么要有C++设计模式

什么时设计模式?

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

如何解决复杂性?

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

抽象:更高层次来讲,人们处理复杂性有一个通用的技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节而去处理泛化和理想化了的对象模型。

比如我们目前有这样的一个绘画类 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

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

细心阅读上方的代码,我们发现,如果再次添加一种绘制方法,我们需要添加的操作分为了这几步:

  1. 在绘画类里面添加绘画方法;
  2. 在主函数类来进行判断他是什么绘制方法;
  3. 进行绘画。

但是我们通过C++的特性多态来进行操作的话,我们的操作步骤和代码量会明显的减少;如下所示:

绘画类 Shape1.h

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

主函数类 MainForm1.cpp

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

从以上代码来看,明显代码量得到了优化,并且代码得到了复用
设计模式的存在其实就是做到让跟多复杂的代码,得到抽象处理,并得到代码复用。

http://www.lryc.cn/news/474442.html

相关文章:

  • 模式匹配类型
  • 每天10个vue面试题(七)
  • 如何在Linux系统中使用Apache HTTP Server
  • C++基于opencv的视频质量检测--画面冻结检测
  • Day22 opencv图像预处理
  • QT中的定时器与计时器
  • 国内AI大模型学习平台
  • 曹操出行借助 ApsaraMQ for Kafka Serverless 提升效率,成本节省超 20%
  • 深入理解数据库的三范式
  • P11233 [CSP-S 2024] 染色
  • 图传推流学习(敬请期待)
  • 【JavaGuide】十大经典排序算法总结
  • 程序中怎样用最简单方法实现写excel文档
  • 《机器学习与人类学习:比较、融合与未来展望》
  • Mysql 8.4.3LTS 的离线部署
  • h5项目打包上线报错404文件找不到
  • mysql上课总结(5)(MySQL的完整性约束(详细介绍))
  • 复原IP地址
  • Effective C++ 学习笔记二
  • 以「JIMUMETA元宇宙体验馆」为例,探讨有哪些元宇宙场景?
  • RHCE的练习(8)
  • yocto是如何收集recipes,如何加入现有的bb文件
  • [运维] 服务器本地网络可用性检查脚本
  • MYSQL-显示信息关于服务器插件语法(二十五)
  • 【线下培训】龙信受邀参加开封市公安局举办的电子数据取证培训班
  • 软件测试工程师面试整理 —— 编程与自动化!
  • 【鸿蒙新闻】10月29日警用鸿蒙开发者大会在北京胜利召开,开启智慧应用新时代!
  • java.io.IOException: Too many open files
  • ElementUI el-form表单多层数组的校验
  • 常见的向量范数、矩阵范数和对偶范数-对偶范数详细证明过程