标签:隐藏 virt 对象 font fresh 转换 highlight 面向 info
1.理解松耦合设计思想
2.掌握面向对象设计原则
3.掌握重构技法改善设计
4.掌握GOF核心设计模式
1.底层思维:向下,如何掌握机器底层,从微观理解对象构造
→封装,隐藏内部实现
→继承,复用现有代码
→多态,改写对象行为
2.抽象思维:向上,如何将周围世界抽象为程序代码
变化!!!!
。。。。
1.分解:人们面对复杂性有一个常见的做法:即分而治之,将大问题分解成多个小问题,将复杂问题分解为多个简单问题。(面向过程思想)
2.更高层次来讲,人们处理复杂性有一个共同技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽略它的非本质,从而处理泛华和理想化了的对象模型。(现象对象思想)
面向过程实现:
Shape.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& letfUp, int width, int height){ this->LeftUp = letfUp; this->width = width; this->height = height; } };
MaintForm.cpp
class MainForm :publuc Form { privat: Point p1; Point p2; vector<line> lineVector; vevtor<Rect> rectVector; public: MainForm() { //... } protected: virtual void OnMouseDown(const MouseEventArg& e); virtual void OnMouseUp(const MouseEventArg& e); virtual void OnPaint(const MouseEventArg& e); }; void MainForm::OnMouseDown(const MouseEventArg& e) { p1.x = e.X; p1.y = e.Y; Form::OnMouseDown(e); } void MainForm::OnMouseUp(const MouseEventArg& e) { p2.x = e.X; p2.y = e.Y; if (rdoLine.Checled) { Line line(p1, p2); lineVector.push_back(line); } else if { int width = abs(p2.x - p1.x); int height = abs(p2.y - p1.y); Rect rect(p1, width, height); } //... this->Refresh(); Form::OnMouseUp(e); } void MainForm::Onpaint(const PaintEventArg& e) { //针对直线 for (int i = 0; i < lineVector.size(); i++) { e.Graphice.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.Graphice.DrawLine(Pens.Red, rectVector[i].leftup; rectVector[i].width, rectVector[i].height); } //... Form::OnPaint(e); }
面向对象实现方式
Shape.h
class Shape { public: virtual void Draw(const Graphics& a) = 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: Point LeftUp; int width; int height; Rect(const Point& letfUp, int width, int height) { this->LeftUp = letfUp; this->width = width; this->height = height; } //实现自己的Draw,负责画自己 virtual void Draw(const Graphics& g) { g.DrawLine(Pens.Red leftUp, width,height); } };
MainForm.cpp
class MainForm :publuc Form { privat: Point p1; Point p2; //针对所有形状 vector<Shape*> shapeVector; public: MainForm() { //... } protected: virtual void OnMouseDown(const MouseEventArg& e); virtual void OnMouseUp(const MouseEventArg& e); virtual void OnPaint(const MouseEventArg& e); }; void MainForm::OnMouseDown(const MouseEventArg& e) { p1.x = e.X; p1.y = e.Y; Form::OnMouseDown(e); } void MainForm::OnMouseUp(const MouseEventArg& e) { p2.x = e.X; p2.y = e.Y; if (rdoLine.Checled) { shapeVector.push_back(new line(p1, p2)); } else if { int width = abs(p2.x - p1.x); int height = abs(p2.y - p1.y); shapeVector.push_back(new rect(p1, width, height)); } //... this->Refresh(); Form::OnMouseUp(e); } void MainForm::Onpaint(const PaintEventArg& e) { //针对所有形状 for (int i = 0; i < shapeVector.size(); i++) { shapeVector[i]->Draw(e.Graphics); //多态调用各负其责 } //... Form::OnPaint(e); }
当需求变化时:
比如需要加画圆的需求。面向对象和面向过程的修改方式自己脑补。。。。
复用!
标签:隐藏 virt 对象 font fresh 转换 highlight 面向 info
原文地址:https://www.cnblogs.com/malloc1free/p/11087290.html