设计模式
单例模式
-
- 私有构造函数,防止外部直接实例化
- 静态成员函数,返回唯一实例的引用
- 静态局部变量,在程序生命周期内
#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;
}