您如何创建工厂? [英] How do you create your Factories?

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

问题描述

因此,谈到工厂问题,我想知道它们的设置方式.

从我的立场,我可以看到三种类型的工厂:

多合一

基本上包含应用程序中使用的所有类的工厂.感觉是为了拥有工厂而只是在拥有工厂,并没有真正的结构化感觉.

示例(其中ClassA,Class B和ClassC除了在同一个App中没有其他共同点之外):

class Factory
{
 public static function buildClassA()
 public static function buildClassB()
 public static function buildClassC()
}

所提供的代码示例在PHP中.但是,这个问题与语言无关.

内置工厂

下一个是将静态函数与常规函数混合,以形成特殊的创建模式(请参见解决方案

工厂的要点似乎是拥有使用它的代码,而无需知道将构造哪个具体类(应通过配置工厂).似乎排除了多合一"和边上工厂".

我喜欢Java库经常使用的方法:您有一个创建Factory的静态方法. Factory具有创建实例的getInstance方法.这为您提供了两点配置(通过系统属性):默认的FactoryImpl具有许多设置,例如它应生成的类,并且如果这些配置选项不够用,您也可以完全交换FactoryImpl.

至于多合一"与边上工厂",我认为工厂不应产生不相关的类.同样,用Java术语来说,每个工厂都会生成某个接口的实例.

多合一"听起来应该用依赖注入"(在其中有一个容器可以生成各种实例并将它们注入到应用程序中)代替.

So, coming upon the subject of Factories, I'm wondering how they are set up.

From where I stand, I can see 3 types of Factories:

All In One

A factory that basically contains all of the classes used in an application. It feels like it is just having a factory for the sake of having a factory, and doesn't really feel structured.

Example (Where ClassA, Class B, and ClassC have nothing in common except being in the same App):

class Factory
{
 public static function buildClassA()
 public static function buildClassB()
 public static function buildClassC()
}

Code samples provided are in PHP. However, this question is language-agnostic.

Built-In Factory

The next one is mixing in static functions with the regular functions in order to make special creation patterns (see this question)

Example:

class ClassA
{
 public static function buildClass()
 public function __construct()
}

Factory On-the-Side

The last one I can think of is having a factory for individual classes, or individual sets of classes. This just seems to variable to be used in an uniform manner.

Example (Where ClassA, B, and C are related, and 1, 2, and 3 are related):

class FactoryAlpha
{
 public static function buildClassA()
 public static function buildClassB()
 public static function buildClassC()
}

class FactoryNumeric
{
 public static function buildClass1()
 public static function buildClass2()
 public static function buildClass3()
}

My question is: Are all of these bad ideas, are any of them bad ideas? Are there other ways of creating factories? Are any of these actually good ideas? What is a good/best way to create Factories.

解决方案

The point of a factory seems to be to have the code that uses it not need to know which concrete class will be constructed (this should be handled by configuring the factory). That seems to rule out "All-in One" and "Factory-on-the-Side".

I like the approach that Java libraries often use: You have a static method that creates the Factory. The Factory has a getInstance method that creates the instance. This gives you two points of configuration (via system properties): The default FactoryImpl has a number of settings, such as the class it should produce, and if these configuration options are not enough, you can also swap out the FactoryImpl altogether.

As for "All-in One" vs "Factory-on-the-Side", a Factory should not produce unrelated classes I think. Again, it Java terms, every factory produces instances of a certain interface.

"All-in-One" sounds like something that should be replaced with Dependency Injection (where you have a container that produces all kinds of instances and injects them into the application).

这篇关于您如何创建工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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