工厂和模式如何关联?

发布于 2024-08-20 18:09:16 字数 110 浏览 3 评论 0原文

我刚刚读了一篇关于 SO 的帖子,讨论单例与静态类的优点。

有些人提到模式X似乎更像是一个“工厂”而不是单例“模式”。

“工厂”和“设计模式”有什么区别?

I was just reading a thread on SO that was discussing the merits of Singleton vs. Static Classes.

Some people mentioned that pattern X appeared to be more of a 'factory' rather than a Singleton 'pattern'.

What are the differences between a 'factory' and a 'design pattern'?

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

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

发布评论

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

评论(9

℉服软 2024-08-27 18:09:16

“工厂”是一种特定的设计模式:
http://en.wikipedia.org/wiki/Factory_method_pattern

类似地,“singleton”也是一个设计模式:
http://en.wikipedia.org/wiki/Singleton_pattern

A "factory" is a specific design pattern:
http://en.wikipedia.org/wiki/Factory_method_pattern

Similarly "singleton" is also a design pattern:
http://en.wikipedia.org/wiki/Singleton_pattern

会发光的星星闪亮亮i 2024-08-27 18:09:16

工厂和单例是众多设计模式中的一些。

工厂模式可以实现为生成对象的单例模式。工厂也可以是实例化类,因此不是单例。同样,单例可以是工厂,但也可以是其他东西,例如全局设置管理器或事件注册表。

Factories and Singletons are some of the many design pattern.

A factory pattern can be implemented as a singleton pattern that produces objects. A factory could also be an instanced class, and therefore not a singleton. Likewise, a singleton can be a factory, but it can also be something else, like a global settings manager or event registry.

浪推晚风 2024-08-27 18:09:16

“工厂”而不是单例
'模式'

让我充实一下并正确放置引号:

是“工厂模式”而不是“单例模式”

两者都是设计模式

a 'factory' rather than a Singleton
'pattern'

Let me flesh that out and place the quotation marks correctly:

a 'factory pattern' rather than a 'singleton pattern'

Both are design patterns.

拥醉 2024-08-27 18:09:16

工厂是一个设计模式 - 而不是相反。

A Factory is a Design Pattern - not the other way around.

甜`诱少女 2024-08-27 18:09:16

你有点错了。 “工厂”也是一种模式,与“单例”形成对比。

You got it slightly wrong. "Factory" is a pattern too and contrasts here with "Singleton".

薄暮涼年 2024-08-27 18:09:16

“工厂”是一种设计模式。您可以在此处查看抽象工厂或工厂方法此处 根据上下文

'Factory' is a type of design pattern. You can see a couple examples of the Abstract Factory here or the Factory Method here based on the context

铃予 2024-08-27 18:09:16

工厂一种设计模式:-)就像单例一样。有人可能会说单例是一种工厂。它在需要时创建一个对象,并使用设定的缓存策略(一旦创建,它总是返回相同的对象),但这是学术性的,并且通常在大多数关于结构的辩论中会令人困惑

factory is a design pattern :-) as is Singleton. one could argue that singleton is kind of a factory. It creates an object when needed and uses a set chaching policy (which is always return the same object once it has been creaqted) but that's achedemic and would generally just be confusing in most debates about structure

所谓喜欢 2024-08-27 18:09:16

工厂是一种设计模式。基本上,工厂根据调用类的需要返回一个类。工厂返回的所有类应该共享相同的接口,以便您可以在它们上调用相同的公共方法(尽管每个类实现该方法的方式可能不同)。

这是一个很好的链接 http://en.wikipedia.org/wiki/Factory_method_pattern

A factory is a type of design pattern. Basically a factory returns a class dependant on the needs of the calling class. All classes returned by the factory should share the same interface so you can invoke the same public methods over them (although how each class implements the method may be different).

Here's a good link http://en.wikipedia.org/wiki/Factory_method_pattern

风吹雨成花 2024-08-27 18:09:16

有很多答案,但似乎没有一个能够真正很好地区分这两种模式。让我试试看是否不能让问题变得更加混乱。

单例是一种限制系统只能创建给定类的一个实例的模式。该限制通常通过创建一个工厂来实现,该工厂将创建该类的实例(如果尚不存在)或在后续调用中返回已创建的实例。

工厂用于创建单例以及其他情况。在很多情况下可以用来代替“new”。优点之一是您可以编写工厂以允许返回的对象类型为“Set”。这样,您的测试框架就可以“设置”一个模拟对象而不是真实的对象,然后系统的其余部分将使用该模拟对象。

另一种情况可能是让工厂根据要返回的类型的参数或数据(可能是 XML)进行评估。它们还用于实现依赖注入,工厂查看您的需求并构建对象链来满足这些需求。

Lots of answers, but none seem to actually differentiate between the two patterns well. Let me try and see if I can't confuse issue more.

A Singleton is a pattern that restricts your system to creating only one instance of a given class. The restriction is usually implemented by creating a Factory that will either create an instance of the class (if none already exist) or return the already-created instance on subsequent calls.

A factory is used to create singletons and in other situations. It can be used to replace "new" in many cases. One advantage is that you can write your factory to allow the type of object to be returned to be "Set". This way your testing framework can "set" a mock object instead of the real one--and the rest of your system will then use the mock object.

Another case might be to have the factory evaluate from parameters which type to return, or from data (perhaps XML). They are also used to implement Dependency Injection where the factory looks at what you need and builds chains of objects to fulfill those needs.

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