自学设计模式(简单工厂模式、工厂模式、抽象工厂模式)

使用工厂模式来生产某类对象(代码简化且容易维护,类之间有血缘关系,可以通过工厂类进行生产);

简单工厂模式(用于创建简单对象)

对于简单工厂模式,需要的工厂类只有一个;

在工厂类中的公共成员函数来创建所需对象;

cpp 复制代码
#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个工厂类来解决上述问题(对简单工厂模式解耦合),对已经写好的代码无需修改;

cpp 复制代码
#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;
}

抽象工厂模式

通过造船,船分为三个模块,船体、武器、动力,三个模块又分为了简易、标准、旗舰三个版本

cpp 复制代码
#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类设置三种规格的船。

相关推荐
Zane19942 天前
一个 new 就能创建对象,为什么还要拆出单例、工厂、建造者、原型四种模式?
设计模式
怕浪猫2 天前
一行命令复刻爆款视频,我把 Hypit 从安装跑到了出片
人工智能·设计模式·程序员
Zane19943 天前
函数式编程里的函数,其实不是你天天写的那个函数——三大编程范式的边界在哪
设计模式
Zane19944 天前
策略模式现在该不该上?一次讲清楚过度设计和设计不足怎么找平衡
设计模式
她说..4 天前
常见设计模式-模板方法模式
java·spring·设计模式·springboot
xiaofeiyang1504 天前
第六章 · 桥接 — 三支毛笔,画出九种颜色
设计模式
Shadow(⊙o⊙)5 天前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki5 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅665 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa6 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio