Argho Chatterjee
Argho Chatterjee

Reputation: 599

Which Design Pattern to use to Encapsulate different operations on same class

I have a scenario and am unable to understand which design pattern to use

Suppose there is class A

enum A {
   A1, A2, A3, OVERALL;

   int value;
   int mod = 0; // represents how many times the value has been modified
    public void changeValue(String newValue) {
           value = newValue;
           mod++;
    }
}

Then I have a few instances of A

public static void main(String args[]) {
     A obj1 = A.A1;
     A obj2 = A.A2;
     A obj3 = A.A3;
     obj1.changeValue("value1");
     obj2.changeValue("value2");
     obj3.changeValue("value3");
}

Now I want to create a object (say "OVERALL") , say an instance of A only which will be like , if any instance of A changes its value, then the "OVERALL" will hold the last changed value on any instance of class A , and the "mod" value of "OVERALL" will be incremented with every change to any instance of class A (here, obj1, obj2,obj3).

In the above code, my "OVERALL" value is "3" (since obj1, obj2,, obj3 , each have been modified once, and the value the "OVERALL" object should hold is that of obj3 since it was last modified there)

So which design pattern can be used for solving this design issue.

I am trying to reuse the A class for creating the "OVERALL" object since the required fields (i.e. value and mod are same as that of class A)

Kindly suggest.

Regards.

Upvotes: 0

Views: 94

Answers (1)

sethu
sethu

Reputation: 8411

What you need is an Observer pattern. The simplest solution though would be a static variable in A to hold both the last value and the mod value.

class A{
 static int value;
 static int mod;
}

So any time for any instance of A if you change the value or mod, it will be updated for all instances.

But if you don't want to use static variables, then you could use the Observer pattern https://dzone.com/articles/observer-pattern-java

So you would make A an Observable and also make it an Observer. Convoluted. No one will understand why you are doing this. But working with just what your question describes, it will something like this:

package test;


import java.util.Observable;
import java.util.Observer;

public class Test {

public static void main(String[] args) {
    A obj1=new A();
    A obj2=new A();
    A obj3=new A();

    A superSet=new A();

    obj1.addObserver(superSet);
    obj2.addObserver(superSet);
    obj3.addObserver(superSet);


    obj1.changeValue("change1");
    obj2.changeValue("change1");
    obj3.changeValue("change1");

    System.out.println(superSet.mod);
    System.out.println(superSet.value);
    }
}

class A extends Observable implements Observer{

String value;
int mod;

@Override
public void update(Observable o, Object arg) {
    A updatedValue = (A) arg;
    this.value=updatedValue.value;
    mod++;
}

public void changeValue(String newValue) {
    this.value = newValue;
    setChanged();
    notifyObservers(this);
    }
}

This prints:

3
change1

Hope this makes sense. But again not sure why you would want to do this.

Upvotes: 1

Related Questions