首页 技术 正文
技术 2022年11月10日
0 收藏 345 点赞 2,178 浏览 3728 个字

1.学习目标

1)理解松耦合设计思想;

2)掌握面向对象设计原则;

3)掌握重构技法改善设计;

4)掌握GOF核心设计模式;

2.定义

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

3.思维模型:

1)底层思维:向下,如何把握机器底层从微观理解对象构造。内容包括语言构造、编译转换、内存模型、运行时机制。

向下需要深入理解三大面向对象机制。

封装:隐藏内部实现

继承:复用现有代码

多态:改变对象行为

2)抽象思维:向上,如何将我们的周围世界抽象为成程序代码。内容包括面向对象、组件封装、设计模式、架构模式。

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

4.软件设计复杂性的根本原因:变化

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

5.如何解决复杂性

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

//Shape1.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{
};
//MainForm1.cpp
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);
}

抽象:人们处理复杂性问题的通用技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。

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

在绘制点、线、矩形的问题上,分解和抽象两种思维下的解决方案是不一样的。在出现了新需求(画圆)后,两种方案应对策略也是不同的。

3.6 软件设计的目标:复用!

参考:《李建忠C++设计模式》视频

https://www.bilibili.com/video/av64805906/?p=1

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289