设计模式概述
设计目标
- 理解软件松耦合思想
- 掌握面向对象的设计原则
- 掌握重构技法改善设计
- 掌握GOF设计模式
什么是设计模式?
每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心,这样,你就可以一次又一次地使用该方案而不必重复劳动
--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++设计模式-李建忠