了解工厂方法模式 [英] Understanding the factory method pattern

查看:107
本文介绍了了解工厂方法模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关工厂方法模式的信息。



我可以理解何时只有一个工厂类,即 StoreFactory#getStore(),它返回一个 Store 实现基于某个运行时或其他状态。



但是,通过阅读(例如此链接),似乎人们通常会创建一个抽象工厂类,其他工厂类也会扩展到该类:

 公共抽象类AbstractFactory {
public abstract Store getStore(int store);
}

公共类StoreFactoryA扩展AbstractFactory {
public Store getStore(int Store){
if(Store == 1){返回新的MyStoreImplA(); }
if(Store == 2){return new MyStoreImplB(); }
}
}

公共类StoreFactoryB扩展AbstractFactory {
public Store getStore(int Store){
if(Store == 1){返回新的MyStoreImplC(); }
if(Store == 2){return new MyStoreImplD(); }
}
}

公共类Runner {
public static void main(String [] args){
AbstractFactory storeFactory = new StoreFactoryA();
Store myStore = storeFactory.getStore(1);
}
}

我的示例是人为设计的,但模型与上述相同链接。



对我来说,这种实现似乎有点像鸡蛋。使用Factory Method模式可以消除客户代码指定类类型的需要,但是现在客户代码需要有选择地选择要使用的正确工厂,即 StoreFactoryA StoreFactoryB



在这里使用抽象类的背后原因是什么?

解决方案

不幸的是,您正在阅读的链接没有提供该模式的实际示例。实际上,按照原始的GoF设计模式,该模式称为 Abstract Factory (工厂方法是不同的模式)。



Abstract Factory如果您的工厂可以创建对象族,则使用模式。例如您可以使用 AbstractGUIFactory ,其中可以使用方法 createButton(),createWindow(),createTitleBar 等。然后,您将有具体工厂,例如 WindowsGUIFactory,MacGUIFactory,MotifGUIFactory 等,每个工厂都会在其中生成 Button,Window,TitleBar 对象自己的方式。



工厂将在应用程序中的某个位置设置为一种实现(可能使用配置),然后将在需要创建对象的任何地方使用该工厂。 / p>

如果您正在学习设计模式,最好的建议是从经典的GoF书开始。


I'm reading about the Factory Method pattern.

I can understand when there is a single factory class, i.e. StoreFactory#getStore(), that returns a Store implementation based on some runtime or other state.

But, from reading (e.g. this link), there seems to be a general pattern of people creating an abstract factory class, to which other factory classes extend:

 public abstract class AbstractFactory {
     public abstract Store getStore(int store);
 }

 public class StoreFactoryA extends AbstractFactory {
     public Store getStore(int Store) {
         if(Store == 1) { return new MyStoreImplA(); }
         if(Store == 2) { return new MyStoreImplB(); }
     }
 }

 public class StoreFactoryB extends AbstractFactory {
     public Store getStore(int Store) {
         if(Store == 1) { return new MyStoreImplC(); }
         if(Store == 2) { return new MyStoreImplD(); }
     }
 }

 public class Runner {
    public static void main(String[] args) {
        AbstractFactory storeFactory = new StoreFactoryA();
        Store myStore = storeFactory.getStore(1);
    }
 }

My example is contrived, but models that of the aforementioned link.

This implementation seems kind of chicken-egg to me. Use the Factory Method pattern to eliminate the need for the client code to specify a class type, yet now the client code needs to selectively choose the correct factory to use, i.e. StoreFactoryA, StoreFactoryB?

What is the reasoning behind using the abstract class here?

解决方案

The link you are reading unfortunately does not give a realistic example of the pattern. In fact, as per the original GoF Design Patterns, this pattern is called Abstract Factory (Factory method is a different pattern).

Abstract Factory pattern is used when you have factories that can create a family of objects. e.g. You can have and AbstractGUIFactory which can have methods createButton(), createWindow(), createTitleBar etc. And then, you would have concrete factories like WindowsGUIFactory, MacGUIFactory, MotifGUIFactory etc, each of which will produce Button, Window, TitleBar objects in their own way.

The factory will be set to one implementation at some point in the application (probably using configuration) and then that factory will be used wherever the objects need to be created.

If you are learning Design Patterns, the best advice is to start with the classic GoF book.

这篇关于了解工厂方法模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆