什么是单例类?它能帮助我运行单个实例的一个类的两个相关的服务? [英] what is a singleton class? Can it help me running single instance of a class for two related services?

查看:167
本文介绍了什么是单例类?它能帮助我运行单个实例的一个类的两个相关的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来很复杂,但我会问:

This might sound complex but i will ask anyway:

我正在运行 A 强>。我想开始使用除新类之外的类 A 的另一个服务 B

I am running a service A which uses class X. I want to start another service B which uses classes A besides new classes.

服务 A 已在运行。我执行Service B 的热部署。

Service A is already running. I do a hot deployment of Service B.

这是真正的问题 - Will Service B 使用同一个类 X 或单独的实例。

Here is the real question - Will Service B use the same instance of class X or a separate instance. How can singleton class help me here?

推荐答案

每个服务都将运行在自己的操作系统(OS)进程空间,进程空间有它自己的类实例。 singleton类通常使用类中的静态字段进行编码,这对于代码运行的进程空间是本地的,所以不会,它们不会共享单例。

Each Service will run in it's own operating System (OS) Process space, and each process space has it's own class instances. A "singleton" class is normally coded using static fields in a class, which would be local to the process space the code was running in, so no, they will not share singletons. Each will get it's own instance.

你可以做你想做的事情,但是使用一些外部的共享同步过程(例如, java等效是指.Net Remoting(或WCF) - 一个网络暴露的端点,它被编码为使用Singleton,并且使您的服务连接到并使用可远程访问的Singleton)

You can do what you are trying to do, however, using some external shared synchronization process, (for example, exposing a singleton over whatever the java equivilent is to .Net Remoting (or WCF) - a network exposed endpoint which is coded to use a Singleton, and have both your services "connect to" and use that remotely accessible Singleton)

这篇关于什么是单例类?它能帮助我运行单个实例的一个类的两个相关的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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