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

本文介绍了C++中单例模式的实现,以及工厂模式(包括抽象工厂模式)在类创建和数据访问中的应用,展示了如何使用这些模式组织和管理复杂系统中的对象创建。
摘要由CSDN通过智能技术生成

设计模式

单例模式

    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;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值