设计模式——工厂设计模式(c++版,简单理解)

工厂设计模式

它属于创建型模式,他提供了一种创建对象的最佳方式,他创建对象时不会对客户端暴露创建的逻辑,并且通过一个共同的接口来指向新创建的对象。
简单工厂模式
举个?:你和同学去金拱门,看着小姐姐头上的菜单,你看上了个儿童套餐A,你基友看上了儿童套餐B。你点餐的时候只用给小姐姐说要个儿童套餐A,等会你就会得到一个汉堡,一杯可乐,一份大薯和你看上的玩具。而且你和你基友领餐的时候都在同一个领餐区,你们不知道这个汉堡是如何做出来的,不知道是哪个小哥哥炸出来的,不知道炸了几分钟。这是一个典型的工厂模式的?。

一个简单工厂设计模式包含了以下4个角色:

  1. 抽象工厂:工厂的基类,提供公共的操作接口(我们所说的“金拱门”,他属于一个品牌)
  2. 具体工厂:这是简单工厂的核心,用来生产产品的(相当于我们进入的一家金拱门实体店)
  3. 抽象产品:具体产品的基类,提供产品操作的公共接口(相当于菜单,因为它是所有产品的一个抽象)
  4. 具体产品:抽象产品的实现(汉堡、薯条、可乐、套餐)

简单工厂模式结构图

简单工厂模式结构图


代码:
1.产品的抽象类与具体产品

//food.h
#ifndef FOOD_H
#define FOOD_H
class AbstractFood{
public:
	AbstractFood();
	virtual ~AbstractFood();
	virtual void getFood(int count) = 0;
};

class Coke:public AbstractFood{
public:
	Coke(int count);
	~Coke();
	virtual void getFood(int);
};

class Chips:public AbstractFood{
public:
	Chips(int count);
	~Chips();
	virtual void getFood(int);
};

class Hamburg:public AbstractFood{
public:
	Hamburg(int count);
	~Hamburg();
	virtual void getFood(int);
};

class ChildA:public AbstractFood{
public:
	ChildA(int count);
	~ChildA();
	virtual void getFood(int);
};

#endif
//food.cpp
#include <iostream>
#include "food.h"

using namespace std;

AbstractFood::AbstractFood(){}
AbstractFood::~AbstractFood(){}

Coke::Coke(int count){
	cout << "make 可乐 x " << count << endl;
}
Coke::~Coke(){}

Chips::Chips(int count){
	cout << "make 薯条 x " << count << endl;
}
Chips::~Chips(){}

Hamburg::Hamburg(int count){
	cout << "make 垃圾堡 x " << count << endl;
}
Hamburg::~Hamburg(){}

ChildA::ChildA(int count){
	cout << "make 可乐 x " << count << endl;
	cout << "make 薯条 x " << count << endl;
	cout << "make 垃圾堡 x " << count << endl;
}
ChildA::~ChildA(){}

void Coke::getFood(int count){
	while(count--){
		cout << "获得:可乐" << endl;
	}
}

void Chips::getFood(int count){
	while(count--){
		cout << "获得:薯条" << endl;
	}
}

void Hamburg::getFood(int count){
	while(count--){
		cout << "获得:辣鸡堡" << endl;
	}
}

void ChildA::getFood(int count){
	while(count--){
		cout << "获得:儿童套餐A" << endl;
	}
}

2.工厂的抽象类与工厂的实现类

//JGM.h
#ifndef JGM_H
#define JGM_H
#include <string>
#include "food.h"
using namespace std;
class AbstractFood;

class AbstractJGM{
public:
	AbstractJGM();
	virtual ~AbstractJGM();
	virtual AbstractFood* make_food(int,int) = 0;
};

class JGM:public AbstractJGM{
public:
	JGM(string);
	~JGM();
	virtual AbstractFood* make_food(int,int);
};

#endif
//JGM.cpp
#include <iostream>
#include <string>
#include "JGM.h"
#include "food.h"

using namespace std;

AbstractJGM::AbstractJGM(){}
AbstractJGM::~AbstractJGM(){}

JGM::JGM(string shopName){
	cout << "欢迎来到金拱门——" << shopName << "店!" << endl; 
}
JGM::~JGM(){}

AbstractFood* JGM::make_food(int type, int count){
	switch (type){
		case 0: return new Coke(count);
		case 1: return new Chips(count);
		case 2: return new Hamburg(count);
		case 3: return new ChildA(count);
	}
	return NULL;
}

3.实现了上面的4个角色就完成了

//SimpleFactoryModel.cpp
#include <iostream>
#include <string>
#include "JGM.h"
#include "food.h"

using namespace std;

int main(){
	AbstractJGM *jgm = new JGM("北京路");  //你进入北京路的金拱门
	int count = 2;
	cout << "----------------" << endl;
	//0-可乐 1-薯条 2-汉堡 3-儿童套餐A
	AbstractFood *food1 = jgm->make_food(3,count);  //找小姐姐点了一个儿童套餐 
	cout << "----------------" << endl;
	food1->getFood(count);  //得到一个儿童套餐A(他做了什么你完全不知道,直接得到的是套餐)
	return 0;
}

实验截图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值