在基类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");
}
测试结果