关于设计模式Singleton和Factory [英] About Design Patterns Singleton and Factory

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

问题描述

大家好

我对于在哪里实现Singleton和在哪里使用Factory感到困惑.
请帮助我解决此问题.

Hello All

I become confused where to implement Singleton and where to use the Factory.
Please help me to Solve this issue .

推荐答案

您好

浏览此 design_patterns [ ^ ],您将是个好主意.非常适合初学者.

[更新]
关于stackoverflow.com的类似讨论,
何时使用单例 [何时使用工厂方法? [模式单例 [ ^ ]
模式工厂 [ ^ ]
Hi

Go through this design_patterns[^], You will good idea. Very good one for beginners.

[Update]
Similar discussion on stackoverflow.com,
When to use the singleton[^]
When to use factory methods?[^]

Some other articles,
Pattern Singleton[^]
Pattern Factory[^]


E


w @ r的链接是好一个.简而言之,通常使用Singleton来防止在内存中重复加载大量资源.假设A类有一个可重复使用的大型通用数据集.如果许多其他对象访问该对象的各种实例,那么这将浪费内存,因为同一类型的数据在内存中重复出现.因此,单例模式可以防止它的多个实例.

如果静态成员不满意,也可以使用Singleton来保留全局数据.

工厂本身就是名称,它解释了对象的创建.基本上,如果您在类内部通过硬编码创建对象,则当需要在新条件下创建新类型的对象时,需要对您的类进行编辑.取而代之的是,您可以将对象创建分离到工厂,然后将创建工作委托给工厂.因此,对象创建中的任何更改都会影响工厂类编码,但无需使用工厂来修改类.一旦将新条件传递给工厂,工厂就会知道必须实例化哪个对象才能完成工作.调用方类不必担心必须实例化哪个对象.

这只是一个简短的解释


互联网上有很多好的资料.您可以查看它.
w@r''s link is a good one. In a nutshell Singletons are generally used prevent repeated load of a heavy resource in memory. Say class A has a big common dataset which is reusable. If many other objects access various instance of this object then it is a wastage of memory, because the same type of data repeated in memory. So singleton pattern prevents the multiple instance of it.

Singleton can be used to keep global data as well, if static members not satisfied.

Factory is by the name itself it explains creation of objects. Basically if you create an object by hardcode inside the class then your class need to be edited when a new type of objects need to create in a new condition. Instead of that you can separate the object creation to a factory and delegate the creation work to it. So any changes in the object creation will impact on the factory class coding, but no need to modify the classes which using the factory. Once the new condition is passed to the factory, the factory knows which object has to instantiate to do the job. The caller class doesn''t need to worry about which object has to instantiate.

This is just a nutshell explanation


There are lot of good material available in internet. You can go through it.


这篇关于关于设计模式Singleton和Factory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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