我当时正在研究设计模式,并偶然发现了Abstract Factory Pattern,它的定义是:
抽象工厂模式说,只定义一个接口或抽象类来创建相关(或相依)对象的家族,但没有指定它们的具体子类,这意味着抽象工厂允许类返回一个类工厂。
但我不能彻底理解它。我甚至看过了此链接和这个问题中给出的一些例子,但是没有任何帮助。
有谁能用一个简单的、真实的Abstract Factory Pattern例子以及我们应该使用这种设计模式的情况来提供一个清晰的解释吗?
发布于 2016-07-21 13:16:22
这是用java实现的抽象工厂pattern.Its的流程。
//创建形状接口和实现者类形状
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}//创建颜色接口及其实现者
public interface Color {
void fill();
}
public class Red implements Color {
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
public class Blue implements Color {
@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}//创建抽象工厂类,它通常是生成接口的类,或者用简单的语言创建一个工厂,它可以生成您要求的任何东西
public abstract class AbstractFactory {
abstract Color getColor(String color);
abstract Shape getShape(String shape) ;
}//创建形状工厂,就像您所知道的一般工厂生产的东西一样。这是一家制造shapes.you的工厂,只要给它你想要的形状的名字,它就会制造它。
public class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
@Override
Color getColor(String color) {
return null;
}
}//色彩工厂.This是生产颜色的工厂。你只要给它一个你想要的颜色的名字,它就会制造它。
public class ColorFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
return null;
}
@Override
Color getColor(String color) {
if(color == null){
return null;
}
if(color.equalsIgnoreCase("RED")){
return new Red();
}else if(color.equalsIgnoreCase("BLUE")){
return new Blue();
}
return null;
}
}//produces .Now这类类似于建造工厂的投资者。给它起个名字,它就会为你建造一家生产它的工厂。
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}
return null;
}
}//这是一个演示类,就像交易商一样,它会要求投资者建造一个形状工厂,然后这个工厂可以制造矩形、方块等。
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
//get shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//get an object of Shape Rectangle
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//call draw method of Shape Rectangle
shape2.draw();
//get an object of Shape Square
Shape shape3 = shapeFactory.getShape("SQUARE");
//call draw method of Shape Square
shape3.draw();
//get color factory
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
//get an object of Color Red
Color color1 = colorFactory.getColor("RED");
//call fill method of Red
color1.fill();
//get an object of Color Blue
Color color3 = colorFactory.getColor("BLUE");
//call fill method of Color Blue
color3.fill();
}
}发布于 2016-07-21 13:18:14
抽象工厂的关键词是“家庭”。下面是我非常简单的例子。
正如你所看到的,老板不需要知道它是什么样的工厂,只要它是VehichleFacotry。Boss只需调用Build()就可以创建相应的车辆。
abstract class Vehicle { }
class Car : Vehicle { }
class Bus : Vehicle { }
abstract class VehicleFactory
{
abstract Vehicle Build();
}
class CarVehicleFactory : VehicleFacory
{
Vehicle Build()
{
return new Car();
}
}
class BusVehicleFactory : VehicleFacory
{
Vehicle Build()
{
return new Bus();
}
}
public class Boss
{
public void Order(VehicleFactory factory)
{
var vehicle = factory.Build();
//do something else ...
}
}
public class Foo
{
public void Bar()
{
var boss = new Boss();
boss.Order(new CarVehicleFactory());
boss.Order(new BusVehicleFactory());
}
}发布于 2016-07-21 13:30:19
假设您正在设计一个页面,其中您需要从服务器(带有分页)获取数据,触发一些分析事件并具有可定制的视图。
现在,您希望此页面足够通用,供任何希望使用相同功能集的人使用。
那么有什么不同呢?
您可以封装不同的内容,并将它们放入不同的类中。就像这样
class MyPage {
IDataFetcher datafetcher;
IAnalyticsHelper analyticsHelper;
IMenuBuilder menuBuilder;
}现在,您的MyPage类依赖于这些类来呈现页面。但是,如果你仔细观察,这些是算法家族,他们将一起工作,以呈现一个页面。(一个很大的暗示,你可以使用抽象工厂)。
因此,您可能可以将代码更改为:
public interface IPageAbstractFactory {
IDataFetcher getDatafetcher();
IAnalyticsHelper getAnalyticsHelper();
IMenuBuilder getMenuBuilder();
}
class MyPage {
IPageFactory factory;
}我们刚刚实现了一个抽象工厂!我希望我的例子是清楚的。
https://stackoverflow.com/questions/38504837
复制相似问题