1 什么是设计模式
“每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次又一次地使用该方案而不必做重复劳动”。
2 面向对象
1.程序员需要打通底层思维与抽象思维,什么是底层?什么是抽象?
1.1. 底层思维:向下,如何把握机器底层从微观理解对象构造
• 语言构造
• 编译转换
• 内存模型
• 运行时机制
1.2. 抽象思维:向上,如何将我们的周围世界抽象为程序代码
• 面向对象
• 组件封装
• 设计模式
• 架构模式
2.我们需要深入理解面向对象,如何理解?分为两个方面:
2.1.向下:深入理解三大面向对象机制
• 封装,隐藏内部实现
• 继承,复用现有代码
• 多态,改写对象行为
2.2.向上:深刻把握面向对象机制所带来的抽象意义,理解如何使用
这些机制来表达现实世界,掌握什么是“好的面向对象设计
3 软件设计固有的复杂性
3.1 建筑商从来不会去想给一栋已建好的100层高的楼房底下再新修一个小地下室——这样做花费极大而且注定要失败。然而令人惊奇的是,软件系统的用户在要求作出类似改变时却不会仔细考虑,而且他们认为这只是需要简单编程的事.什么是复杂性呢?—变化
3.2 变化:
• 客户需求的变化
• 技术平台的变化
• 开发团队的变化
• 市场环境的变化
4 如何解决复杂性
1 分解
• 人们面对复杂性有一个常见的做法:即分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。
2 抽象
• 更高层次来讲,人们处理复杂性有一个通用的技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。
5 代码解释
下面代码主要的意思是绘制出不同形状的图形,代码1是利用分解法。代码2采用抽象法。
代码1主要思路就是每个图形对应一个类,对于每个类通过ifelse来进行判断并进行逻辑处理
代码2定义一个虚基类,逻辑处理利用多态进行处理
假设现在要增加一个圆形,代码1的代码变动要比代码2要多很多,不仅需要新定义类,再逻辑处理上也需要进行改变;而代码2仅仅需要定义一个新类即可,逻辑处理基本上通过多态进行区分。
// 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{
};
// main1.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,
...);
}
};
// main2.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(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);
}
6 软件设计的目标
什么是好的软件设计?软件设计的金科玉律:复用