在哪种情况下,使用工厂类而不是静态函数是有意义的? [英] In which cases it makes sense to use factory classes instead of static functions?

查看:107
本文介绍了在哪种情况下,使用工厂类而不是静态函数是有意义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我已经创建了一个ABCFactory类,它具有创建ABC对象的单一方法。现在我想到了,可能不是有一个工厂,我可以在我的ABC方法中做一个静态的方法。 pro和con的改变是什么?会不会导致相同的?我没有预见有其他类继承ABC,但一个从来不知道!

Currently I have created a ABCFactory class that has a single method creating ABC objects. Now that I think of it, maybe instead of having a factory, I could just make a static method in my ABC Method. What are the pro's and con's on making this change? Will it not lead to the same? I don't foresee having other classes inherit ABC, but one never knows!

谢谢

推荐答案

在现实中,如果你想获得工厂类的好处,你需要在自己的类中使用静态方法。这将允许您以后创建新的工厂类,或重新配置现有的类,以获得不同的行为。例如,一个工厂类可能会创建实现IFourHoovedAnimal接口的独角兽。您可能会编写一个使用IFourHoovedAnimal进行操作的算法,并需要实例化它们。后来你可以创建一个新的工厂类,而不是实例化飞马座,它也实现了IFourHoovedAnimal的。旧的算法现在可以通过使用新的工厂重新用于飞马座!为了使这个工作,PegasusFactory和UnicornFactory都必须从一些常见的基类(通常是一个抽象类)继承。

In reality, if you want to get the benefits of a factory class, you need the static method in it's own class. This will allow you to later create new factory classes, or reconfigure the existing one to get different behaviors. For example, one factory class might create Unicorns which implement the IFourHoovedAnimal interface. You might have an algorithm written that does things with IFourHoovedAnimal's and needs to instantiate them. Later you can create a new factory class that instead instantiates Pegasus's which also implement IFourHoovedAnimal's. The old algorithm can now be reused for Pegasus's just by using the new factory! To make this work both the PegasusFactory and the UnicornFactory must inherit from some common base class(usually an abstract class).

所以你可以通过将静态方法放在自己的类中工厂课程,您可以使用较新的工厂类别来更换旧的算法。这也有助于提高可测性,因为现在可以为单元测试提供一个创建模拟对象的工厂。

So you see by placing the static method in it's own factory class, you can swap out factory classes with newer ones to reuse old algorithms. This also works for improving testability, because now unit tests can be fed a factory that creates mock objects.

我之前已经做了这个(在工厂的静态工厂方法您正在为非常小的项目创建实例),但这只是因为我需要它来帮助重构一些旧代码,而是将更改保持在最低限度。基本上在这种情况下,我已经考虑了一大堆代码,创建了一堆ASP.NET控件,并将所有这些控件放入用户控件。我想使我的新的用户控件属性为基础,但是旧的旧代码更容易创建基于参数的构造函数的用户控件。

I have done the latter before (static factory method on the class that you are creating instances of) for very small projects, but it was only because I needed it to help refactor some old code, but keep changes to a minimum. Basically in that case I had factored out a chunk of code that created a bunch of ASP.NET controls, and stuff all those controls into a user control. I wanted to make my new user control property based, but it was easier for the old legacy code to create the user control with a parameter based constructor.

所以我创建了一个静态工厂方法采取所有参数,然后根据参数实例化用户控件并设置其属性。旧的旧代码使用这种静态方法来创建用户控件,而将来的代码将使用漂亮属性。

So I created a static factory method that took all the parameters, and then instanced the user control and set it's properties based on the parameters. The old legacy code used this static method to create the user control, and future code would use the "prettier" properties instead.

这篇关于在哪种情况下,使用工厂类而不是静态函数是有意义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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