学以致用,使用抽象工厂及单例模式创建不同轿车及轿车装饰品。
abstractFactory.h
#pragma once
#include "Car.h"
#include "CarDecorate.h"
class AbstractFactory
{
public:
virtual Car* createCar() = 0;
virtual CarDecorate* createDecorate() = 0;
};
car.h
#pragma once
#include <string>
class Car
{
public:
Car(std::string name) : m_name(name) {
}
virtual ~Car() {
}
std::string getName() const {
return m_name; }
private:
std::string m_name;
};
class SUV : public Car
{
public:
SUV() : Car("SUV") {
}
};
class SportsCar : public Car
{
public:
SportsCar() : Car("SportsCar") {
}
};
carDecorate.h
#pragma once
#include <string>
class CarDecorate
{
public:
CarDecorate(std::string name) : m_name(name) {
}
virtual ~CarDecorate() {
}
std::string getName() const {
return m_name; }
private:
std::string m_name;
};
class CarAudio : public CarDecorate
{
public:
CarAudio() : CarDecorate("CarAudio") {
}
};
class LeatherSeat : public CarDecorate
{
public:
LeatherSeat() : CarDecorate("LeatherSeat") {
}
};
suvFactory .h
#pragma once
#include "AbstractFactory.h"
class SUVFactory : public AbstractFactory
{
public:
static SUVFactory* getInstance();
virtual Car* createCar() override;
virtual CarDecorate* createDecorate() override;
private:
SUVFactory() {
}
static SUVFactory* m_instance;
};
class SportsCarFactory : public AbstractFactory
{
public:
static SportsCarFactory* getInstance();
virtual Car* createCar() override;
virtual CarDecorate* createDecorate() override;
private:
SportsCarFactory() {
}
static SportsCarFactory* m_instance;
};
在实现工厂类时,使用单例模式创建工厂类的实例,确保每个工厂只有一个实例。实现工厂类的 createCar()
和 createDecorate()
方法,用于创建不同类型的轿车及轿车装饰品。
suvFactory.cpp
#include "SUVFactory.h"
#include "SportsCarFactory.h"
SUVFactory* SUVFactory::m_instance = nullptr;
SportsCarFactory* SportsCarFactory::m_instance = nullptr;
SUVFactory* SUVFactory::getInstance()
{
if (m_instance == nullptr)
{
m_instance = new SUVFactory();
}
return m_instance;
}
Car* SUVFactory::createCar()
{
return new SUV();
}
CarDecorate* SUVFactory::createDecorate()
{
return new LeatherSeat();
}
SportsCarFactory* SportsCarFactory::getInstance()
{
if (m_instance == nullptr)
{
m_instance = new SportsCarFactory();
}
return m_instance;
}
Car* SportsCarFactory::createCar()
{
return new SportsCar();
}
CarDecorate* SportsCarFactory::createDecorate()
{
return new CarAudio();
}
main.cpp
#include <iostream>
#include "SUVFactory.h"
#include "SportsCarFactory.h"
int main()
{
AbstractFactory* suvFactory = SUVFactory::getInstance();
Car* suv = suvFactory->createCar();
CarDecorate* suvDecorate = suvFactory->createDecorate();
std::cout << suv->getName() << " with " << suvDecorate->getName() << std::endl;
AbstractFactory* sportsCarFactory = SportsCarFactory::getInstance();
Car* sportsCar = sportsCarFactory->createCar();
CarDecorate* sportsCarDecorate = sportsCarFactory->createDecorate();
std::cout << sportsCar->getName() << " with " << sportsCarDecorate->getName() << std::endl;
return 0;
}
以上就是使用抽象工厂及单例模式结合创建不同轿车及轿车装饰品示例。记录。
更多【c++-【c++之设计模式】组合使用:抽象工厂模式与单例模式】相关视频教程:www.yxfzedu.com