单例模式与工厂模式的争议

发布于 2024-12-02 23:57:32 字数 290 浏览 0 评论 0原文

当对象创建存在复杂性并且复杂性包含创建范围无法访问(不应该访问)的信息或创建包含一些强制性的不可避免的解决方法时,我经常使用工厂模式。

我经常将工厂设为单例,因为不需要多个工厂。将同一个工厂传递给多个类看起来很奇怪。 在参数中传递整个工厂

关于单例模式存在很多争议。那么我应该让 Factory 仍然是 Singleton 吗?

工厂需要能够到达需要工厂生产某种产品的每个角落。这需要将该工厂作为参数传递。并再次将其传递到链中。而且这条链不会是单向的。这将很容易形成分支。这也将导致测试困难。

Often I use factory Pattern when there are complexities around object creations and that complexities incorporates informations inaccessible (should not be accessible)to the creation scope or creation incorporates some mandatory unavoidable workarounds.

Often I make the Factory a Singleton cause there is no need to multiple factories. and passing the same factory to multiple classes looks so odd. passing a whole Factory in parameter

There have been a lot of controversies regarding Singleton Pattern. So Should I make Factory a Singleton still ?

The Factory needs to accessible to every corners that need the factory to produce some product . that requires passing that factory as an argument. and again passing that in a chain. and that chain will not be unidirectional. that will easily make branches. that will also lead to testing difficulties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-12-09 23:57:32

类不必是单例来提供共享实例,并且只有一个实例这一事实并不意味着该类必须是单例。如果您必须拥有不超过一个实例,那就需要使用单例。工厂模式中没有任何内容要求您只有一个工厂——很容易想象有多个工厂,每个工厂都有不同的配置,并且每个工厂都创建不同配置的对象。

A class needn't be a singleton to provide a shared instance, and the fact that you only have one instance doesn't mean that the class must be a singleton. If you must have no more than one instance, that's the place to use a singleton. There's nothing in the Factory pattern that requires that you have only one factory -- it's easy to imagine having several factories each configured differently and each creating differently configured objects.

情深缘浅 2024-12-09 23:57:32

我支持 Doug T。

您可以非常轻松地在类中创建静态函数,该函数可用于创建该类的实例(工厂函数)。更好的是,您可以创建一个工厂类,并让工厂类中的静态函数生成所需的对象。 C# 实际上提供了静态类,其中所有成员和函数都是静态的,以达到这样的目的 - 无论如何,它本质上是一个单例。

这里的要点是,无论您做什么,简单地将函数指针传递给工厂函数作为参数可能更有意义。然后,您将可以灵活地选择存储创建逻辑的位置,并且可以选择完全避免单例/额外的类。

(PS:我认为单例是一个很好的模式,但是如果你过度使用它们,它们就会成为一种反模式,因为它们将设计简化为类似于只有全局函数和数据的东西)。

I'll side with Doug T.

You can very easily create a static function within a class which can be used to create an instance of that class (factory function). Better yet, you can create a factory class, and make the static function in the factory class generate the desired object. C# actually provides static classes where all members and functions are static for purposes like this - it's essentially a singleton anyway.

The point here is, whatever you do, it may make more sense to simply pass a function pointer to a factory function as your parameter. You'll have flexibility with where to store your creation logic then, and you can choose to avoid singletons/extra classes altogether.

(PS: I think singletons are a good pattern, but if you over-use them they become an anti-pattern because they reduce a design to something resembling just having global functions and data).

沉鱼一梦 2024-12-09 23:57:32

不,你不应该这样做,事实上,单例模式经常被视为某人的反模式。

最好的办法是依赖一些 IOC 容器,避免同时使用单例和工厂模式。

No you shouldn't, in fact singleton pattern is often seen almost as an antipattern from someone.

The best thing would be to rely on some IOC container avoiding both singleton both factory pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文