简体   繁体   English

不使用单例的类之间共享数据的方法

[英]A way to share data between classes not using singleton

I am working on library that is used in application (as Maven artifact). 我正在研究在应用程序中使用的库(作为Maven工件)。 In my current situation library has a main class that provides static methods for working with it. 在我当前的情况下,库有一个主类,提供了使用它的静态方法。 Therefore it allows only "one instance" work mode with this library. 因此,该库仅允许“一个实例”工作模式。 At the beginning of work it is necessary to provide Configuration object to this main class. 在工作开始时,有必要向该主类提供Configuration对象。 Such as MainClass.start(new Configuration()) and later on this configuration object is accessed by other classes via singleton factory. 例如MainClass.start(new Configuration())及更高版本,其他类可通过singleton工厂访问此配置对象。

My problem currently is that I need to change library in a way that it should allow multi-instance mode. 我目前的问题是我需要以允许多实例模式的方式更改库。 Such that MainClass won't have any more static methods, and will have to be instantiated. 这样MainClass将不再有静态方法,而必须实例化。 What I stumble upon is how do I then provide each instance with its unique Configuration in a way that instances of other classes (those that are used together with current instance of MainClass) can also access it while not having direct access to instance of MainClass. 我偶然发现的是如何为每个实例提供其唯一的Configuration ,以使其他类的实例(那些与MainClass的当前实例一起使用的实例)也可以在不直接访问MainClass实例的情况下访问它。 Of course in this scenario Singleton isn't a solution because two or more MainClass instances should have each own configuration. 当然,在这种情况下,Singleton不是解决方案,因为两个或多个MainClass实例应该具有各自的配置。

class Configuration{
 String a;
 String b;

 public Configuration(String a, String b){
  this.a = a;
  this.b = b;
 }

 public String getA(){
  return a;
 }

 public String getB(){
  return b;
 }

}


class MainClassFactory{
 private final static MainClassFactory INSTANCE = new MainClassFactory();
 private ConcurrentHashMap<String,MainClass> mainClassMap = new ConcurrentHashMap<String,MainClass>();
 public static getInstance(){
  return INSTANCE;
 }

 public MainClass getMainClass(Configuration config){
  String key = config.getA() +","+ config.getB();
  MainClass mainClass = mainClassMap.get(key);
  if(mainClass == null){
   mainClass = new MainClass(config);
   mainClassMap.put(key,mainClass);
  }
  return mainClass;
 }

}

Will this help you ? 这对您有帮助吗?

Assuming the MainClass instantiate with Configuration then by calling MainClassFactory.getInstance().getMainClass(config) you will be able to get the same MainClass by "same" configuration (the "same" here means the value of a and b are the same) 假设MainClass使用Configuration实例化,然后调用MainClassFactory.getInstance().getMainClass(config) ,则可以通过“相同”配置获得相同的MainClass(此处“相同”表示a和b的值相同)

The idea is simple: use a key with can represent the characteristic of the Configuration and cache the MainClass using a Map with this key. 这个想法很简单:使用带有的键可以表示Configuration的特性,并使用带有此键的Map来缓存MainClass Finally return the same object when providing configuration with same characteristic; 当提供具有相同特性的配置时,最后返回相同的对象;

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

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