目录
- 1 单例模式
- 2 工厂方法模式
- 3 抽象工厂模式
- 4
- 参考
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;
}
注意要点:
- 构造函数私有化,防止外部直接实例化
ShoppingCart
类的对象。 - 静态变量
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
参考
卡码网