代码
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{
};
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);
}
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;
}
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;
}
virtual void Draw(const Graphics& g){
g.DrawRectangle(Pens.Red,
leftUp,width,height);
}
};
class Circle : public Shape{
public:
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);
}
依赖倒置原则DIP
- 高层模块(一般是稳定的)不应该依赖于低层模块(一般是变化的),二者都应该依赖于抽象(稳定的)
像上面的代码中,MainForm就属于高层模块,而Line、Rectangle(忽略我们自己加的Circle)就属于低层模块。而写法一中稳定的MainForm就依赖于会变化的Line和Rectangle。而写法二就改成依赖于一个抽象出来的Shape类,Line和Rectangle都是依赖于Shape这个抽象的。 - 抽象(稳定的)不应该依赖于实现细节(变化的),实现细节应该依赖于抽象。
就像写法二中Shape不会使用子类对象(如果这么做了Shape就不是稳定的)低层模块的方法实现应该是继承于接口,按照接口定义的抽象方法来实现,而不是接口去按照低层模块实现的方法来定义接口
开放封闭原则OCP
- 对扩展开放,对更改封闭
- 类模块应该是可扩展的,但是不可更改
有新的需求时不要想着对代码更改,而是想着增加一些东西应对需求。像写法一里除了改动一是扩展,其余改动都是修改。而写法二中如果用工厂模式其实不会有改动二,只会有改动一的扩展。(扩展指在已有代码基础上扩展代码,即新增模块、类、方法等,而非修改已有代码,比如修改模块、类、方法等)
单一职责原则SRP
- 一个类应该仅用一个引起它变化的原因
- 变化的方向隐含着类的责任
Liskov替换原则LSP
- 子类必须能够替换它们的基类(即is-a)
- 继承表达类型抽象
接口隔离原则ISP
- 不应该强迫客户程序依赖它们不用的方法
- 接口应该小而完备
优先使用对象组合而不是类继承
- 类继承通常为“白箱复用”,对象组合通常为“黑箱复用”
- 继承在某种程度上破坏了封装性,子类父类耦合度较高
- 对象组合只要求被组合的对象具有良好定义的接口,耦合度低
对象组合指在一个类中的对象可以将其他类的对象作为自己的组成部分。
封装变化点
- 使用封装来创建对象之间的分界层,让设计者在分界层的一侧进行修改,而不会对另一侧产生不良影响,从而实现层次间的松耦合。
针对接口编程而不是针对实现编程
- 不将变量类型声明为某个特定的具体类,而是声明为某个接口
写法一就违背了面向接口设计原则,Line和Rectangle都依赖一个具体类。写法二就将Line和Rectangle变为具体的一个抽象接口 - 客户程序无需获知对象的具体类型,只需知道对象所具有的接口
- 减少系统中各部分的依赖关系,从而实现高内聚低耦合