常用的设计模式(单例模式和工厂模式)

设计模式

单例模式

    1. 私有构造函数,防止外部直接实例化
    2. 静态成员函数,返回唯一实例的引用
    3. 静态局部变量,在程序生命周期内

    #include<iostream>
    #include<map>
    using namespace std;

    class shoppingCartManager {
    public:
    // 获取购物车实例
    // 2. 静态成员函数, 返回唯一实例的引用
    static shoppingCartManager& getInstance(){
    static shoppingCartManager instance;// 3. 静态局部变量,在程序生命周期内只被创建一次,因此,每次调用getInstance,都返回相同的实例
    return instance;
    }

      void addToChart(const string& itemName, int quantity) {
      	cart[itemName] += quantity;
      }
    
      void viewCart() const {
      	for (const auto& item : cart) {
      		cout << item.first << "" << item.second << endl;
      	}
      }
    

    private:
    // 1, 私有构造函数,防止外部直接实例化
    shoppingCartManager() {}
    map<string, int>cart;
    };

    int main() {
    string itemName;
    int quantity;
    shoppingCartManager& ca = shoppingCartManager::getInstance();
    ca.addToChart("aaa", 3);
    while (cin >> itemName >> quantity) {
    if (itemName == "end") break;
    shoppingCartManager& cart = shoppingCartManager::getInstance();
    cart.addToChart(itemName, quantity);
    }

      const shoppingCartManager& cart = shoppingCartManager::getInstance();
      cart.viewCart();
    
      return 0;
    

    }

工厂模式

  • 对类内部数据的只读访问

    const vector<Block*> & getBlocks() const{
    	return blocks;
    }
    
  • 创建型设计模式,引入抽象类和具体类

  • 一个工厂方法可以创建一个具体产品

    #include <iostream>
    #include <vector>

    using namespace std;

    // 抽象积木接口
    class Block {
    public:
    virtual void produce() = 0;
    };

    // 具体圆形积木实现
    class CircleBlock :public Block {
    public:
    void produce() override {
    cout << "Circle Block." << endl;
    }
    };

    // 具体方形积木实现
    class SquareBlock : public Block {
    public:
    void produce() override {
    cout << "Square Block." << endl;
    }
    };

    // 抽象积木工厂接口
    class BlockFactory {
    public:
    virtual Block* createBlock() = 0;
    };

    // 具体圆形积木实现
    class CircleFactory : public BlockFactory {
    public:
    Block* createBlock() override {
    return new CircleBlock();
    }
    };

    // 具体方形积木实现
    class SquareFactory : public BlockFactory {
    public:
    Block* createBlock() override {
    return new SquareBlock();
    }
    };

    // 积木工厂系统
    class BlockFactorySystem {
    private:
    vector<Block*> blocks;
    public:
    void produceBlocks(BlockFactory* factory, int quantity) {
    for (int i = 0; i < quantity; i++) {
    Block* block = factory->createBlock();
    blocks.push_back(block);
    block->produce();
    }
    }

      // 对类内部数据的只读访问
      const vector<Block*>& getBlocks() const {
      	return blocks;
      }
    
      ~BlockFactorySystem() {
      	for (Block* block : blocks) {
      		delete block;
      	}
      }
    

    };

    int main() {
    BlockFactorySystem factorySystem;

      int productionCount;
      cin >> productionCount;
    
      for (int i = 0; i < productionCount; i++) {
      	string blockType;
      	int quantity;
      	cin >> blockType >> quantity;
    
      	if (blockType == "Circle") {
      		factorySystem.produceBlocks(new CircleFactory(), quantity);
      	}
      	else {
      		factorySystem.produceBlocks(new SquareFactory(), quantity);
      	}
      }
      const vector<Block*>& blocks = factorySystem.getBlocks();
      for (auto& block : blocks) {
      	block->produce();
      }
      
      return 0;
    

    }

抽象工厂模式

  • 一个工厂方法可以创建一类具体产品

  • 应用场景:使用抽象工厂模式来创建不同数据库的连接对象

    #include <iostream>
    #include <string>

    using namespace std;

    // 抽象椅子类
    class Chair {
    public:
    virtual void showInfo() = 0;
    };

    class ModernChair : public Chair {
    public:
    void showInfo()override {
    cout << "modern chair" << endl;
    }
    };

    class ClassicalChair : public Chair {
    void showInfo() override {
    cout << "classical chair" << endl;
    }
    };

    class Sofa {
    public:
    virtual void displayInfo() = 0;
    };

    class ModernSofa : public Sofa {
    public:
    void displayInfo() override{
    cout << "display sofa" << endl;
    }
    };

    class ClassicalSofa : public Sofa {
    public:
    void displayInfo() override {
    cout << "classical sofa" << endl;
    }
    };

    // 抽象家具工厂接口
    class furnitureFactory {
    public:
    virtual Chair* createChair() = 0;
    virtual Sofa* createSofa() = 0;
    };

    // 现代家具工厂接口
    class ModernFurnitureFactory : public furnitureFactory {
    public:
    Chair* createChair() override {
    return new ModernChair();
    }
    Sofa* createSofa() override {
    return new ModernSofa();
    }
    };

    // 传统家具工厂接口
    class ClassicalFunitureFactory : public furnitureFactory {
    public:
    Chair* createChair() override {
    return new ClassicalChair();
    }
    Sofa* createSofa() override {
    return new ClassicalSofa();
    }
    };

    int main() {
    int N;
    cin >> N;

      for (int i = 0; i < N; i++) {
      	string furnitureType;
      	cin >> furnitureType;
    
      	furnitureFactory* factory = nullptr;
      	if (furnitureType == "Modern") {
      		factory = new ModernFurnitureFactory();
      	}
      	else if (furnitureType == "Classical") {
      		factory = new ClassicalFunitureFactory();
      	}
    
      	Chair* chair = factory->createChair();
      	Sofa* sofa = factory->createSofa();
    
      	chair->showInfo();
      	sofa->displayInfo();
    
      	delete chair;
      	delete sofa;
      	delete factory;
      }
      return 0;
    

    }

相关推荐
ThereIsNoCode1 小时前
「软件设计模式」状态模式(State)
设计模式·状态模式
菜鸟一枚在这7 小时前
深入理解设计模式之代理模式
java·设计模式·代理模式
mjr9 小时前
设计模式-Java
java·设计模式
yuanpan9 小时前
23种设计模式之《组合模式(Composite)》在c#中的应用及理解
开发语言·设计模式·c#·组合模式
菜鸟一枚在这11 小时前
深入解析设计模式之单例模式
开发语言·javascript·单例模式
FLZJ_KL11 小时前
【设计模式】【创建型模式】单例模式(Singleton)
java·单例模式·设计模式
非 白13 小时前
【Java】单例模式
java·笔记·单例模式
万兴丶14 小时前
Unity 适用于单机游戏的红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含源码)
数据结构·unity·设计模式·c#
菜鸟一枚在这14 小时前
深入剖析抽象工厂模式:设计模式中的架构利器
设计模式·架构·抽象工厂模式
码熔burning14 小时前
(三)趣学设计模式 之 抽象工厂模式!
设计模式·抽象工厂模式