设计模式 简介


学习设计模式的目的:

  1. 理解松耦合
  2. 掌握面向对象设计模式
  3. 掌握重构技法
  4. 掌握GOF

可复用

手法:面向对象

本人下一步要学习架构搭建


抽象思维非常重要。

面向对象-》组件封装-》设计模式-》架构模式

面向对象

封装:隐藏内部代码

继承:复用现有代码

多态:改写对象行为


主要是变化导致

解决变化的复杂性:

  1. 分解:分而治之。--》C语言,大问题分解成小问题。
  2. 抽象:忽视非本质特征,去泛化和理想化对象模型。

下面是伪代码方式说明分解方式和抽象方式,对于后期需求变化修改产生的影响:

//分解方法
//Shape.h
#pragma once
//例子为伪码
//设计画图工具
//点
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
//分解的设计方法。
#include 
#include 
#include "Shape.h"

using namespace std;

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

	//这个地方用的是类对象,非指针
	vector lineVector;
	vector rectVector;
	//改变
	vector 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 (rdoCircle.Checked)
	{
		Circle circle();
		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);
}


//抽象方法---可复用
//Shape_abstract.h
#pragma once
class Shape
{
public:
	virtual void Draw(const Graphics& g) = 0;
	//虚析构函数作用:子类通过多态释放时,子类的析构函数才能被正确的调用到
	virtual ~Shape() {}
};

class Point
{
public:
	int x;
	int y;
};


//所有继承都推荐用public
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(Pend.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, ...);
	}
};





//抽象方法---可复用
//MainForm_abstract.cpp
#include 
#include 
#include "shape_abstract.h"

using namespace std;

class MainForm : public Form
{

private:
	Point p1;
	Point p2;

	//针对所有形状----注意,这个地方用的是Shape指针
	//这里面我们需要使用多态性。可能是Line Rect类型。
	//如果不用指针会造成对象切割,切割成小对象。。会有问题
	//必须要房Shape指针
	vector shapeVector;

public:
	MainFor()
	{
		//...
	}

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)
	{
		//此处不能放栈对象,在MainForm的析构函数中要复制释放shapeVector中存放的堆对象指针
		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(...);
	}
	//...
	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);

}


//用户有修改需求

软件设计的目标---》复用。

软件设计的金科玉律---复用。

展开阅读全文

页面更新:2024-04-30

标签:模式   这个地方   指针   分解   抽象   对象   需求   代码   方式   简介   方法

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top