工厂模式是一种创建型设计模式,它提供了一种创建对象的接口,但具体的对象创建逻辑由子类决定。工厂模式将对象的实例化过程封装在一个工厂类中,客户端只需要通过工厂类来创建对象,而不需要直接调用对象的构造函数。这样可以降低客户端与具体对象之间的耦合度,提高代码的可维护性和可扩展性。
工厂模式根据不同的分类方法可以分为以下几种类型:
简单工厂模式(Simple Factory Pattern):简单工厂模式是最基本的工厂模式,它通过一个工厂类来创建不同类型的对象。根据传入的参数不同,工厂类可以创建不同的产品对象。
工厂方法模式(Factory Method Pattern):工厂方法模式通过定义一个创建对象的接口,但是将具体的对象创建延迟到子类中实现。每个子类都可以根据需要创建自己的对象。
抽象工厂模式(Abstract Factory Pattern):抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要指定具体的类。
简单工厂模式:
优点:实现简单,适用于创建单一类型的对象。对象的创建和使用的分离,客户端只需要知道工厂类和产品类的接口,而不需要关心具体的实现细节。
缺点:当需要添加新的产品时,需要修改工厂类的代码,违反了开闭原则。
工厂方法模式:
优点:工厂方法模式符合开闭原则,可以通过添加新的工厂类来扩展系统,增加新的产品时无需修改已有代码,只需要添加新的工厂类即可。
缺点:每个具体的产品都需要对应一个具体的工厂类,每增加一个产品都需要增加一个具体的工厂类
抽象工厂模式:
优点:抽象工厂模式可以创建一系列相关的产品对象,客户端只需要知道抽象工厂和抽象产品的接口,而不需要关心具体的实现类。
缺点:当需要添加新的产品族时,需要修改抽象工厂的接口和所有的具体工厂类,违反了开闭原则。
综上所述,简单工厂模式适用于创建单一类型的对象,工厂方法模式适用于创建一种类型的对象,抽象工厂模式适用于创建一系列相关的对象。根据具体的需求和设计要求,选择合适的工厂模式可以提高代码的可扩展性和灵活性。
工厂方法代码实现
interface Product
{
};
class ConcreteProduct:public Product
{
public:
ConcreteProduct(void);
};
ConcreteProduct::ConcreteProduct(void)
{
Print("The concrete product: ",&this," created");
}
class Creator
{
public:
virtual Product* FactoryMethod(void)=0;
void AnOperation(void);
~Creator(void);
protected:
Product* product;
};
Creator::~Creator(void) {delete product;}
void Creator::AnOperation(void)
{
Print("The creator runs its operation");
delete product;
product=FactoryMethod();
Print("The creator saved the product that received from the virtual factory method");
}
class ConcreteCreator:public Creator
{
public:
Product* FactoryMethod(void);
};
Product* ConcreteCreator::FactoryMethod(void)
{
Print("The creator runs the factory method");
Print("The concrete creator creates and returns the new concrete product");
return new ConcreteProduct;
}
class Client
{
public:
string Output(void);
void Run(void);
};
string Client::Output(void) {return __FUNCTION__;}
void Client::Run(void)
{
Print("requests to make the creator");
ConcreteCreator creator;
Print("requests the creator to run its factory method to return the product");
Product* product=creator.FactoryMethod();
Print("requests the creator to run its operation");
creator.AnOperation();
delete product;
}
抽象工厂代码实现
// 抽象产品A接口
interface AbstractProductA
{
};
// 抽象产品B接口
interface AbstractProductB
{
void Interact(AbstractProductA*);
};
// 抽象工厂接口
interface AbstractFactory
{
AbstractProductA* CreateProductA(void);
AbstractProductB* CreateProductB(void);
};
// 实际产品A1类
class ProductA1:public AbstractProductA
{
public:
ProductA1(void);
};
void ProductA1::ProductA1(void)
{
Print("Product A1 is constructed");
}
// 实际产品A2类
class ProductA2:public AbstractProductA
{
public:
ProductA2(void);
};
void ProductA2::ProductA2(void)
{
Print("Product A2 is constructed");
}
// 实际产品B1类
class ProductB1:public AbstractProductB
{
public:
ProductB1(void);
void Interact(AbstractProductA*);
};
void ProductB1::ProductB1(void)
{
Print("Product B1 is constructed");
}
void ProductB1::Interact(AbstractProductA*src)
{
Print("Product B1: ",&this," is interacting with Product A: ",src);
}
// 实际产品B2类
class ProductB2:public AbstractProductB
{
public:
ProductB2(void);
void Interact(AbstractProductA*);
};
void ProductB2::ProductB2(void)
{
Print("Product B2 is constructed");
}
void ProductB2::Interact(AbstractProductA*src)
{
Print("Product B2: ",&this," is interacting with Product A: ",src);
}
// 实际工厂1
class Factory1:public AbstractFactory
{
public:
Factory1(void);
AbstractProductA* CreateProductA(void);
AbstractProductB* CreateProductB(void);
};
void Factory1::Factory1(void)
{
Print("Factory 1: ",&this," is constructed");
}
AbstractProductA* Factory1::CreateProductA(void)
{
Print("Factory 1 creates and returns Product A1");
return new ProductA1;
}
AbstractProductB* Factory1::CreateProductB(void)
{
Print("Factory 1 creates and returns Product B1");
return new ProductB1;
}
// 实际工厂2
class Factory2:public AbstractFactory
{
public:
Factory2(void);
AbstractProductA* CreateProductA(void);
AbstractProductB* CreateProductB(void);
};
void Factory2::Factory2(void)
{
Print("Factory 2: ",&this," is constructed");
}
AbstractProductA* Factory2::CreateProductA(void)
{
Print("Factory 2 creates and returns Product A2");
return new ProductA2;
}
AbstractProductB* Factory2::CreateProductB(void)
{
Print("Factory 2 creates and returns Product B2");
return new ProductB2;
}
// 工厂客户端
class FactoryClient
{
public:
void Run(void);
void Switch(AbstractFactory*);
FactoryClient(AbstractFactory*);
~FactoryClient(void);
protected:
AbstractProductA* apa;
AbstractProductB* apb;
AbstractFactory* factory;
void Delete(void);
};
void FactoryClient::FactoryClient(AbstractFactory* af)
{
Print("Factory client created and received Abstract Factory ",af);
Print("Factory client requests to accept/switch the factories");
Switch(af);
}
void FactoryClient::~FactoryClient(void)
{
Delete();
}
void FactoryClient::Run(void)
{
Print("Factory client runs the abstract Product B");
apb.Interact(apa);
}
void FactoryClient::Delete(void)
{
delete apa;
delete apb;
delete factory;
}
void FactoryClient::Switch(AbstractFactory *af)
{
string sFactory;
StringConcatenate(sFactory,sFactory,factory);
int iFactory=(int)StringToInteger(sFactory);
if(iFactory>0)
{
Print("Factory client switches the old factory ",factory," to the new one ",af);
}
else
{
Print("Factory client accepts the new factory ",af);
}
Delete();
factory=af;
Print("Factory client saved the new factory");
Print("Factory client requests its new factory to create the Product A");
apa=factory.CreateProductA();
Print("Factory client requests its new factory to create the Product B");
apb=factory.CreateProductB();
}
// 客户端
class Client
{
public:
string Output(void);
void Run(void);
};
string Client::Output(void) {return __FUNCTION__;}
void Client::Run(void)
{
Print("The client requests to create the Factory 1");
Print("The client requests to create the Factory client");
Print("The client requests the Factory client to manage the Factory 1");
FactoryClient client(new Factory1);
Print("The client requests the Factory client to operate");
client.Run();
Print("The client requests to create the new factory 2 and asks the factory client to switch factories");
client.Switch(new Factory2);
Print("The client requests the Factory client to run again");
client.Run();
}