设计模式简介

课程目标:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1aLmGFi3-1591490352093)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1591355271653.png)]

什么是设计模式

“每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次又一次的使用该方案而不必做重复劳动”。

​ —Christopher

GOF设计模式:

在这里插入图片描述
在这里插入图片描述

从面向对象谈起:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BDfsVCR2-1591490352103)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1591356149431.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lS02M0Me-1591490352105)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1591356190336.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JMnPBOWb-1591490352107)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1591356245413.png)]

深入理解面向对象:

在这里插入图片描述

软件设计固有的复杂性

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qzVQYMPc-1591490352111)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1591356591788.png)]

软件设计复杂的根本原因:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FsVfJE63-1591490352112)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1591356826002.png)]

如何解决复杂性:

在这里插入图片描述

结构化 VS 面向对象

分解

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& leftUp, int width, int height) {
		this->leftUp = leftUp;
		this->width = width;
		this->height = height;
	}
};

MainForm.cpp

class MainForm : public Form {
private:
	Point p1;
	Point p2;

	vector<Line> lineVector;
	vector<Rect> rectVector;

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

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

	//...

	Form::OnPaint(e);
}

抽象

Shape2.h

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

MainForm2.cpp

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) {
		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);
		rectVector.push_back(new Rect(p1, width, height));
	}

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

改变:增加圆

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& leftUp, int width, int height) {
		this->leftUp = leftUp;
		this->width = width;
		this->height = height;
	}
};
//增加
class Circle{
    
};

MainForm.cpp

class MainForm : public Form {
private:
	Point p1;
	Point p2;

	vector<Line> lineVector;
	vector<Rect> rectVector;
    
    //改变
    vector<Circle> rectVector;

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

抽象

Shape2.h

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

MainForm2.cpp

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

软件设计的目标:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-G46vUnrl-1591490352116)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1591490197171.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值