• C++闲谈04——设计模式


    C++闲谈04——设计模式

    单例模式

    饿汉式单例模式

    #include
    #include
    
    using namespace std;
    mutex mtx;
    
    
    class SingleTon{
    public:
        static SingleTon* GetInstance() {
            if (instance == nullptr) {
                lock_guard<mutex> lk(mtx);  // 不是尖括号
                instance = new SingleTon();
            }
    
            return instance;
        }
    private:
        SingleTon() {
            cout << "test" << endl;
        }
    
        static SingleTon* instance;
    };
    
    SingleTon* SingleTon::instance = nullptr;
    
    int main() {
        SingleTon* test1 = SingleTon::GetInstance();
        SingleTon* test2 = SingleTon::GetInstance();
        system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    优点:对象提前创建好了 使用的时候无需等待

    缺点:对象提前创建 占用内存

    懒汉式单例模式

    #include
    #include
    
    using namespace std;
    mutex mtx;
    
    
    class SingleTon{
    public:
        static SingleTon* GetInstance() {
            if (instance == nullptr) {
                lock_guard<mutex> lk(mtx);  // 不是尖括号
                instance = new SingleTon();
            }
    
            return instance;
        }
    private:
        SingleTon() {
            cout << "test" << endl;
        }
    
        static SingleTon* instance;
    };
    
    SingleTon* SingleTon::instance = nullptr;
    
    int main() {
        SingleTon* test1 = SingleTon::GetInstance();
        SingleTon* test2 = SingleTon::GetInstance();
        system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    优点:使用的时候才创建 不占内存

    缺点:使用的时候需要创建 比较耗时

    工厂模式

    工厂模式

    简单工厂模式

    • 工厂类
    • 抽象产品类
    • 具体产品类

    image-20221203120811174

    #include 
    #include 
    using namespace std;
      
    typedef enum ProductTypeTag
    {
      Hair,
      Hisense,
    }PRODUCTTYPE;
      
    //抽象产品类 TV(电视机类)
    class TV
    {
    public:
      virtual void Show() = 0;
      virtual ~TV(){};//声明析构函数为虚函数,防止内存泄漏
    };
     //具体产品类 HairTV(海尔电视类)
    class HairTV : public TV
    {
    public:
      void Show()
      {
        cout<<"I'm HairTV "<<endl;
      }
    };
     //具体产品类 HisenseTV(海信电视类)  
    class  HisenseTV : public TV
    {
    public:
      void Show()
      {
        cout<<"I'm HisenseTV"<<endl;
      }
    };
      
     
    // 工厂类 TVFactory(电视机工厂类)
    class TVFactory
    {
    public:
      TV* CreateTV(PRODUCTTYPE type)
      {
        switch (type)
        {
        case Hair:
          return new HairTV();
      
        case Hisense:
          return new HisenseTV();
        default:
          return NULL;
        }
      }
    };
      
    int main(int argc, char *argv[])
    {
        // 创建工厂类对象
        TVFactory* myTVFactory = new  TVFactory();
        TV* hairTV = myTVFactory->CreateTV(Hair);
        if (hairTV != NULL)
            hairTV->Show();
     
        TV* hisenseTV = myTVFactory->CreateTV(Hisense);
        if (hisenseTV != NULL)
            hisenseTV->Show();
     
        delete  myTVFactory;
        myTVFactory = NULL;
     
        delete hairTV;
        hairTV = NULL;
     
        delete hisenseTV;
        hisenseTV = NULL;  
      
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    TVFactory 是工厂类,它是整个系统的核心,提供了静态工厂方法CreateTV(),该方法中包含一个字符串类型的参数,在内部业务逻辑中根据参数值得不同实例化不同的具体产品类,返回相依的对象。简单工厂模式的缺点是 如增加 生成TCL电视时,需要修改工厂类TVFactory。

    小结:

    在程序中,需要创建的对象很多,导致对象的new操作多且杂时,需要使用简单工厂模式。

    工厂方法模式

    简单工厂模式中最大的缺点是当有新产品要加入系统时,必须要修改工厂类,加入必要的处理逻辑,违背了“开闭原则”

    工厂方法模式定义:在工厂模式中,工厂父类负责定义创建产品对象的公告接口,而工厂子类负责生成具体的产品对象。目的是将产品的实例化操作延迟到工厂子类中完成,通过工厂子类来确定究竟应该实例化哪一个具体产品类。

    image-20221203145257797

    工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。

    #include 
    using namespace std;
     /*抽象产品类 TV(电视机类)*/ 
    class TV
    {
    public:
      virtual void Show() = 0;
     virtual ~TV();//声明析构函数为虚函数,防止内存泄漏
    };
     /*具体产品类 HairTV(海尔电视机类)*/ 
    class HairTV : public TV
    {
    public:
      void Show()
      {
        cout<< "I'm HairTV"<<endl;
      }
    };
    /*具体产品类 HisenseTV(海信电视机类)*/
    class HisenseTV : public TV
    {
    public:
      void Show()
      {
        cout<< "I'm HisenseTV"<<endl;
      }
    };
    /*工厂类(电视机工厂类)*/  
    class TVFactory
    {
    public:
      virtual TV *CreateTV() = 0;
       virtual ~TVFactory(){};//析构函数声明为虚函数,防止内存泄漏
    };
     /*具体工厂类 HairTVFactory(海尔电视机工厂类)*/ 
    class HairTVFactory : public TVFactory
    {
    public:
      TV *CreateTV()
      {
        return new HairTV ();
      }
    };
     /*具体工厂类 HisenseTV(海信电视机工厂类)*/ 
    class HisenseTVFactory : public TVFactory
    {
    public:
      TV *CreateTV()
      {
        return new HisenseTV ();
      }
    };
      
    int main(int argc , char *argv [])
    {
      TVFactory *hairTVFactory = new HairTVFactory();
      TV *hairTV = hairTVFactory->CreateTV();
      hairTV->Show();
      
      TVFactory *hisenseTVFactory = new HisenseTVFactory();
      TV *hisenseTV = hisenseTVFactory->CreateTV();
      hisenseTV->Show();
      
      if (hairTVFactory!= NULL)
      {
        delete hairTVFactory;
        hairTVFactory = NULL;
      }
      
      if (hairTV != NULL)
      {
        delete hairTV;
        hairTV = NULL;
      }
      
      if (hisenseTVFactory != NULL)
      {
        delete hisenseTVFactory;
        hisenseTVFactory = NULL;
      }
      
      if (hisenseTV != NULL)
      {
        delete hisenseTV;
        hisenseTV = NULL;
      }
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    抽象工厂模式

    抽象工厂模式是工厂方法模式的泛化版,工厂模式是一种特殊的抽象工厂模式,在工厂模式中,每个具体工厂只能生产一种具体的产品,如海尔电视机厂只生产海尔电视机,而抽象工厂方法模式中,一个具体的工厂可以生产多个具体产品。

    image-20221203145524563

    优点:

    抽象工厂模式将产品族的依赖与约束关系放到抽象工厂中,便于管理。
    职责解耦,用户不需要关心一堆自己不关心的细节,由抽象工厂来负责组件的创建
    切换产品族容易,只需要增加一个具体工厂实现,客户端选择另一个套餐就可以了
    缺点:

    抽象工厂模式类增加的速度很快,有一个产品族就需要增加一个具体工厂实现,比较繁琐
    产品族难以扩展产品。当产品族中增加一个产品时,抽象工厂接口中需要增加一个函数,对应的所有具体工厂实现都需要修改,修改放大严重。
    抽象工厂并未完全屏蔽创建细节,给出的都是组件。对于这种情况可以结合工厂模式或简单工厂模式一起使用

    #include 
    using namespace std;
      
    // 抽象产品类类 Television(电视机类)
    class Television
    {
    public:
        virtual void Show() = 0;
        virtual ~Television(){};//析构函数声明为虚函数,防止内存泄漏
    };
     
    //具体产品类 HaierTelevision(海尔电视机类)
    class HaierTelevision : public Television
    {
    public:
        void Show()
        {
            cout << "I'm HaierTelevision" << endl;
        }
    };
    //具体产品类 TCLTelevision(TCL电视机类)
    class TCLTelevision : public Television
    {
    public:
        void Show()
        {
            cout << "I'm TCLTelevision" << endl;
        }
    };
     
    // 抽象产品类  AirCondition(空调类)
    class AirCondition
    {
    public:
        virtual void Show() = 0;
        virtual ~AirCondition(){};//析构函数声明为虚函数,防止内存泄漏
    };
    //具体产品类 HairAirCondition(海尔空调类)
    class HairAirCondition : public AirCondition
    {
    public:
        void Show()
        {
            cout << "I'm HairAirCondition" << endl;
        }
    };
    //具体产品类 TCLAirCondition(TCL空调类)
    class TCLAirCondition : public AirCondition
    {
    public:
        void Show()
        {
            cout << "I'm TCLAirCondition" << endl;
        }
    };
     
    // 抽象工厂类 EFactory(电器工厂类)
    class EFactory
    {
    public:
        virtual Television* CreateTelevision() = 0;
        virtual AirCondition* CreateAirCondition() = 0;
         virtual ~EFactory(){};//析构函数声明为虚函数,防止内存泄漏
    };
    //具体工厂类 HairFactory(海尔工厂类)
    class HairFactory : public EFactory
    {
    public:
        Television* CreateTelevision()
        {
            return new HaierTelevision();
        }
     
        AirCondition* CreateAirCondition()
        {
            return new HairAirCondition();
        }
    };
    //具体工厂类 TCLFactory(TCL工厂类) 
    class TCLFactory : public EFactory
    {
    public:
        Television* CreateTelevision()
        {
            return new TCLTelevision();
        }
     
        AirCondition* CreateAirCondition()
        {
            return new TCLAirCondition();
        }
    };
      
    int main(int argc, char *argv[])
    {
      EFactory *hairFactory = new HairFactory ();/*实例化工厂抽象类*/
      Television *haierTelevision =hairFactory->CreateTelevision();/*实例化产品抽象类*/
      AirCondition *haierAirCondition = hairFactory->CreateAirCondition();
      
      haierTelevision->Show();
      haierAirCondition->Show();
      
      EFactory *tCLFactory = new TCLFactory ();
      Television *tCLTelevision = tCLFactory->CreateTelevision();
      AirCondition *tCLAirCondition = tCLFactory->CreateAirCondition();
      
      tCLTelevision->Show();
      tCLAirCondition->Show();
      
      if (hairFactory != NULL)
      {
        delete hairFactory;
        hairFactory = NULL;
      }
      
      if (haierTelevision != NULL)
      {
        delete haierTelevision;
        haierTelevision= NULL;
      }
      
      if (tCLAirCondition != NULL)
      {
        delete tCLAirCondition;
        tCLAirCondition = NULL;
      }
      
      if (tCLFactory != NULL)
      {
        delete tCLFactory;
        tCLFactory= NULL;
      }
      
      if (tCLTelevision != NULL)
      {
        delete tCLTelevision;
        tCLTelevision = NULL;
      }
      
      if (tCLAirCondition != NULL)
      {
        delete tCLAirCondition;
        tCLAirCondition = NULL;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    总结

    大家应该已经发现了,其实抽象工厂模式如果只有一个组件的话,其实是退化到了工厂方法模式,也就是没有了产品族的概念,只剩一个产品了,因此简单工厂,工厂方法,抽象工厂这三者之间是有内在联系的,区别只是产品的复杂度。

    抽象工厂的本质是选择产品族,因此大家可以根据这个特征来识别是否可以应用抽象工厂。
    简单工厂:唯一工厂类,一个产品抽象类,工厂类的创建方法依据入参判断并创建具体产品对象。
    工厂方法:多个工厂类,一个产品抽象类,利用多态创建不同的产品对象,避免了大量的if-else判断。
    抽象工厂:多个工厂类,多个产品抽象类,产品子类分组,同一个工厂实现类创建同组中的不同产品,减少了工厂子类的数量。 增加了泛化能力

  • 相关阅读:
    农场管理小程序|基于微信小程序的农场管理系统设计与实现(源码+数据库+文档)
    Java最新面试宝典 Spring面试题
    labview与stm32通信
    4. react路由
    [unity][通过代码]控制模型旋转,动态改变模型角度,让模型转动到固定角度
    【SpringBoot笔记19】SpringBoot框架如何自定义starter启动器
    CSDN每日一练 |『相似三角形』『 运输石油』『鬼画符门之宗门大比』2023-09-13
    centos 上安装 kafka 与 python 调用
    【中秋国庆不断更】HarmonyOS对通知类消息的管理与发布通知(下)
    Sunlogin RCE漏洞分析和使用
  • 原文地址:https://blog.csdn.net/qq_41945053/article/details/128169471