赞
踩
每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心,这样,你就可以一次又一次地使用该方案而不必重复劳动
--christopher Alexander
底层思维 | 抽象思维 |
---|---|
向下,如何把握机器底层从微观理解对象构造 | 向上,如何将我们周围的世界抽象为程序代码 |
语言构造 | 面向对象 |
编译转换 | 组件封装 |
内存模型 | 设计模式 |
运行时机制 | 架构模式 |
变化
客户需求变化
软件平台变化
开发团队变化
市场环境变化
…
为说明问题,忽略代码实现细节,伪代码
/********MainForm.cpp*********/ class MainForm : public Form{ public: MainForm(){ //... } private: Point p1; Point p2; vector<Line> lineVector; vector<Rect> rectVector; // ... 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); } //... this->Refresh();//系统会去调用Paint Form::OnMouseUp(e); } virtual void OnPaint(const PaintEventArgs& e){ //对于直线 for(auto line: lineVector){ e.Graphics.DrawLine(Pens.Red, line.start.x, line.start.y, line.end.x, line.end.y,) } //对于矩形 for(auto rect: rectVector){ e.Graphics.DrawLine(Pens.Red, rect.start.x, rect.start.y, rect.end.x, rect.end.y,) } //... Form::OnPaint(e); } ... /********Shape.h************/ class Point{ public: int x; int y; } class Line { public: Line(const Ponit& start, const Point& end){ this->start = start; this->end = end; } ~Line(); private: Point a; Point b; }; class Rect { public: Rect(const Point& leftUp, int width, int height){ this->leftUp = leftUp; this->width = width; this->height = height; } ~Rect(); private: Point leftUp; int width; int height; }; ...
/********MainForm.cpp*********/ class MainForm : public Form{ public: MainForm(){ //... } private: Point p1; Point p2; vector<Shape*> shapeVector;//注意这时Vector里放的是指针,为了保证多态,不要忘记释放内存 // ... 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(new line(p1, p2));//因为Vector里放的是指针所以要new出 } 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(new rect(p1, width, height));//因为Vector里放的是指针所以要new出 } //... this->Refresh();//系统会去调用Paint Form::OnMouseUp(e); } virtual void OnPaint(const PaintEventArgs& e){ //对于直线 for(auto shape: shapeVector){ shape->Draw(e.Graphics);//多态调用,各负其责 } //... Form::OnPaint(e); } ... /********Shape.h************/ class Shape{ public: virtual void Draw(const Graphics& g) = 0;//设为纯虚函数,各形状自己画自己 virtual ~Shape(){};//Shape指针可以调用正确的析构函数,c++多态 } class Line : public Shape { public: Line(const Ponit& start, const Point& end){ this->start = start; this->end = end; } ~Line(); void draw(const Graphics& g){ g.DrawLine(Pens.Red, start.x, start.y, end.x, end.y); } private: Point start; Point end; }; class Rect : public Shape { public: Rect(const Point& leftUp, int width, int height){ this->leftUp = leftUp; this->width = width; this->height = height; } ~Rect(); void draw(const Graphics& g){ g.DrawLine(Pens.Red, leftUp, width, height); } private: Point leftUp; int width; int height; }; ...
当系统需要增加圆形的需求时
/********MainForm.cpp*********/ class MainForm : public Form{ public: MainForm(){ //... } private: Point p1; Point p2; vector<Line> lineVector; vector<Rect> rectVector; //改变的地方 vector<Circle> circleVector; // ... 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(...){ //... } //... this->Refresh();//系统会去调用Paint Form::OnMouseUp(e); } virtual void OnPaint(const PaintEventArgs& e){ //对于直线 for(auto line: lineVector){ e.Graphics.DrawLine(Pens.Red, line.start.x, line.start.y, line.end.x, line.end.y,) } //对于矩形 for(auto rect: rectVector){ e.Graphics.DrawLine(Pens.Red, rect.start.x, rect.start.y, rect.end.x, rect.end.y,) } //改变的地方 for(...){ //... } //... Form::OnPaint(e); } ... /********Shape.h************/ class Point{ public: int x; int y; } class Line { public: Line(const Ponit& start, const Point& end){ this->start = start; this->end = end; } ~Line(); private: Point a; Point b; }; class Rect { public: Rect(const Point& leftUp, int width, int height){ this->leftUp = leftUp; this->width = width; this->height = height; } ~Rect(); private: Point leftUp; int width; int height; }; //改变的地方 class Circle{ //... } ...
/********MainForm.cpp*********/ class MainForm : public Form{ public: MainForm(){ //... } private: Point p1; Point p2; vector<Shape*> shapeVector;//注意这时Vector里放的是指针,为了保证多态,不要忘记释放内存 // ... 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(new line(p1, p2));//因为Vector里放的是指针所以要new出 } 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(new rect(p1, width, height));//因为Vector里放的是指针所以要new出 } //这里做些改变,不过后续利用设计模式可以消除掉 //... this->Refresh();//系统会去调用Paint Form::OnMouseUp(e); } virtual void OnPaint(const PaintEventArgs& e){ //对于直线 for(auto shape: shapeVector){ shape->Draw(e.Graphics);//多态调用,各负其责 } //... Form::OnPaint(e); } ... /********Shape.h************/ class Shape{ public: virtual void Draw(const Graphics& g) = 0;//设为纯虚函数,各形状自己画自己 virtual ~Shape(){};//Shape指针可以调用正确的析构函数,c++多态 } class Line : public Shape { public: Line(const Ponit& start, const Point& end){ this->start = start; this->end = end; } ~Line(); void draw(const Graphics& g){ g.DrawLine(Pens.Red, start.x, start.y, end.x, end.y); } private: Point start; Point end; }; class Rect : public Shape { public: Rect(const Point& leftUp, int width, int height){ this->leftUp = leftUp; this->width = width; this->height = height; } ~Rect(); void draw(const Graphics& g){ g.DrawLine(Pens.Red, leftUp, width, height); } private: Point leftUp; int width; int height; }; //改变的地方 class Circle : public Shape{ //... } ...
此时,抽象相对于分解的方法,对代码的改动要少的多
参考:
C++设计模式-李建忠
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。