samedi 28 mars 2015

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. Now this JAR keeps on getting updated and we also need to update our system along with this too. Now if there is a slight change in this JAR i have to make changes to every class, so we decided to decouple it. 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. Hence every object has its own importance in every method.


Let me explain this with simple example.


Say i have a class 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


Aucun commentaire:

Enregistrer un commentaire