I've an entity class that the user will construct the object using the setters. This object when passed to another layer, that layer will call the constructChangeDataMap() method to identify the variables that must be updated in another framework.

Class Test {
   private int a;
   private int b;
   public int getA() {
      return a;
   public void setA(int a) {
      this.a = a;
   public int getB() {
      return b;
   public void setB(int b) {
      this.b = b;
   public Map<String, int> constructChangeDataMap() {
      // check what values has been modified and construct the map

What am doing now-

To find the updated entities I'd have to have a boolean variable and when the setter is called, this boolean will be updated and when the construtctMap method is called, I'll put only the updated values in the map. However this doesn't feel right. Is there any other pattern or way to figure out the updated variable.

  • Is there only one entity to track?
    – Laiv
    Commented Jul 27, 2017 at 18:14

2 Answers 2


There's one called the observer pattern. When something changes your state by calling your setters, loop through all your observers and let them know that you've changed. You can either directly tell them how you've changed or wait for them to call back to you and ask how you changed.

There are flavors that even work across threads.

The really nifty thing is that you don't have to know who your observers are. You let them tell you who they are. Just keep a collection of things that will let you call notify on them. Let them build it when they register as observers.

You may have heard of events or delegates. Those are just different words for this same thing.

  • 1
    If you are going down the observer path, consider using weak-references for observers. this pattern can lead to memory leaks since the references to the observers can keep them from being collected.
    – JimmyJames
    Commented Jul 27, 2017 at 14:40
  • @CandiedOrange if there was only one class to track (probably it's not the case, but we don't know yet), would not be easier with Proxy pattern?
    – Laiv
    Commented Jul 27, 2017 at 18:19

There are two basic approaches to this problem. One is to perform the update every time a variable is changed. This is very valid, but it involves potentially a lot of updates, which doesn't sound like what you want. It's unsuitable if the update process is expensive or slow, or if performing it multiple times has negative consequences. If it is what you want, the Observer pattern, as described by CandiedOrange, will work fine.

The alternative pattern is more suitable if the update process is expensive or has other negative consequences from being performed very often, and if you can predict when the updates need to be done. This sounds like your situation.

In that case you simply keep track of when each variable is updated. In your example you would add two private booleans 'updatedA' and 'updatedB'. They are set when you modify A and B respectively, doing it in the setter to ensure it always happens. When you want to potentially update the other framework you test the booleans, and do the relevant update if it is set. Then you clear the booleans.

If the number of variables being updated is not fixed then you need some kind of set structure to keep track, adding an entry when a variable is changed and clearing the set when the update happens.

This approach will only perform one update if the variable has been changed multiple times, but that is normally what you want. It won't work if the remote framework tracks changes. You also need to be careful in a multithreaded environment that the variables are not changed during the framework update process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.