Search code examples
javawrapperdynamic-programmingdynamic-proxy

How to create a dynamic Wrapper?


In some parts of my Program I need to pass around a set of Objects (also some primitive types) between my classes.

For every new combination I have to create a pretty "stupid" wrapper-class which just contains two or three fields. (int, String or today I've got a int, String, Date).

All they contain is an empty and a full constructor and each field has its getter/setter. I came across Java dynamic proxies which is praised as a solution for my problem. It appears to be impressive and mighty - I could find some fancy examples how to control robots or fetch changes in beans, yet nothing for my original purpose only to transfer a simple wrapper object between two classes.

Is there a tutorial for this kind of issue?


Solution

  • As i meantion in comments, consider to use generic.

    You need to have object which holds two custom types, here we go:

        class Pair<F,S> {
        private F first;
        private S second;
    
        Pair(F first, S second){
           this.first = first;
          this.second= second;
        }
    
       public F getFirst(){
       return first;
       }
    
       public S getSecond(){
       return second;
       }
    }
    

    How to use it? it is simple, Pair<String,Integer> pair1 = new Pair<>("This is One",1);