| 设计模式概述设计目标理解软件松耦合思想掌握面向对象的设计原则掌握重构技法改善设计掌握GOF设计模式
 什么是设计模式???每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心,这样,你就可以一次又一次地使用该方案而不必重复劳动 --christopher Alexander 面向对象| 底层思维 | 抽象思维 | 
|---|
 | 向下,如何把握机器底层从微观理解对象构造 | 向上,如何将我们周围的世界抽象为程序代码 |  | 语言构造 | 面向对象 |  | 编译转换 | 组件封装 |  | 内存模型 | 设计模式 |  | 运行时机制 | 架构模式 | 
 向下:深入理解三大面向对象机制 
   向上:深刻把握面向对象机制所带来的抽象意义,理解如何把握这些机制来表达现实世界,掌握什么是好的设计模式。
 软件设计复杂的根本原因?变化?客户需求变化
 ?软件平台变化
 ?开发团队变化
 ?市场环境变化
 ?…
 如何解决复杂性?分解:??分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。
抽象:??更高层次来讲,人们处理复杂性有一个通用的技术,即抽象,由于不能掌握全部的复杂对象,我们选择忽略他的分本质细节,而去处理泛化和理想了的模型
 分解和抽象的方法对比为说明问题,忽略代码实现细节,伪代码 
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();
	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);
}
...
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 MainForm : public Form{
public:
	MainForm(){
		
	}
private:
	Point p1;
	Point p2;
	
	vector<Shape*> shapeVector;
    
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));
	}
	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));
	}
	
	
	this->Refresh();
	Form::OnMouseUp(e);
}
virtual void OnPaint(const PaintEventArgs& e){
	
	for(auto shape: shapeVector){
	shape->Draw(e.Graphics);
	}
	
	Form::OnPaint(e);
}
...
class Shape{
public:
	virtual void Draw(const Graphics& g) = 0;
	virtual ~Shape(){};
}
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 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();
	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);
}
...
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{
	
}
...
 
class MainForm : public Form{
public:
	MainForm(){
		
	}
private:
	Point p1;
	Point p2;
	
	vector<Shape*> shapeVector;
    
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));
	}
	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));
	}
	
	
	
	this->Refresh();
	Form::OnMouseUp(e);
}
virtual void OnPaint(const PaintEventArgs& e){
	
	for(auto shape: shapeVector){
	shape->Draw(e.Graphics);
	}
	
	Form::OnPaint(e);
}
...
class Shape{
public:
	virtual void Draw(const Graphics& g) = 0;
	virtual ~Shape(){};
}
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++设计模式-李建忠
 |