-2

I need to store some value in a variable which can be written from one class and read from another class so basically I need to hide the writing class to reading class.
So will it be a good option to have that variable in a separate class as a static variable? This project is regarding the OSGI framework.

Is there any design pattern to solve this problem or will this be okay?

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37

3 Answers3

1

You would be better off creating an interface to provide the value in question. Example:

Interface weighable {
   int getWeight();
}

 class dog implements Weighable {
     private int weight;
     public getWeight() {
         return weight;
     }
     public void setWeight(int weight){
         this.weight = weight;
     }
 }

 class person implements Weighable


 Class AirfreightCalculator {
    float calculateFreightPrice (Weighable w)
    .... calc details here
  }


  AirfreightCalculator fc = new AirfreightCalculator();
  Dog d = new Dog();
  dog.setWeight(30);
  price = fc.calculateFreightPrice(d);

Obviously, this is one scenario. Your case may differ but you need to provide more details.

May I point you to this article: https://medium.com/@ronnieschaniel/object-oriented-design-patterns-explained-using-practical-examples-84807445b092 that might further help you.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • can u point the specific design pattern – Hasindu Dahanayake Apr 02 '20 at 19:43
  • I meant that you need to familiarize yourself with these design patterns and then pick up the most appropriate one. Otherwise, you will always be dependant on others and might be given bad advices. – Tarik Apr 02 '20 at 19:48
  • I basically needs a one single variable to be share among two different classes .as an example class A and class B will need to have a access to shared variable in the program .So class A will change that variable from a method inside the class and it also can read that variable too.class B will basically only read that value which was written by class A but if I store that variable in class A as a static variable then class B will see the class A's name when it's using it I need to stop that too.So where to store this.I mean class B can't access class A for any reason. – Hasindu Dahanayake Apr 02 '20 at 19:50
  • The example I gave you would not work? If you use an interface and A implements that interface, and B reads the value via the interface, then class B is decoupled from class A. It only knows about the interface. Moreover, you might want to look at the Observer pattern so as to have class B notified of any change that class A makes to this value. – Tarik Apr 02 '20 at 19:56
  • how class B can read it via interface ?It would be great if you can show it via an brief example only the setting the value and reading the value part – Hasindu Dahanayake Apr 02 '20 at 20:02
  • Read the example I gave you. – Tarik Apr 02 '20 at 20:20
1

Since you are using OSGi, why not register the object as an OSGi service. Then the party consuming the object can find it in the service registry and does not need to know anything about the party creating and registering the object.

The OSGi service registry acts as a broker so the provider and consumer don't need to know any details about the other.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27
  • can you please tell me how to do it,I mean I need to share that variable among different consumers as well as the producer so producer will have the access tot do the modifications and other consumers will consume it.Do you have any resource or any kind of an example because I am a beginner to OSGI. – Hasindu Dahanayake Apr 03 '20 at 08:45
  • Look at OSGi Declarative Services. It provides an annotation based programming approach to registering and using services. – BJ Hargrave Apr 03 '20 at 16:54
0

It sounds like you are looking for a class to act as an intermediate container for 2 other classes. e.g.:

public class DataStore {
  private Object _data;
  public void setData(Object data) { _data = data; }
  public Object getData() { return _data; }
}

Then you could use this to pass data between two other classes, e.g.:

public class A {
  private DataSource _ds;
  public A(DataSource ds) { _ds = ds; }

  public void doSomething() {
    _ds.setData("hello");
  }
}

public class B {
  private DataSource _ds;
  public B(DataSource ds) { _ds = ds; }

  public void doSomething() {
    System.out.println(_ds.getData());
  }
}

Then you would use it like this:

DataSource ds = new DataSource();
A a = new A(ds);
B a = new B(ds);
a.doSomething();
b.doSomething();
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • yeah basically your idea is same as mine ,I tried to store it as a static variable in DatasSource class and neglect the object creation,getters and setters.I mean then I can set it direclty with the classname.propertyName and read it directly through the class.propertyName. – Hasindu Dahanayake Apr 02 '20 at 20:11
  • @HasinduDahanayake in general, using static variables, aka "global state", is a bad idea. there are a select few situations where it makes sense, but usually it is a bad design which will cause problems later. unless there is a reason you can't use a shared instance like a showed, you should definitely use some variation of the approach i showed – jtahlborn Apr 03 '20 at 13:13