【C++设计模式】1-设计模式简介

设计模式简介

一、面向对象

底层思维

向下,如何把握机器底层从微观理解对象构造:
  • 语言构造
  • 编译转换
  • 内存模型
  • 运行时机制

抽象思维

向上,如何将我们的周围世界抽象为程序代码:
  • 面向对象
  • 组件封装
  • 设计模式
  • 架构模式

二、深入理解面向对象

向下

深入理解三大面向对象机制:

  • 封装,隐藏内部实现
  • 继承,复用现有代码
  • 多态,改写对象行为

向上

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

三、软件设计固有的复杂性

四、软件设计复杂的根本原因

变化

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

五、软件设计的目标

复用

六、如何解决复杂性

分解

    分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。

抽象

    由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化的对象模型。

七、代码示例

重构前(分解)

// MainVision.cpp
class MainForm : public Form{
private:
	Point p1;
	Point p2;
	vector<Line> lineVector;
	vector<Line> 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);
}

重构后(抽象)

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

// MainVision.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));
	}
	//...
	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);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值