什么是单身人士以及为什么我们必须使用 [英] What is singleton and why we have to use

查看:109
本文介绍了什么是单身人士以及为什么我们必须使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是Singleton设计模式的新手,如果可以任何人解释或快速了解什么是Singleton设计模式以及我应该如何使用简单的网络应用程序或任何东西。



我尝试过:



尝试使用ONLINE EXAMPLE但是没有理解

Hi all,

i'm new to Singleton design pattern, if can ANYONE EXPLAINS or quick understanding of what is Singleton design pattern and how should i use for simple web application or anything.

What I have tried:

Tried with ONLINE EXAMPLE but did not understood

推荐答案

单例的想法很简单:它是一个带有私有构造函数的类,所以它不能在实例化之外进行实例化类。这意味着该类绝对可以控制创建该类的实例数以及创建它们的时间。单例类创建自己的单个实例,它返回到调用类,需要通过特定方法为此目的访问它。



我个人认为它们是一个美化的全局变量,它打破了整个OOP范式,并且不使用它们 - 但我已经看到它们很好地用于单一资源系统,其中有一个硬件对象需要由多个类合理地使用和/或者线程。



这些天一般被认为是一种诅咒...
The idea of a singleton is very simple: it's a class with a private constructor, so it cannot be instantiated form outside the class. This means that the class is absolutely in control of how many instances of the class are created, and when they are created. A singleton class creates a single instance of itself, which it returns to calling classes that need to access it via a specific method for that purpose.

Personally, I think of them as a glorified global variable which kinda breaks the whole OOPs paradigm, and don't use them - but I have seen them used well for single-resource systems where there is one hardware object which needs to be used sensibly by multiple classes and /or threads.

Generally considered an anathema these days ...


我将从其维基百科页面开始:单身人士模式 - 维基百科 [ ^ ]。
I would start form its very Wikipedia page: Singleton pattern - Wikipedia[^].


这篇关于什么是单身人士以及为什么我们必须使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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