0

Is there a way to setup the following construct in JAVA:

  • having a common interface or base class
  • having a static public field declared by the common interface
  • each model implementing the common interface should have its own static field (not one shared instance for all models)

Detailed explanation: I'm working with ORMLite and I need to refresh the ForeignCollections of my models after deserialization. For doing this I need to have a reference to my DAO from the models, which I don't want to.

So, I came up with the following concept:

  • keep a static field in each of the models of the following Interface:

    public interface SerializableObserver {
      void onAfterDeserialization(Object object);
    }
    
  • in my implementation of private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException after reading in all ivars i'am calling the onAfterDeserialization-Method of the static field i'am holding in the model.

    • In the Dao, I'm setting the static field of the model. So when the deserialization is finished, a method in my Dao is called. Where i can finally refresh the ForeignCollection so it's still valid after deserialization.

So what I'm looking for is some sort of way to make this whole approach a bit more generic so, I don't have to implement this behavior for all of my 20 Models. And finally, this is going to be an Android-App. so no fancy Java-8 things.

2
  • I don't understand your question. Though I found it somewhat interesting, can we move to chat? Commented Jul 2, 2017 at 7:05
  • That's what I've tried. But as far as I know I can't declare static fields or methods for an interface. Commented Jul 2, 2017 at 8:30

2 Answers 2

1

I would use another class that maps Model classes to SerializableObserver implementations. For example,

DeserializerMap:

public enum DeserializerMap {

    INSTANCE;

    private Map<Class<? extends Model>, SerializableObserver> modelObserverMap = new HashMap<>();

    public void registerSerializableObserver(Class<? extends Model> modelClass, SerializableObserver serializableObserver) {
        modelObserverMap.put( modelClass, serializableObserver );
    }

    public void deregisterSerializableObserver(Class<? extends Model> modelClass) {
        modelObserverMap.remove( modelClass );
    }

    public SerializableObserver getSerializableObserver(Class<? extends Model> modelClass){
        return modelObserverMap.get( modelClass );
    }

}

Model class:

public class ModelClass implements Model{

    private int id;

    public ModelClass(int id) {
        this.id = id;
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
        ois.defaultReadObject();
        DeserializerMap.INSTANCE.getSerializableObserver( this.getClass() ).
                onAfterDeserialization( this );
    }
}

The "Model" interface just extends Serializable and is used in DeserializerMap, but you can just get rid of the interface and use Class<? extends Object> instead of Class<? extends Model> in DeserializerMap,

Model:

public interface Model extends Serializable{
}

DAO Class:

public class DAOClass {

    public DAOClass(){
        SerializableObserver serializableObserver = new SerializableObserver() {
            @Override
            public void onAfterDeserialization(Object object) {
                System.out.println("After deserialization");
                anotherMethod();
            }
        };
        DeserializerMap.INSTANCE.registerSerializableObserver( ModelClass.class, serializableObserver );
    }

    public void anotherMethod(){
        System.out.println("another method");
    }
}

if you don't want to do anything additional than just call DAOClass method then you can map ModelClass with DAOClass classes, but I would recommend using DAO just for communicating with your persistence system and register mappings in your main class and not in DAOClass constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, super helpful!
@SaschaHeld I've found your question on Upwork, could you accept a proposal?
0

Without understanding the details I can offer the following: Use an Interface to define commonly required behaviour and then a generic abstract base class to define a common structure is something you should consider.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.