一、简单工厂模式
工厂模式(Factory Pattern)主要用于选择,根据客户不同的要求,使用同一个接口实现不同的功能,然后使用工厂类来判断使用哪个实例接口,工厂模式是一个最佳的创建对象的模式。
工厂模式的优点:
1、在调用者只需要知道需要创建的名称就能够创建一个对象,而不用知道产品功能的具体实现,简化了调用者的工作。
2、利于扩展,当我们需要扩展当前类型的功能时,只需要修改工厂类,使工厂类能够创建当前对象。
工厂模式的缺点 :
每当重新创建一个新的类时,就需要在工厂类中实例化对象,还需要重新创建一个类,增加了系统的复杂度,还增加了类之间的依赖性。
二、工厂类的具体实现
1、工厂类的实现模型
Shape : 抽象类,提供具体具体的接口,提供给调用这调用
Circle 、Square 、Rectangle :为Shape的派生类,通过继承实现接口功能,不同的派生类提供不同的功能。
ShapeFactory :工厂类用于管理实例对象的创建,工厂类根据接口调用者提供的需要创建的子类信息,来创建一个子类的实例并返回提供给开发者使用。
2、工厂类的C++代码实现源码 :
#include <stdio.h>
#include <iostream>
class FactoryPattern
{
public:
virtual void Draw()=0;
};
class Circil :public FactoryPattern
{
public:
void Draw();
};
void Circil::Draw()
{
printf("当前对象为 : Circil");
}
class Square :public FactoryPattern
{
public:
void Draw();
};
void Square::Draw()
{
printf("当前对象为 :Square ");
}
//工厂管理类
class ShapeFactory
{
public:
FactoryPattern * GetShaper(char * name);
};
FactoryPattern * ShapeFactory::GetShaper(char *name)
{
//判断创建哪个对象
if(name==nullptr)
{
return nullptr;
}
if (name=="Circil")
{
return new Circil();
}
if (name=="Square")
{
return new Square();
}
return nullptr;
}
void main(void)
{
ShapeFactory * shap=new ShapeFactory();
FactoryPattern * sha=shap->GetShaper("Circil");
sha->Draw();
FactoryPattern * wang=shap->GetShaper("Square");
wang->Draw();
getchar();
return;
}
运行结果:
三、抽象工厂模式
当在一个工厂中具有多个完整的产品且同一个产品由具有不同的型号,抽象工厂用于管理下层的产品的创建工厂,即抽象工厂是创建工厂上的工厂。抽象工厂的作用就是选择创建某个产品的管理工厂对象,然后通过工厂管理对象创建具体的实例。
抽象工厂模式的优点:
当一个产品系列中的不同产品对象实例一起工作时,在客户端都只是用同一个实例对象。调用者能够跟简单的创建多个对象进行协同工作。
抽象工厂模式的缺点;
当需要在产品族中新增加一个产品是,需要更改工厂类中的代码,又要添加类实现代码,大大的增加的产品的代码量。
四、抽象工厂模式的具体实现
1、工厂管理的的是实现模型
Shape :作为一个产品族中的父类提供给抽象工厂管理类调用的接口,Circle、Squar、Rectangle作为派生类进行接口类的具体实现。Color和Shape同样的功能
ShapeFactory作为Shape产品族工厂管理类,管理派生类对象实例的创建。ColorFactory同ShapeFactory功能相同。
AbstractFactory :抽象类,这里的接口通过形参获取外界创建工厂管理类的实例。工厂管理类通过继承这个类实现接口。
FactoryProducer:作为一个抽象工厂管理类,通过传递信息来确定创建那个工厂管理类。
2、抽象工厂模式的C++实现:
#include <stdio.h>
class FactoryPattern
{
public:
virtual void Draw()=0;
};
class Circil :public FactoryPattern
{
public:
void Draw();
};
void Circil::Draw()
{
printf("当前对象为 : Circil");
}
class Square :public FactoryPattern
{
public:
void Draw();
};
void Square::Draw()
{
printf("当前对象为 :Square ");
}
class Color
{
public:
virtual void fuller()=0;
};
class Green :public Color
{
public:
void fuller();
};
void Green::fuller()
{
printf("This is Green\n");
}
class Red :public Color
{
public:
void fuller();
};
void Red::fuller()
{
printf("This is Red\n");
}
//对象管理工厂基类
class AbstractFactory
{
public:
virtual Color* GetColor(char * name)=0;
virtual FactoryPattern *GetFactoryPattern(char * name)=0;
};
//shape工厂管理
class ShaperFactory : public AbstractFactory
{
public:
FactoryPattern * GetFactoryPattern(char*name)
{
if(name==nullptr)
{
return nullptr;
}
if(name=="Circil")
{
return new Circil();
}
if(name=="Square")
{
return new Square();
}
return nullptr;
}
Color*GetColor(char*name)
{
return nullptr;
}
};
//Color工厂管理
class ColorFactory :public AbstractFactory
{
public:
Color*GetColor(char*name)
{
if (name==nullptr)
{
return nullptr;
}
if(name=="Red")
{
return new Red();
}
if(name=="Green")
{
return new Green();
}
return nullptr;
}
FactoryPattern *GetFactoryPattern(char * name)
{
return nullptr;
}
};
//管理工厂的工厂
class FactoryProducer
{
public:
static AbstractFactory*GetFactory(char*name)
{
if(name=="SHAPE")
{
return new ShaperFactory();
}
if (name=="COLOR")
{
return new ColorFactory();
}
return nullptr;
}
};
void main(void)
{
//创建shaper功能管理工厂
AbstractFactory *shaperfac=FactoryProducer::GetFactory("SHAPE");
FactoryPattern * shap1=shaperfac->GetFactoryPattern("Circil");
shap1->Draw();
FactoryPattern * shap2=shaperfac->GetFactoryPattern("Square");
shap2->Draw();
shaperfac=FactoryProducer::GetFactory("COLOR");
Color *color1=shaperfac->GetColor("Red");
color1->fuller();
Color * color2=shaperfac->GetColor("Green");
color2->fuller();
getchar();
return;
}
运行结果为 :
本文详细介绍了工厂模式和抽象工厂模式的概念及其实现方式。通过具体的C++代码示例,展示了如何利用工厂模式简化对象创建过程,并探讨了两种模式的优缺点。
1512

被折叠的 条评论
为什么被折叠?



