使用工厂模式来生产某类对象(代码简化且容易维护,类之间有血缘关系,可以通过工厂类进行生产);
简单工厂模式(用于创建简单对象)
对于简单工厂模式,需要的工厂类只有一个;
在工厂类中的公共成员函数来创建所需对象;
#include <bits/stdc++.h>
using namespace std;
// 产品父类
class shape{
public:
virtual void cal() = 0;
virtual ~shape(){
}
};
class triangle:public shape{
public:
void cal() override{
cout<<"三角形面积";
}
};
class square:public shape{
public:
void cal() override{
cout<<"正方形面积";
}
};
class cycle:public shape{
public:
void cal() override{
cout<<"圆形面积";
}
};
// 工厂类
enum class Type:char{
triangle ,
square ,
cycle
};
class fac{
public:
shape* creat_kinds(Type type){
shape* ptr = nullptr;
switch(type){
case Type::triangle:
ptr = new triangle;
break;
case Type::square:
ptr = new square;
break;
case Type::cycle:
ptr = new cycle;
break;
}
return ptr;
}
};
int main(){
fac* cur = new fac;
shape* obj = cur->creat_kinds(Type::cycle);
obj->cal();
return 0;
}
工厂模式
简单工厂模式会违反开放封闭原则 在添加类时需要修改子类、枚举类、工厂类、和判断代码
工厂模式通过创立N个工厂类来解决上述问题(对简单工厂模式解耦合),对已经写好的代码无需修改;
#include <bits/stdc++.h>
using namespace std;
// 产品父类
class shape{
public:
virtual void cal() = 0;
virtual ~shape(){
}
};
class triangle:public shape{
public:
void cal() override{
cout<<"三角形面积"<<endl;
}
};
class square:public shape{
public:
void cal() override{
cout<<"正方形面积"<<endl;
}
};
class cycle:public shape{
public:
void cal() override{
cout<<"圆形面积"<<endl;
}
};
// 工厂类
class fac{
public:
virtual shape* creat_kinds() = 0;
virtual ~fac(){
}
};
class triangle_fac:public fac{
public:
shape* creat_kinds(){
return new triangle;
}
~triangle_fac(){
cout<<"三角形被析构";
}
};
class square_fac:public fac{
public:
shape* creat_kinds(){
return new square;
}
~square_fac(){
cout<<"三角形被析构";
}
};
class cycle_fac:public fac{
public:
shape* creat_kinds(){
return new cycle;
}
~cycle_fac(){
cout<<"三角形被析构";
}
};
int main(){
fac* cur = new cycle_fac;
shape* obj = cur->creat_kinds();
obj->cal();
delete obj;
delete cur;
return 0;
}
抽象工厂模式
通过造船,船分为三个模块,船体、武器、动力,三个模块又分为了简易、标准、旗舰三个版本
#include <bits/stdc++.h>
using namespace std;
// 三个抽象类 每个抽象类下三个具体派生类
// 抽象工厂类 一个抽象类 三个派生类
class body{
public:
virtual void getbody() = 0;
virtual ~body(){
}
};
class wood_body:public body{
public:
void getbody() override{
cout<<"船的船体为<木头>---";
}
};
class iron_body:public body{
public:
void getbody() override{
cout<<"船的船体为<钢铁>---";
}
};
class mental_body:public body{
public:
void getbody() override{
cout<<"船的船体为<合成金属>---";
}
};
class weapon{
public:
virtual void getweapon() = 0;
virtual ~weapon(){
}
};
class gun_weapon:public weapon{
public:
void getweapon() override{
cout<<"船的武器为<枪>---" ;
}
};
class cannon_weapon:public weapon{
public:
void getweapon() override{
cout<<"船的武器为<炮>---" ;
}
};
class laser_weapon:public weapon{
public:
void getweapon() override{
cout<<"船的武器为<激光>---" ;
}
};
class power{
public:
virtual void getpower() = 0;
virtual ~power(){
}
};
class human_power:public power{
public:
void getpower() override{
cout<<"船的动力为<手动>---";
}
};
class engine_power:public power{
public:
void getpower() override{
cout<<"船的动力为<内燃机>---";
}
};
class nuclear_power:public power{
public:
void getpower() override{
cout<<"船的动力为<核反应堆>---";
}
};
class ship{
private:
body* m_body;
weapon* m_weapon;
power* m_power;
public:
ship(body* o_body , weapon* o_weapon , power* o_power):m_body(o_body) , m_weapon(o_weapon) , m_power(o_power){
m_body->getbody();
m_weapon->getweapon();
m_power->getpower();
}
~ ship(){
delete m_body;
delete m_weapon;
delete m_power;
}
};
class fac{
public:
virtual ship* get() = 0;
~ fac(){
}
};
class e_fac:public fac{
public:
ship* get() override{
ship* cur = new ship(new wood_body , new gun_weapon , new human_power);
return cur;
}
};
class m_fac:public fac{
public:
ship* get() override{
ship* cur = new ship(new iron_body , new cannon_weapon , new engine_power);
return cur;
}
};
class h_fac:public fac{
public:
ship* get() override{
ship* cur = new ship(new mental_body , new laser_weapon , new nuclear_power);
return cur;
}
};
int main() {
fac* cur = new h_fac;
ship* res = cur->get();
delete cur;
delete res;
return 0;
}
描述:首先定义三个抽象类,代表船的三个组成部分(船体、武器、动力);每个抽象类下有三个派生类,分别对应初级船、中级船、高级船所用的对应材料。通过ship类将船的三个部分组成起来,一个工厂抽象类fac,通过工厂抽象类的派生类,结合ship类设置三种规格的船。
版权归原作者 逮到647了 所有, 如有侵权,请联系我们删除。