简体   繁体   English

参数化单例模式

[英]Parameterized singleton patterns

The link over here lists ([http://www.yoda.arachsys.com/csharp/singleton.html][1]) some singleton patterns in C#. 这里的链接列出了([http://www.yoda.arachsys.com/csharp/singleton.html] [1])C#中的一些单例模式。 The article also describes the obvious that a singleton is not meant to accept parameters which “as otherwise a second request for an instance but with a different parameter could be problematic”. 该文章还明确地描述了单例并不意味着接受参数“否则对实例的第二次请求但具有不同的参数可能会有问题”。 This means that any parameters you need to get the class working should be induced as a property. 这意味着您需要使用该类所需的任何参数作为属性。

I am curious to know if there are any parameterized singleton design patterns out there. 我很想知道是否有任何参数化的单例设计模式。 Accepting values as a property does not enforce anything to the consumer. 接受值作为属性不会对消费者强制执行任何操作。

Based on your question, it seems you may be looking at an Abstract Factory pattern (creates an instance of several families of classes) that keeps an internal list/dictionary of classes that have already been instantiated, thus mimicking the singleton pattern functionality. 根据您的问题,您可能正在查看抽象工厂模式(创建几个类系列的实例),该模式保留已经实例化的类的内部列表/字典,从而模仿单例模式功能。

You would then use this factory class to request an object based on parameters you've passed in, and if it exists in its internal list it gets returned, and if not, a new instance is created and then added to the list and returned. 然后,您将使用此工厂类根据您传入的参数请求对象,如果它存在于其内部列表中则返回,如果不存在,则创建一个新实例,然后将其添加到列表中并返回。

his means that any parameters you need to get the class working should be induced as a property. 他的意思是,你需要让所有类工作的参数作为一个属性。

Ideally singleton class should not depend on external code. 理想情况下,单例类不应该依赖于外部代码。

In case when you need to provide additional information to singleton constructor, you can just create a pool of objects. 如果您需要向单例构造函数提供其他信息,则可以只创建一个对象池。

It can be a simple list or any other suitable data structure. 它可以是简单列表或任何其他合适的数据结构。 You will need to make it thread-safe (if it matters) and guarantee that there will not be multiple objects instantiated with the same parameters. 您需要使其成为线程安全的(如果重要)并保证不会有多个对象使用相同的参数进行实例化。

Basically you will have a class factory. 基本上你会有一个班级工厂。 It will return the same object for the same parameters. 它将为相同的参数返回相同的对象。

In this case you will have N singleton objects - ie objects with different state will be treated as completely different instances. 在这种情况下,您将拥有N个单例对象 - 即具有不同状态的对象将被视为完全不同的实例。

You can find examples of such singletons in Inversion of Controls containers. 您可以在Inversion of Controls容器中找到此类单例的示例。

For example you can have some service that depends on other services. 例如,您可以拥有一些依赖于其他服务的服务。 When you call container.Get(type of service). 当你调用container.Get(服务类型)时。 DI container will automatically initialize service instance with required parameters and return it to caller. DI容器将使用所需参数自动初始化服务实例并将其返回给调用者。 But this service instance becomes singleton - you will not be able to create another service with the same parameters. 但是这个服务实例变成了单例 - 你将无法使用相同的参数创建另一个服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM