设计模式中的创建型模式主要关注对象的创建过程,旨在将对象的创建过程与它们的使用过程分离,以提高代码的灵活性和可维护性。常见的创建型模式有以下几种:
- 单例模式(Singleton Pattern)
- 工厂方法模式(Factory Method Pattern)
- 抽象工厂模式(Abstract Factory Pattern)
- 生成器模式(Builder Pattern)
- 原型模式(Prototype Pattern)
1. 单例模式(Singleton Pattern)
单例模式确保一个类只有一个实例,并提供一个全局访问点。
cpp
class Singleton {
private:
static Singleton* instance;
// 私有化构造函数以防止外部实例化
Singleton() {}
public:
static Singleton* getInstance() {
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
void showMessage() {
cout << "Hello from Singleton!" << endl;
}
};
// 初始化静态成员
Singleton* Singleton::instance = nullptr;
int main() {
Singleton* singleton = Singleton::getInstance();
singleton->showMessage();
return 0;
}
2. 工厂方法模式(Factory Method Pattern)
工厂方法模式定义一个用于创建对象的接口,但由子类决定实例化哪个类。工厂方法使一个类的实例化延迟到其子类。
cpp
class Product {
public:
virtual void use() = 0;
};
class ConcreteProductA : public Product {
public:
void use() override {
cout << "Using Product A" << endl;
}
};
class ConcreteProductB : public Product {
public:
void use() override {
cout << "Using Product B" << endl;
}
};
class Creator {
public:
virtual Product* factoryMethod() = 0;
void anOperation() {
Product* product = factoryMethod();
product->use();
}
};
class ConcreteCreatorA : public Creator {
public:
Product* factoryMethod() override {
return new ConcreteProductA();
}
};
class ConcreteCreatorB : public Creator {
public:
Product* factoryMethod() override {
return new ConcreteProductB();
}
};
int main() {
Creator* creatorA = new ConcreteCreatorA();
creatorA->anOperation();
Creator* creatorB = new ConcreteCreatorB();
creatorB->anOperation();
delete creatorA;
delete creatorB;
return 0;
}
3. 抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
cpp
class AbstractProductA {
public:
virtual void use() = 0;
};
class AbstractProductB {
public:
virtual void use() = 0;
};
class ConcreteProductA1 : public AbstractProductA {
public:
void use() override {
cout << "Using Product A1" << endl;
}
};
class ConcreteProductA2 : public AbstractProductA {
public:
void use() override {
cout << "Using Product A2" << endl;
}
};
class ConcreteProductB1 : public AbstractProductB {
public:
void use() override {
cout << "Using Product B1" << endl;
}
};
class ConcreteProductB2 : public AbstractProductB {
public:
void use() override {
cout << "Using Product B2" << endl;
}
};
class AbstractFactory {
public:
virtual AbstractProductA* createProductA() = 0;
virtual AbstractProductB* createProductB() = 0;
};
class ConcreteFactory1 : public AbstractFactory {
public:
AbstractProductA* createProductA() override {
return new ConcreteProductA1();
}
AbstractProductB* createProductB() override {
return new ConcreteProductB1();
}
};
class ConcreteFactory2 : public AbstractFactory {
public:
AbstractProductA* createProductA() override {
return new ConcreteProductA2();
}
AbstractProductB* createProductB() override {
return new ConcreteProductB2();
}
};
int main() {
AbstractFactory* factory1 = new ConcreteFactory1();
AbstractProductA* productA1 = factory1->createProductA();
AbstractProductB* productB1 = factory1->createProductB();
productA1->use();
productB1->use();
AbstractFactory* factory2 = new ConcreteFactory2();
AbstractProductA* productA2 = factory2->createProductA();
AbstractProductB* productB2 = factory2->createProductB();
productA2->use();
productB2->use();
delete productA1;
delete productB1;
delete productA2;
delete productB2;
delete factory1;
delete factory2;
return 0;
}
4. 生成器模式(Builder Pattern)
生成器模式将复杂对象的构建过程分离出来,使得同样的构建过程可以创建不同的表示。
cpp
class Product {
public:
void addPart(const string& part) {
parts.push_back(part);
}
void show() {
for (const auto& part : parts) {
cout << part << " ";
}
cout << endl;
}
private:
vector<string> parts;
};
class Builder {
public:
virtual void buildPartA() = 0;
virtual void buildPartB() = 0;
virtual Product* getResult() = 0;
};
class ConcreteBuilder : public Builder {
public:
ConcreteBuilder() {
product = new Product();
}
void buildPartA() override {
product->addPart("PartA");
}
void buildPartB() override {
product->addPart("PartB");
}
Product* getResult() override {
return product;
}
private:
Product* product;
};
class Director {
public:
void setBuilder(Builder* builder) {
this->builder = builder;
}
void construct() {
builder->buildPartA();
builder->buildPartB();
}
private:
Builder* builder;
};
int main() {
Director director;
Builder* builder = new ConcreteBuilder();
director.setBuilder(builder);
director.construct();
Product* product = builder->getResult();
product->show();
delete product;
delete builder;
return 0;
}
5. 原型模式(Prototype Pattern)
原型模式通过复制现有的对象来创建新对象,而不是通过实例化类来创建对象。
cpp
class Prototype {
public:
virtual Prototype* clone() = 0;
virtual void use() = 0;
};
class ConcretePrototype1 : public Prototype {
public:
ConcretePrototype1(const string& name) : name(name) {}
Prototype* clone() override {
return new ConcretePrototype1(*this);
}
void use() override {
cout << "Using " << name << endl;
}
private:
string name;
};
class ConcretePrototype2 : public Prototype {
public:
ConcretePrototype2(const string& name) : name(name) {}
Prototype* clone() override {
return new ConcretePrototype2(*this);
}
void use() override {
cout << "Using " << name << endl;
}
private:
string name;
};
int main() {
Prototype* prototype1 = new ConcretePrototype1("Prototype1");
Prototype* prototype2 = new ConcretePrototype2("Prototype2");
Prototype* clone1 = prototype1->clone();
Prototype* clone2 = prototype2->clone();
clone1->use();
clone2->use();
delete prototype1;
delete prototype2;
delete clone1;
delete clone2;
return 0;
}
总结
创建型模式提供了灵活、可维护的对象创建方式,帮助我们在软件开发中更加高效地管理对象的创建过程。通过了解和使用这些模式,可以编写出更加健壮、可扩展的代码。