在实现接口的类上强制单例模式 [英] Force Singleton Pattern on a Class implementing an Interface

查看:2269
本文介绍了在实现接口的类上强制单例模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更好地用一个例子来解释这个问题。
我有一个接口模型,可用于访问数据。
可以有不同的模型实现,它们可以表示各种格式的数据,例如XMl,txt格式等。模型不关心格式。
让我们说一个这样的实现是 myxmlModel



现在我想强制 myxmlModel 实现模型遵循 Singleton模式。通常的方法是使 myxmlModels 构造函数为private,并提供一个静态工厂方法来返回myModel的一个实例class。但问题是接口不能有静态方法定义,结果我无法强制执行 Model 的所有实现的特定Factory方法定义。所以一个实现可以通过提供 getObject()结束,而另一个实现可能得到 NewModel()。



解决的办法是允许程序包访问 myxmlModel的构造函数,并创建一个Factory类,它创建 myxmlModel 对象并将其缓存以供进一步使用。



我想知道是否有更好的方法来实现相同的功能。

解决方案



  1. 使同一个包中的模型包私有类
    的所有具体实现都作为一个工厂返回
    实例您的工厂。

  2. 如果您的模型是单身份,并且您使用的是java
    5+,则使用枚举而不是传统的
    单例,因为它更安全。





  public enum MyXMLModel {
INSTANCE ();
//课余
};

编辑:
另一种可能性是创建执行所有工作的代理类,然后使用一个枚举,以提供所有的模型选项。



例如:

  class MyXMLModelDelegate implements Model {
public void foo(){/ * do foo * /}
...
}

class MyJSONModelDelegate implements Model {
public void foo(){/ * do foo * /}
...
}

public enum Models {
XML(new MyXMLModelDelgate() ),
JSON(new MyJSONModelDelegate());

私人模特代表;
public Models(Model delegate){this.delegate = delegate; }

public void foo(){delegate.foo(); }
}


I better explain the question with an example. I have an Interface Model which can be used to access data. There can be different implementations of Model which can represent the data in various format say XMl , txt format etc. Model is not concerned with the formats. Lets say one such implementation is myxmlModel.

Now i want to force myxmlModel and every other implementation of Model to follow Singleton Pattern.The usual way is to make myxmlModels constructor private and provide a static factory method to return an instance of myModel class.But the problem is interface cannot have static method definitions and a result i cannot enforce a particular Factory method definition on all implementation of Model. So one implementation may end with providing getObject() and other may have getNewModel()..

One work around is to allow package access to myxmlModel's constructor and create a Factory class which creates the myxmlModel object and cache it for further use.

I was wondering if there is a better way to achieve the same functionality .

解决方案

  1. Make a factory that returns instances of your interface, Model.
  2. Make all concrete implementations of the model package-private classes in the same package as your factory.
  3. If your model is to be a singleton, and you are using java 5+, use enum instead of traditional singleton, as it is safer.

public enum MyXMLModel{  
INSTANCE();  
//rest of class  
};

EDIT: Another possibility is to create delegate classes that do all the work and then use an enum to provide all of the Model Options.

for instance:

class MyXMLModelDelegate implements Model {
public void foo() { /*does foo*/}
...
}

class MyJSONModelDelegate implements Model {
public void foo() { /*does foo*/ }
...
}

public enum Models {
XML(new MyXMLModelDelgate()),
JSON(new MyJSONModelDelegate());

private Model delegate;
public Models(Model delegate) { this.delegate=delegate; }

public void foo() { delegate.foo(); }
}

这篇关于在实现接口的类上强制单例模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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