学习设计模式目标

Posted malloc1free

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习设计模式目标相关的知识,希望对你有一定的参考价值。

学习目标:

 1.理解松耦合设计思想

 2.掌握面向对象设计原则

 3.掌握重构技法改善设计

 4.掌握GOF核心设计模式

从面向对象谈设计模式:

 1.底层思维:向下,如何掌握机器底层,从微观理解对象构造

  • 语言构造
  • 编译转换
  • 内存模型
  • 运行时机制

       →封装,隐藏内部实现

       →继承,复用现有代码

       →多态,改写对象行为

  2.抽象思维:向上,如何将周围世界抽象为程序代码

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

软件设计复杂的根本原因

  变化!!!!

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

           。。。。

 如何解决复杂问题

   1.分解:人们面对复杂性有一个常见的做法:即分而治之,将大问题分解成多个小问题,将复杂问题分解为多个简单问题。(面向过程思想)

   2.更高层次来讲,人们处理复杂性有一个共同技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽略它的非本质,从而处理泛华和理想化了的对象模型。(现象对象思想)

面向过程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& letfUp, int width, int height) this->LeftUp = letfUp; this->width = width; this->height = height; ;

 MaintForm.cpp

class MainForm :publuc Form

privat:
	Point p1;
	Point p2;

	vector<line> lineVector;
	vevtor<Rect> rectVector;

public:
	MainForm() 
		//...
	

protected:
	virtual void OnMouseDown(const MouseEventArg& e);
	virtual void OnMouseUp(const MouseEventArg& e);
	virtual void OnPaint(const MouseEventArg& e);
;

void MainForm::OnMouseDown(const MouseEventArg& e) 
	p1.x = e.X;
	p1.y = e.Y;

	Form::OnMouseDown(e);


void MainForm::OnMouseUp(const MouseEventArg& e) 
	p2.x = e.X;
	p2.y = e.Y;

	if (rdoLine.Checled)
	
		Line line(p1, p2);
		lineVector.push_back(line);
	
	else if
	
		int width = abs(p2.x - p1.x);
		int height = abs(p2.y - p1.y);
		Rect rect(p1, width, height);
	
	//...
	this->Refresh();

	Form::OnMouseUp(e);
 

void MainForm::Onpaint(const PaintEventArg& e)

	//针对直线
	for (int i = 0; i < lineVector.size(); i++)
	
		e.Graphice.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.Graphice.DrawLine(Pens.Red,
			rectVector[i].leftup;
			rectVector[i].width,
			rectVector[i].height);
	
	//...
	Form::OnPaint(e);

 

面向对象实现方式

Shape.h

class Shape 
public:
	virtual void Draw(const Graphics& a) = 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:
	Point LeftUp;
	int width;
	int height;

	Rect(const Point& letfUp, int width, int height) 
		this->LeftUp = letfUp;
		this->width = width;
		this->height = height;
	

	//实现自己的Draw,负责画自己
	virtual void Draw(const Graphics& g) 
		g.DrawLine(Pens.Red
			leftUp, width,height);
	
; 

MainForm.cpp

class MainForm :publuc Form

privat:
	Point p1;
	Point p2;

	//针对所有形状
	vector<Shape*> shapeVector;

public:
	MainForm() 
		//...
	

protected:
	virtual void OnMouseDown(const MouseEventArg& e);
	virtual void OnMouseUp(const MouseEventArg& e);
	virtual void OnPaint(const MouseEventArg& e);
;

void MainForm::OnMouseDown(const MouseEventArg& e) 
	p1.x = e.X;
	p1.y = e.Y;

	Form::OnMouseDown(e);


void MainForm::OnMouseUp(const MouseEventArg& e) 
	p2.x = e.X;
	p2.y = e.Y;

	if (rdoLine.Checled)
	 
		shapeVector.push_back(new line(p1, p2));
	
	else if
	
		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 PaintEventArg& e)

	//针对所有形状
	for (int i = 0; i < shapeVector.size(); i++)
	
		shapeVector[i]->Draw(e.Graphics); //多态调用各负其责
	
	
	//...
	Form::OnPaint(e);

  

两者的优缺点

当需求变化时:

比如需要加画圆的需求。面向对象和面向过程的修改方式自己脑补。。。。

软件设计的目标

  复用!

 

以上是关于学习设计模式目标的主要内容,如果未能解决你的问题,请参考以下文章

《从零开始学Swift》学习笔记(Day 64)——Cocoa Touch设计模式及应用之目标与动作

目标编写的abcd模式中b指的是

JAVA设计模式学习--代理模式

前端学习目标分项

《javascript高级程序设计》学习笔记 | 9.1.代理基础

Facade模式——设计模式学习(转载)