您的位置:首页 > 编程语言 > C语言/C++

C++设计模式 factory工厂模式,实现绘制几何形状

2017-05-23 21:25 615 查看
恰逢学习c++模板,练习题中有一题要求:

使用工厂模式,实现图形的绘制。

不会啊,得,查一下,一下是我主要参考的两篇博文

参考博客1

参考博客2

ps:另参考书籍:c++primer
大话数据结构


得到一个最终目的,使用抽象工厂模式实现。

具体模式区别就不多说了,但是抽象工厂模式是最终形态,优点是:

不用修改已有代码,开放封闭原则:对扩展开放,对更改封闭

先把框架搭好

point

shape

factory

以下是第一步最简单的简单工厂模式的代码,并为加入easyx库,仅仅实现输出图形的名称

/*
CopyRight Lu 2017 ZJ
简单工厂模式绘制几何形状
*/
#include <iostream>
using namespace std;
enum ch{triangle,rectangle,circle};

class point
{
private:
float x,y;
public:
point(float x=0,float y=0)
{
this->x=x;
this->y=y;
}
};
class shape
{
public:
shape(){}
virtual void show()=0;
};
class triangle1:public shape
{
private:
point a,b,c;
public:
triangle1(){}
void show()
{
cout<<"triangle"<<endl;
}
};
class circle1:public shape
{
private:point a;float r;
public:
circle1(){}
void show()
{
cout<<"circle"<<endl;
}
};
class rectangle1:public shape
{
private:point a,b,c,d;
public:
rectangle1(){}
void show()
{
cout<<"rectangle"<<endl;
}
};
class factory
{
public:
shape *creat(ch type)
{
switch(type)
{
case triangle:return new triangle1();
break;
case rectangle:return new rectangle1();
break;
case circle:return new circle1();
break;
dafault:break;
}
}
};
int main()
{
factory newfactory;
shape* shape1=newfactory.creat(rectangle);
shape1->show();
shape* shape2=newfactory.creat(triangle);
shape2->show();
shape* shape3=newfactory.creat(circle);
shape3->show();
return 0;
}


仔细想了下,工厂方法模式与简单工厂模式的区别在于对程序的保护。使用工厂方法模式后,代码只许增加新的工厂,而不是在原工厂中修改。

但亦有弊端

/*
CopyRight Lu 2017 ZJ
工厂方法模式绘制几何形状
*/
#include<iostream>
using namespace std;

enum ch{triangle1,rectangle1,circle1};

class point
{
private:
float x,y;
public:
point(float x=0,float y=0)
{
this->x=x;
this->y=y;
}
};

class shape
{
public:
virtual ~shape(){}
virtual void show()=0;
};
class triangle:public shape
{
public:
void show(){cout<<"triangle"<<endl;}
};
class rectangle:public shape
{
public:
void show(){cout<<"rectangle"<<endl;}
};
class circle:public shape
{
public:
void show(){cout<<"circle"<<endl;}
};

class factory
{
public:
virtual ~factory(){}
virtual shape*creat()=0;
};
class trifactory:public factory
{
public:
shape*creat()
{
return new triangle();
}
};
class recfactory:public factory
{
public:
shape*creat()
{
return new rectangle();
}
};
class cirfactory:public factory
{
public:
shape*creat()
{
return new circle();
}
};

int main()
{
recfactory factory1;
shape* shape1=factory1.creat();
shape1->show();
trifactory factory2;
shape* shape2=factory2.creat();
shape2->show();
cirfactory factory3;
shape* shape3=factory3.creat();
shape3->show();
return 0;
}


终于搞懂了抽象工厂模式,下面解释一下我的理解

相比于简单工厂模式:一个工厂生产所有产品

工厂方法模式:一个工厂只生产一个产品

前者违反了面向对象的开闭原则,后者效率又不是很高,尤其是产品多且成品牌时。

比如,显卡有,1060,1070,1080,1080ti,我们华硕,技嘉,微星,索泰,映众等厂。

而且,华硕的卡有战枭,猛禽,龙骑等系列,技嘉有g1,萤火虫,大雕………………

一个工厂只生产一块显卡,就很蠢了。

如果我是技嘉的厂,不管它是1060,1070,1080,还是1080ti,也不管它是g1,萤火虫还是大雕,我统统生产。

但是如果,我罗技,也来卖显卡,只要新建一间罗技的厂,亦可以生产所有的显卡。

所以说,增加产品族很方便,符合开闭原则,且效率远高于工厂方法模式。

但但是,我是英伟达,我要新推出1090的显卡,就很麻烦了。

我得重写一遍所有的架构,这无疑就违反了开闭原则。

下面给出抽象工厂模式的代码

/*
CopyRight Lu 2017 ZJ
抽象工厂模式绘制几何形状
*/
#include<iostream>
using namespace std;

enum ch{triangle1,rectangle1,circle1,triangle2,rectangle2,circle2};

class point
{
private:
float x,y;
public:
point(float x=0,float y=0)
{
this->x=x;
this->y=y;
}
};

class shape
{
public:
virtual ~shape(){}
virtual void show()=0;
};
class triangle:public shape
{
public:
void show()=0;
};
class triangle01:public triangle
{
public:
void show()
{
cout<<"triangle01"<<endl;
};
};
class triangle02:public triangle
{
public:
void show()
{
cout<<"triangle02"<<endl;
};
};

class rectangle:public shape
{
public:
void show()=0;
};
class rectangle01:public rectangle
{
public:
void show()
{
cout<<"rectangle01"<<endl;
};
};
class rectangle02:public rectangle
{
public:
void show()
{
cout<<"rectangle02"<<endl;
};
};
class circle:public shape
{
public:
void show()=0;
};
class circle01:public circle
{
public:
void show()
{
cout<<"circle01"<<endl;
};
};
class circle02:public circle
{
public:
void show()
{
cout<<"circle02"<<endl;
};
};
class factory
{
public:
virtual ~factory(){}
virtual triangle*creattri()=0;
virtual rectangle*creatrec()=0;
virtual circle*creatcir()=0;
};
class factory01:public factory
{
public:
triangle*creattri()
{
return new triangle01();
}
rectangle*creatrec()
{
return new rectangle01();
}
circle*creatcir()
{
return new circle01();
}
};
class factory02:public factory
{
public:
triangle*creattri()
{
return new triangle02();
}
rectangle*creatrec()
{
return new rectangle02();
}
circle*creatcir()
{
return new circle02();
}
};

void main()
{
factory01 factory1;
factory02 factory2;
triangle *tri1=NULL;
rectangle *rec1=NULL;
circle *cir1=NULL;
tri1=factory1.creattri();
rec1=factory2.creatrec();
cir1=factory1.creatcir();
tri1->show();
rec1->show();
cir1->show();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 工厂模式