c++创建型设计模式--工厂模式--抽象工厂(多个产品的构建--系列)

在基类CarBase基础上进行构建
基类

#pragma once
#include <iostream>
/**
 * 基类指针
 *Date :[10/12/2018 ]
 *Author :[RS]
 */
 class  CarBase {
public:
	virtual ~CarBase() {
		std::cout << "析构 carBase" << std::endl;
	}
	virtual void PrintName() {
		printf("骑车生产厂商:%s\n",mStrName.c_str());
	}
protected:
	CarBase() {};
	CarBase(const std::string& name) :mStrName(name) {};
	std::string mStrName;

};

基类的产品有两种具体产品的抽象子类

#pragma once
#include "CarBase.hpp"
#include <string>
/**
 *这里有两种类型的车型 
 *Date :[10/12/2018 ]
 *Author :[RS]
 */
class BussinessCarBase :public CarBase {
protected:
	BussinessCarBase() {}
	BussinessCarBase(const std::string& name) :CarBase(name) {}

};
class SportCarBase :public CarBase {
protected:
	SportCarBase(){}
	SportCarBase(const std::string& name) :CarBase(name) {}
};

针对两种不同的基类,不同公司有不同的方案

#pragma once
#include "CarType.hpp"
/**
 *对于两种不同的车型有多公司进行生产
 *Date :[10/12/2018 ]
 *Author :[RS]
 */
class BaoMaBussinessCar :public BussinessCarBase {
public:
	BaoMaBussinessCar() :BussinessCarBase("宝马") {}
	void PrintName()override {
		printf("宝马商务车\n");
		BussinessCarBase::PrintName();
	}
};
class BaoMaSportCar :public SportCarBase {
public:
	BaoMaSportCar() :SportCarBase("宝马") {}
	void PrintName()override {
		printf("宝马跑车车\n");
		SportCarBase::PrintName();
	}
};
class BenCiBussinessCar :public BussinessCarBase {
public:
	BenCiBussinessCar() :BussinessCarBase("奔驰") {}
	void PrintName()override {
		printf("奔驰商务车\n");
		BussinessCarBase::PrintName();
	}
};
class BenCiSportCar :public SportCarBase {
public:
	BenCiSportCar() :SportCarBase("奔驰") {}
	void PrintName()override {
		printf("奔驰跑车车\n");
		SportCarBase::PrintName();
	}
};

抽象工厂方法,其实就是不同公司为了方便客户,提供的接口,提供针对不同的类型生产不同的产品。达到多个产品的创建

#pragma once
#include "CarFilmType.hpp"
#include <memory>
class AbstructFactoryI {
public:
	virtual std::shared_ptr<BussinessCarBase> CreateBassCar() = 0;
	virtual std::shared_ptr<SportCarBase> CreateSportCar() = 0;
};
class BaoMaFactory :public AbstructFactoryI {
public:
	std::shared_ptr<BussinessCarBase> CreateBassCar()override {
		std::shared_ptr<BaoMaBussinessCar> car(new BaoMaBussinessCar());
		return car;
	 }
	 std::shared_ptr<SportCarBase> CreateSportCar()override {
		 std::shared_ptr<BaoMaSportCar> car(new BaoMaSportCar());
		 return car;
	 }
};
class BenCiFactory :public AbstructFactoryI {
public:
	std::shared_ptr<BussinessCarBase> CreateBassCar()override {
		std::shared_ptr<BenCiBussinessCar> car(new BenCiBussinessCar());
		return car;
	}
	std::shared_ptr<SportCarBase> CreateSportCar()override {
		std::shared_ptr<BenCiSportCar> car(new BenCiSportCar());
		return car;
	}
};

测试入口

#include "Manager.hpp"
#include "CarFactory.hpp"
#include "CarFactoryMethod.h"
#include "AssemCarFactory.hpp"
#include "AbstructFactory.h"
void main() {
	//CarFactory::TEST();
	//CarFacotryMethod::Test();
	//AssertCarFactory::Test();
	BaoMaFactory bm;
	BenCiFactory bc;
	{
		auto bmBCar = bm.CreateBassCar();
		bmBCar->PrintName();
	}
	{
		auto bmSCar = bm.CreateSportCar();
		bmSCar->PrintName();
		auto bcBcar = bc.CreateBassCar();
		bcBcar->PrintName();
		auto bcScar = bc.CreateSportCar();
		bcScar->PrintName();
	}
	
	system("pause");
}

测试结果

测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值