【c++之设计模式】组合使用:抽象工厂模式与单例模式

简介

学以致用,使用抽象工厂及单例模式创建不同轿车及轿车装饰品。

代码

    1. 定义一个抽象工厂类来创建不同类型的轿车和轿车装饰品。抽象工厂类中具有创建不同类型轿车和轿车装饰品的纯虚方法。

abstractFactory.h

#pragma once

#include "Car.h"
#include "CarDecorate.h"

class AbstractFactory
{
public:
    virtual Car* createCar() = 0;
    virtual CarDecorate* createDecorate() = 0;
};
  • 2.定义不同类型的轿车,如 SUV 和跑车。

car.h

#pragma once

#include 

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") {}
};
  • 3.定义不同类型的轿车装饰品,比如车载音响和真皮座椅。

carDecorate.h

#pragma once

#include 

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") {}
};
  • 4.创建具体的工厂类,用于创建不同类型轿车及轿车装饰品。使用单例模式来确保每个工厂类只有一个实例。

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();
}
  • 5.可以使用工厂类来创建不同类型的轿车及轿车装饰品。

main.cpp

#include 
#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++,c++,设计模式,抽象工厂模式)