C++中的23种设计模式精讲

发布时间:2024年01月14日

1 单例模式

题目链接为:小明的购物车

C++代码如下,

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class ShoppingCart {
public:
    static ShoppingCart& get_instance() {
        static ShoppingCart instance = ShoppingCart(); //静态变量,类的所有对象共用一个
        return instance;
    }

    vector<string>& get_goods() { 
        return goods;
    }
    
    vector<int>& get_cnt() {
        return cnt;
    }
    
    void show() {
        int n = goods.size();
        for (int i = 0; i < n; ++i) {
            cout << goods[i] << " " << cnt[i] << endl;
        }
        return;
    }
    
private:
    ShoppingCart () {} //构造函数私有化,防止外部直接实例化类的对象
    
    vector<string> goods; 
    vector<int> cnt; 
};

int main() {
    ShoppingCart& shopping_cart = ShoppingCart::get_instance();
    
    string a;
    int b;
    while (cin >> a >> b) {
        shopping_cart.get_goods().emplace_back(a);
        shopping_cart.get_cnt().emplace_back(b);
    }
    
    shopping_cart.show();
    
    return 0;
}

注意要点:

  1. 构造函数私有化,防止外部直接实例化ShoppingCart类的对象。
  2. 静态变量instance,只会被创建一次,故ShoppingCart类的所有对象共用一个instance。又因为变量instance的类型为ShoppingCart,故该类只会存在一个实例化对象。

2 工厂方法模式

题目链接:积木工厂

C++代码如下,

#include <iostream>
#include <vector>

using namespace std;

class Block {
public:
    virtual void produce() = 0;
};

class CircleBlock : public Block {
    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 CircleBlockFactory : public BlockFactory {
public:
    Block* createBlock() override {
        return new CircleBlock();
    }
};

class SquareBlockFactory : 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.emplace_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 CircleBlockFactory(), quantity);
        } else if (blockType == "Square") {
            factorySystem.produceBlocks(new SquareBlockFactory(), quantity);
        }
    }
    
    return 0;
}

3 抽象工厂模式

题目链接:家具工厂

C++代码如下,

#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 {
public:
    void showInfo() override {
        cout << "classical chair" << endl;
    }
};

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

class ModernSofa : public Sofa {
public:
    void displayInfo() override {
        cout << "modern 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 ClassicalFurnitureFactory : 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 ClassicalFurnitureFactory();
        }
        
        Chair* chair = factory->createChair();
        Sofa* sofa = factory->createSofa();
        
        chair->showInfo();
        sofa->displayInfo();
        
        delete chair;
        delete sofa;
        delete factory;
    }
    
    return 0;
}

4

参考

卡码网

文章来源:https://blog.csdn.net/YMWM_/article/details/135581832
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。