OOP的目标

  1. 理解松耦合的设计思想
  2. 掌握面向对象的设计原则(设计原则比模式更为重要)
  3. 掌握重构的技法,改善设计
  4. 掌握GOF核心设计模式

image-20200806154857558

向下:深入理解三大面向对象机制 • 封装,隐藏内部实现 • 继承,复用现有代码 • 多态,改写对象行为

向上:深刻把握面向对象机制所带来的抽象意义,理解如何使用这些机制来表达现实世界,掌握什么是“好的面向对象设计”

软件设计复杂的根本原因

变化!!!

  • 客户需求的变化
  • 技术平台的变化
  • 开发团队的变化
  • 市场环境的变化

如何解决复杂性?

  1. 分解 • 人们面对复杂性有一个常见的做法:即分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。
  2. 抽象 • 更高层次来讲,人们处理复杂性有一个通用的技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。

什么是好的软件设计?软件设计的金科玉律: 复用!

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);
}

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{
};

使用抽象的方法:

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);
}

class 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, ...);
	}
};