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

万网虚拟主机建网站最近时事新闻热点事件

万网虚拟主机建网站,最近时事新闻热点事件,卢松松wordpress,网站开发多少钱什么时设计模式? 每一个模式描述了一个在我们周围不断重复的问题以及该问题的解决方案的核心,这样,就能一次有一次地使用该方案,而不必做重复劳动。 如何解决复杂性? 分解:人们面对复杂性有一个常见的做法…

什么时设计模式?

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

如何解决复杂性?

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

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

比如我们目前有这样的一个绘画类 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.hkea.cn/news/438083/

相关文章:

  • 网站建设的仿站seo顾问收费
  • 珠宝行业做网站的好处株洲seo排名
  • java web开发网站开发cpa推广接单平台
  • 广西南宁网络营销网站网站权重优化
  • 黄山网站设计公司营销网站建设多少钱
  • 网站建设招标评分表湖南关键词优化推荐
  • 淘宝上成都网站建设如何制作视频网站
  • 最吃香的男生十大手艺5g网络优化
  • 河源哪里做网站网络项目怎么推广
  • 网站闭关保护怎么做广州百度seo 网站推广
  • 可以在线做动图的网站近期重大新闻事件
  • 伊犁州建设局网站怎么做微信小程序
  • 做网站需要买主机那新媒体营销方式有几种
  • 网络推广seo公司seo排名的方法
  • 南山做网站多少钱百度资讯
  • 西安哪里有做网站的小学生收集的新闻10条
  • 做游戏网站有几个要素seo网站关键词优化报价
  • 蓬业东莞网站建设技术支持东莞做网站公司首选
  • 网站版式设计获客渠道有哪些
  • 今日军事新闻简短扬州seo优化
  • 国外好看的教育类网站模板下载东莞做网站最好的是哪家
  • 微擎与wordpress快速优化seo软件推广方法
  • 英文网站设计哪家好免费网站搭建
  • 网站建设公司 销量深圳谷歌seo公司
  • 新蔡哪有做网站建设的全球疫情今天最新消息
  • 怎么做平台网站百度seo报价方法
  • 帮人做网站 怎么收费怎么用网络推广
  • 网站排名优化建设百度广告投放技巧
  • 文件服务器网站搭建教程好的竞价托管公司
  • 黑龙江省城乡和住房建设厅网站首页百度链接地址