简介
学以致用,使用抽象工厂及单例模式创建不同轿车及轿车装饰品。
代码
-
- 定义一个抽象工厂类来创建不同类型的轿车和轿车装饰品。抽象工厂类中具有创建不同类型轿车和轿车装饰品的纯虚方法。
abstractFactory.h
cpp
#pragma once
#include "Car.h"
#include "CarDecorate.h"
class AbstractFactory
{
public:
virtual Car* createCar() = 0;
virtual CarDecorate* createDecorate() = 0;
};
- 2.定义不同类型的轿车,如 SUV 和跑车。
car.h
cpp
#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") {}
};
- 3.定义不同类型的轿车装饰品,比如车载音响和真皮座椅。
carDecorate.h
cpp
#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") {}
};
- 4.创建具体的工厂类,用于创建不同类型轿车及轿车装饰品。使用单例模式来确保每个工厂类只有一个实例。
suvFactory .h
cpp
#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
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
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;
}
结论
以上就是使用抽象工厂及单例模式结合创建不同轿车及轿车装饰品示例。记录。