Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern for version-specific implementations of a Java class

So here's my conundrum. I am programming a tool that needs to work on old versions of our application. I have the code to the application, but can not alter any of the classes. To pull information out of our database, I have a DTO of sorts that is populated by Hibernate. It consumes a data object for version 1.0 of our app, cleverly named DataObject. Below is the DTO class.

public class MyDTO {  
    private MyWrapperClass wrapper;  

    public MyDTO(DataObject data) {
        wrapper = new MyWrapperClass(data);
    }
}

The DTO is instantiated through a Hibernate query as follows:

select new com.foo.bar.MyDTO(t1.data) from mytable t1

Now, a little logic is needed on top of the data object, so I made a wrapper class for it. Note the DTO stores an instance of the wrapper class, not the original data object.

public class MyWrapperClass {

    private DataObject data;

    public MyWrapperClass(DataObject data) {
        this.data = data;
    }

    public String doSomethingImportant() { ... version-specific logic ... }
}

This works well until I need to work on version 2.0 of our application. Now DataObject in the two versions are very similar, but not the same. This resulted in different sub classes of MyWrapperClass, which implement their own version-specific doSomethingImportant(). Still doing okay. But how does myDTO instantiate the appropriate version-specific MyWrapperClass? Hibernate is in turn instantiating MyDTO, so it's not like I can @Autowire a dependency in Spring.

I would love to reuse MyDTO (and my dozens of other DTOs) for both versions of the tool, without having to duplicate the class. Don't repeat yourself, and all that. I'm sure there's a very simple pattern I'm missing that would help this. Any suggestions?

like image 743
Mike Monkiewicz Avatar asked Nov 14 '22 10:11

Mike Monkiewicz


1 Answers

You can use an Hibernate Interceptor an implement the instantiate(String entityName, EntityMode entityMode, Serializable id).

In that method, you can pass a MyWrapperClass to your data object. Depending on the version of your app, the wrapper will be different. The interceptor can be set at session level or at session factory level.

like image 122
Guillaume Avatar answered Dec 19 '22 12:12

Guillaume