是为所有类创建Singleton模式的好方法吗? [英] Is good approach to create Singleton pattern for all class?

查看:67
本文介绍了是为所有类创建Singleton模式的好方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是模式方法的新手。为所有类创建Singleton模式的好方法是什么?

I'm new to pattern approach.Is good approach to create Singleton pattern for all class?

推荐答案

当然不是。

Singleton设计模式有其用途。

来自维基百科 [ ^ ]页面:


No, of course.
The Singleton design pattern has its uses.
From the very Wikipedia[^] page:

单例模式是一种设计模式,它将类的实例化限制为一个对象。 当需要一个对象来协调整个系统的动作时,这很有用。
the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.





如果你需要多个一个类的实例,那么Singleton是没用的。



Rougly speaking, if you need multiple instances of a class, then the Singleton is useless.


一个很大的号码。没有一个尺寸适合所有。



Singleton候选人必须满足三个要求:

1.)控制对共享资源的并发访问。

2.)将从多个请求访问资源,系统的不同部分。

3.)只能有一个对象。
A big No. There is no one size fits all.

A Singleton candidate must satisfy three requirements:
1.) controls concurrent access to a shared resource.
2.) access to the resource will be requested from multiple, disparate parts of the system.
3.) there can be only one object.


这篇关于是为所有类创建Singleton模式的好方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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