设计模式是在软件工程中被广泛应用的、经过验证的解决特定问题的解决方案。以下是几种常用的设计模式,以及它们的适用场景和代码示例:
单例模式(Singleton Pattern)
单例模式是一种创建型模式,确保一个类只有一个实例,并提供一个全局访问点。
适用场景:
- 当需要严格控制对某个类的实例化次数时。
- 当某个类需要频繁地被访问,且访问时需要保持一致状态时。
代码示例(C++):
class Singleton {
private:
static Singleton* instance;
Singleton() {} // 私有构造函数
public:
static Singleton* GetInstance() {
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
void DoSomething() {
std::cout << "Doing something...\n";
}
};
Singleton* Singleton::instance = nullptr;
int main() {
Singleton* s1 = Singleton::GetInstance();
Singleton* s2 = Singleton::GetInstance();
s1->DoSomething();
s2->DoSomething();
return 0;
}
工厂模式(Factory Pattern)
工厂模式是一种创建型模式,用于创建对象,而无需指定将要创建的对象的具体类。它使用一个工厂类来封装对象的创建细节。
适用场景:
- 当创建对象需要依赖于复杂的选择逻辑时。
- 当需要创建的对象类型在编译时无法确定时。
代码示例(C++):
// 产品基类
class ProductA {
public:
virtual void Use() = 0;
};
// 具体产品A1
class ConcreteProductA1 : public ProductA {
public:
void Use() override {
std::cout << "Result of the product A1\n";
}
};
// 具体产品A2
class ConcreteProductA2 : public ProductA {
public:
void Use() override {
std::cout << "Result of the product A2\n";
}
};
// 工厂基类
class Creator {
public:
virtual ProductA* FactoryMethod() = 0;
};
// 具体工厂1
class ConcreteCreator1 : public Creator {
public:
ProductA* FactoryMethod() override {
return new ConcreteProductA1();
}
};
// 具体工厂2
class ConcreteCreator2 : public Creator {
public:
ProductA* FactoryMethod() override {
return new ConcreteProductA2();
}
};
int main() {
Creator* creator = new ConcreteCreator1();
ProductA* product = creator->FactoryMethod();
product->Use();
delete product; // 释放产品
delete creator; // 释放工厂
return 0;
}
中介者模式(Mediator Pattern)
中介者模式是一种行为型模式,定义了一个中介对象来封装一系列对象之间的交互。它使各对象之间不需要显示地相互引用。
适用场景:
- 当一组对象以复杂的方式进行通信时。
- 当想要减少对象之间的依赖时。
代码示例(C++):
#include <iostream>
#include <list>
#include <algorithm>
// 抽象中介者
class Mediator {
public:
virtual void Send(const std::string& msg, Colleague* c) = 0;
};
// 具体中介者
class ConcreteMediator : public Mediator {
std::list<Colleague*> colleagues;
public:
void Add(Colleague* c) {
colleagues.push_back(c);
}
void Send(const std::string& msg, Colleague* c) override {
for (auto it = colleagues.begin(); it != colleagues.end(); ++it) {
if ((*it) != c) {
(*it)->Notify(msg);
}
}
}
};
// 抽象同事类
class Colleague {
protected:
Mediator* mediator;
public:
Colleague(Mediator* m) : mediator(m) {}
virtual void Send(const std::string& msg) = 0;
virtual void Notify(const std::string& msg) = 0;
};
// 具体同事类A
class ConcreteColleagueA : public Colleague {
public:
ConcreteColleagueA(Mediator* m) : Colleague(m) {}
void Send(const std::string& msg) override {
mediator->Send(msg, this);
}
void Notify(const std::string& msg) override {
std::cout << "ColleagueA received: " << msg << std::endl;
}
};
// 具体同事类B
class ConcreteColleagueB : public Colleague {
public:
ConcreteColleagueB(Mediator* m) : Colleague(m) {}
void Send(const std::string& msg) override {
mediator->Send(msg, this);
}
void Notify(const std::string& msg) override {
std::cout << "ColleagueB received: " << msg << std::endl;
}
};
int main() {
ConcreteMediator* mediator = new ConcreteMediator();
ConcreteColleagueA* a = new ConcreteColleagueA(mediator);
ConcreteColleagueB* b = new ConcreteColleagueB(mediator);
mediator->Add(a);
mediator->Add(b);
a->Send("Hello B");
b->Send("Hello A");
delete a;
delete b;
delete mediator;
return 0;
}