- 使用成员变量:可以在类中定义成员变量来表示对象的状态,通过修改这些成员变量的值来反映对象的状态变化。
- 封装状态修改操作:将修改状态的操作封装在成员函数中,确保状态的改变遵循一定的规则和逻辑,防止非法状态的出现。
- 使用状态模式:当对象的状态比较复杂,且状态之间的转换有较多逻辑时,可以考虑使用状态模式,将不同的状态封装为不同的类,将状态的切换逻辑封装在这些状态类中。
使用状态模式的示例代码:
cpp
#include <iostream>
#include <string>
class State; // 前向声明
class Context {
private:
State* currentState;
public:
Context(State* state) : currentState(state) {}
void setState(State* state) {
currentState = state;
}
void request() {
currentState->handle(this);
}
};
class State {
public:
virtual ~State() {}
virtual void handle(Context* context) = 0;
};
class StateA : public State {
public:
void handle(Context* context) override {
std::cout << "State A handling" << std::endl;
context->setState(new StateB()); // 状态转换
}
};
class StateB : public State {
public:
void handle(Context* context) override {
std::cout << "State B handling" << std::endl;
context->setState(new StateA()); // 状态转换
}
};
int main() {
Context context(new StateA());
context.request(); // 调用状态 A 的处理
context.request(); // 调用状态 B 的处理
return 0;
}
实现工厂模式:
- 简单工厂模式:创建一个工厂类,该类包含一个静态函数,根据输入的参数创建不同类型的对象。
- 工厂方法模式:在基类中定义一个抽象的工厂方法,让子类实现该方法来创建具体的对象。
- 抽象工厂模式:创建一个抽象工厂类,其中包含多个抽象的工厂方法,每个工厂方法负责创建一种产品族的对象,具体工厂类实现这些方法。
简单工厂模式的示例代码:
cpp
#include <iostream>
#include <memory>
#include <string>
class Product {
public:
virtual ~Product() {}
virtual void show() = 0;
};
class ConcreteProductA : public Product {
public:
void show() override {
std::cout << "ConcreteProductA" << std::endl;
}
};
class ConcreteProductB : public Product {
public:
void show() override {
std::cout << "ConcreteProductB" << std::endl;
}
};
class SimpleFactory {
public:
static std::unique_ptr<Product> createProduct(const std::string& type) {
if (type == "A") {
return std::make_unique<ConcreteProductA>();
} else if (type == "B") {
return std::make_unique<ConcreteProductB>();
} else {
return nullptr;
}
}
};
int main() {
auto productA = SimpleFactory::createProduct("A");
if (productA) {
productA->show();
}
auto productB = SimpleFactory::createProduct("B");
if (productB) {
productB->show();
}
return 0;
}
工厂方法模式的示例代码:
cpp
#include <iostream>
class Product {
public:
virtual ~Product() {}
virtual void show() = 0;
};
class ConcreteProductA : public Product {
public:
void show() override {
std::cout << "ConcreteProductA" << std::endl;
}
};
class ConcreteProductB : public Product {
public:
void show() override {
std::cout << "ConcreteProductB" << std::endl;
}
};
class Creator {
public:
virtual ~Creator() {}
virtual Product* createProduct() = 0;
};
class ConcreteCreatorA : public Creator {
public:
Product* createProduct() override {
return new ConcreteProductA();
}
};
class ConcreteCreatorB : public Creator {
public:
Product* createProduct() override {
return new ConcreteProductB();
}
};
int main() {
Creator* creatorA = new ConcreteCreatorA();
Product* productA = creatorA->createProduct();
productA->show();
delete productA;
delete creatorA;
Creator* creatorB = new ConcreteCreatorB();
Product* productB = creatorB->createProduct();
productB->show();
delete productB;
delete creatorB;
return 0;
}
抽象工厂模式的示例代码:
cpp
#include <iostream>
class ProductA {
public:
virtual ~ProductA() {}
virtual void show() = 0;
};
class ConcreteProductA1 : public ProductA {
public:
void show() override {
std::cout << "ConcreteProductA1" << std::endl;
}
};
class ConcreteProductA2 : public ProductA {
public:
void show() override {
std::cout << "ConcreteProductA2" << std::endl;
}
};
class ProductB {
public:
virtual ~ProductB() {}
virtual void show() = 0;
};
class ConcreteProductB1 : public ProductB {
public:
void show() override {
std::cout << "ConcreteProductB1" << std::endl;
}
};
class ConcreteProductB2 : public ProductB {
public:
void show() override {
std::cout << "ConcreteProductB2" << std::endl;
}
};
class AbstractFactory {
public:
virtual ~AbstractFactory() {}
virtual ProductA* createProductA() = 0;
virtual ProductB* createProductB() = 0;
};
class ConcreteFactory1 : public AbstractFactory {
public:
ProductA* createProductA() override {
return new ConcreteProductA1();
}
ProductB* createProductB() override {
return new ConcreteProductB1();
}
};
class ConcreteFactory2 : public AbstractFactory {
public:
ProductA* createProductA() override {
return new ConcreteProductA2();
}
ProductB* createProductB() override {
return new ConcreteProductB2();
}
};
int main() {
AbstractFactory* factory1 = new ConcreteFactory1();
ProductA* productA1 = factory1->createProductA();
ProductB* productB1 = factory1->createProductB();
productA1->show();
productB1->show();
delete productA1;
delete productB1;
delete factory1;
AbstractFactory* factory2 = new ConcreteFactory2();
ProductA* productA2 = factory2->createProductA();
ProductB* productB2 = factory2->createProductB();
productA2->show();
productB2->show();
delete productA2;
delete productB2;
delete factory2;
return 0;
}