简单工厂模式

文章介绍了简单工厂模式的适用场景,包括产品已固定、同类别不分系列且产品种类不多的情况。代码示例展示了如何用C++实现一个简单的汽车工厂,生成不同系列的汽车对象。同时指出了模式的缺点,如产品增多时工厂类会变得臃肿,以及添加新产品需修改工厂类。
摘要由CSDN通过智能技术生成

简单工厂模式适用于下列场景:

1所有产品都已确定下来,不再添加新产品也不删除旧产品(如果需要频繁添加或删除产品推荐使用工厂方法模式)

2所有产品都属于同一类别(不分系列,这里博主犯了一个错误,如产品分多个系列,推荐使用抽象工厂模式)

3产品种类基数不大,否则工厂类将会很臃肿(个人觉得这一点相比前两点对开发影响较小)

简单工厂模式代码:

#include <iostream>
#include <vector>
#include <utility>
#include <random>
using namespace std;

 
const string s_dynastic = "dynastic";
const string s_ocean = "ocean";
const string dyna_arr[5] = {"QING","HAN","TANG","SONG","YUAN"};
const string ocean_arr[3] = {"SEAL","DOLPHIN","SEAGULL"};
const string color_arr[7] = {"red","black","yellow","golden","sliver","blue","white"};

typedef enum carTypeDynastic
{
    carty_qing,
    carty_han,
    carty_tang,
    carty_song,
    carty_yuan
}carTyDy;

typedef enum carTypeOcean
{
    carty_seal,
    carty_dolplin,
    carty_sea_gull
}carTySea;

class bydCar
{
    public:
    virtual void type() = 0;

};

class bydQing:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydQing(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];     
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};

class bydHan:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydHan(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];     
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};

class bydTang:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydTang(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];        
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};


class bydSong:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydSong(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];        
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};

class bydYuan:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydYuan(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];     
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};

class bydSeal:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydSeal(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];     
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};

class bydDolphin:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydDolphin(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];     
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};

class bydSeaGull:public bydCar
{
    public:
    std::string series;
    std::string type_name;
    std::string color;
    bydSeaGull(std::string s, std::string t) : bydCar(),series(s),type_name(t)
    {
        int index = rand()%7;
        color = color_arr[index];     
    }
    void type() override
    {
        std::cout << "car type is " << series << " : " << type_name << "color: " << color <<std::endl;
    }
};


class bydEquipmentFactory
{
    public:
    //根据产品信息创建具体的产品类实例,返回一个抽象产品类
    static bydCar* creatDynastic(carTypeDynastic car_type)
    {
        switch(car_type)
        {
            case carTypeDynastic::carty_han:
                return new bydHan(s_dynastic,"HAN");
            case carTypeDynastic::carty_qing:
                return new bydQing(s_dynastic,"QING"); 
            case carTypeDynastic::carty_tang:
                return new bydTang(s_dynastic,"TANG");    
            case carTypeDynastic::carty_song:
                return new bydSong(s_dynastic,"SONG");    
            case carTypeDynastic::carty_yuan:
                return new bydYuan(s_dynastic,"YUAN");       
            default:
            return nullptr;
        }
   }

   static bydCar* creatOcean(carTypeOcean car_type)
   {
        switch(car_type)
        {
            case carTypeOcean::carty_seal:
                return new bydSeal(s_dynastic,"SEAL");
            case carTypeOcean::carty_dolplin:
                return new bydDolphin(s_dynastic,"DOLPHIN"); 
            case carTypeOcean::carty_sea_gull:
                return new bydSeaGull(s_dynastic,"SEAGULL");    
            default:
            return nullptr;
        }
   }
};


int main()
{
    time(0);
    std::pair<carTypeOcean, int > ocean_indent[3] = { std::pair<carTypeOcean , int >  (carTypeOcean::carty_seal,15) ,
                                                    std::pair<carTypeOcean , int >  (carTypeOcean::carty_dolplin,10) ,
                                                    std::pair<carTypeOcean , int >  (carTypeOcean::carty_sea_gull,3)};

    std::vector<bydCar*> products;

    for(int i = 0 ; i < 3 ; i ++)
    {
        int cnt = ocean_indent[i].second;
        for(int j = 0 ; j < cnt ; j ++)
        {
            bydCar*car = bydEquipmentFactory::creatOcean(ocean_indent[i].first);
            car->type();
            products.push_back(car);
        }
    }

     
}

总结:

优点 在调用创建产品接口时候,调用者不需要关心创建细节

缺点 1在产品种类较多时候,工厂类将会显得非常臃肿

        2 调用者需要添加新产品时候,工厂类的设计着需要修改工厂类的模型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值