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.
Aucun commentaire:
Enregistrer un commentaire