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

设计模式

单例模式

    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;
}

相关推荐

  1. 常用设计模式模式工厂模式

    2024-04-01 23:12:02       16 阅读
  2. 【Python】模式工厂模式

    2024-04-01 23:12:02       8 阅读
  3. 设计模式】:模式

    2024-04-01 23:12:02       37 阅读
  4. 设计模式——模式

    2024-04-01 23:12:02       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 23:12:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 23:12:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 23:12:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 23:12:02       18 阅读

热门阅读

  1. 使用 SQLite数据库,磁盘数据库,也叫本地数据库

    2024-04-01 23:12:02       14 阅读
  2. 关于过四级的一些神级技巧

    2024-04-01 23:12:02       18 阅读
  3. FastAPI+React全栈开发12 搭建FastAPI开发环境

    2024-04-01 23:12:02       12 阅读
  4. 规则引擎QLExpress和Drools的对比

    2024-04-01 23:12:02       13 阅读
  5. 5.控制结构,if、switch、for的使用【go】

    2024-04-01 23:12:02       11 阅读
  6. python 埃氏筛法判断一个数是否为素数

    2024-04-01 23:12:02       17 阅读
  7. ChatGPT:让学术写作更高效

    2024-04-01 23:12:02       16 阅读
  8. 大模型日报2024-04-01

    2024-04-01 23:12:02       16 阅读
  9. Leetcode 3097. Shortest Subarray With OR at Least K II

    2024-04-01 23:12:02       13 阅读
  10. C# 值类型和引用类型

    2024-04-01 23:12:02       12 阅读