简体   繁体   English

Java线程环境中的解耦

[英]Decoupling in threaded environment java

I am facing this decoupling problem. 我正面临这个解耦问题。 Let me explain this with example: 让我用示例解释一下:

Say i have different classes that uses some JAR. 说我有使用一些JAR的不同的类。 Now this JAR keeps on getting updated and we also need to update our system along with this too. 现在,此JAR不断更新,我们也需要与此同时更新我们的系统。 Now if there is a slight change in this JAR i have to make changes to every class, so we decided to decouple it. 现在,如果此JAR稍有更改,我必须对每个类进行更改,因此我们决定将其分离。 But these classes call the JAR classes in such a way that they make an object of it, set some flag and on the basis of that derive results. 但是这些类以使它们成为对象,设置一些标志并基于该结果的方式调用JAR类。 Hence every object has its own importance in every method. 因此,每个对象在每种方法中都有其自己的重要性。

Let me explain this with simple example. 让我用一个简单的例子来解释一下。

Say i have a class X 说我有X班

class X {
  Base base;
  public int getCalResult(int a, int b) {
     base = new Base();
     base.setA(a);
     base.setB(b);
     return base.getResult();
  }

  public int getCalResult2(int a, int b, int c) {
     base = new Base();
     base.setA(a);
     base.setB(b);
     base.setC(c);
     return base.getResult();
  }
}

class Base { This is some legacy class inside JAR so can't change this
  int a, b, c, d;
  public setA(int a) {
    this.a = a;
  }
  public getResult() {
    return a + b + c + d;
  }
}

How to decouple this sort of structure and make it thread safe at the same time. 如何解耦此类结构并使其同时成为线程安全。 Any pointers ? 有指针吗? Any design patterns ? 有没有设计模式?

I want that all the calls to JAR must be maintained in a single class, so that in case if there is change in JAR i only have to deal with single class and not all of them 我希望所有对JAR的调用都必须在一个类中进行维护,以便万一JAR发生变化,我只需要处理一个类,而不必全部处理

What i did is 我所做的是

Introduced BaseProxy 引入了BaseProxy

public class BaseProxy {
  Base b;
  public static newInstance() {
    b = new Base();
  }

  public static setABCD(int a, int b) {
    b.setA(a);
    b.setB(b);
  }
}

class X {
      public int getCalResult(int a, int b) {
         BaseProxy.newInstance();
         BaseProxy.setAB(a,b);
         return BaseProxy.getResult();
 }

  public int getCalResult2(int a, int b, int c) {
     BaseProxy.newInstance();
     BaseProxy.setABC(a,b,c);
     return BaseProxy.getResult();
  }
}

This would have worked in single threaded env but not in mutli. 这本来可以在单线程环境中工作,但不能在多线程环境中工作。 I need suggestion on this. 我需要这个建议。 Any other better design, i am sure there is. 任何其他更好的设计,我敢肯定有。

Few things not clear (or not ideal) 1. When you say, the legacy class in jar get updated, does it change the existing prototype or adds new functionality ? 几件不清楚(或不理想)的事情1.当您说jar中的旧类得到更新时,它会改变现有的原型还是增加新的功能? If they keep updating the definition then you are in trouble. 如果他们不断更新定义,那么您将遇到麻烦。

To get around this , one way is to extract an interface out of the legacy class and define a delegate implementing it (place it outside your calling code) and use it in calling code to reduce direct dependency. 为了解决这个问题 ,一种方法是从旧类中提取一个接口,并定义一个实现它的委托 (将其放置在调用代码之外),并在调用代码中使用它来减少直接依赖。 Now your delegate should talk to legacy class. 现在,您的代表应与旧班交谈。

Also, whenever your legacy class changes, its only the delegate side you will need to update (unless there is complete turnaround in called functions) 另外,只要您的旧类发生更改,就只需要更新它的委托方(除非被调用函数中有完整的转换)

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

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