Reputation: 414
I'm trying to implement a converter between different types in java: I have a super class foo that have 2 sub classes: foo1 and foo2, I also have 2 unrelated other classes bar1 and bar2 and I’m trying to implement a converter from bar1 to foo1 and from bar2 to foo2 (and the opposite).
Because I have inheritance here I was thinking that an interface with generic type (which will be bar1 or bar2) should be created and it will define the functions needed for the conversion to happened, I also want the converter to be one generic converter for both bar1 and bar2 and I was thinking it should somehow use the interface implementations. A better idea or reference to such implementation example will be much appreciated. I was also looking for a design pattern that describes such a scheme but could find appropriate one... Any ideas?
Upvotes: 0
Views: 761
Reputation: 734
I would go for a common interface and a common parent class, and possibly use the builder pattern also..;
a common partialFooBar interface
a common fooBarChimera parent class
You could then potentially put your converter into the fooBarChimera parent (and enforce the required extra details for the finalisation of whichever you want.
You may also want to use the builder pattern.
to flesh it out a bit...
ie the first class
partialFooBarBuilder
has a simple default constrcutor and setter methods.
The final method will then 'convert' this class to the 'full blown' version, or the fooBarChimera, which then only needs to have 'getter' methods in it.
The proper version is only available from this builder class (or the fooBarChimera), so you can more easily enforce that the contents are in good shape before returning your final proper foo class (you can do this with some nice error handling if it is linked to your user interface).
so you end up with somthing like...
public class buildpartFooBar implement fooBarCommon{
//fooBarCommon is the interface the enforces the commonality of between the 2 objects.
public buildPartFooBar(){...}
...[various setter methods here ....]
//make the different types of object we may become
private foo1 makeFoo1(iNeedThisToBeAFoo1 object){...}
private foo2 makeFoo2(iNeedThisToBeAFoo2 object){...}
private bar1 makeBar1(iNeedThisToBeABar1 object){...}
private bar2 makeBar2 (iNeedThisToBeABar2 object){...}
private fooBarChimera makeChimera (){...}
}//end of buildPartFooBar class
It is then simple to include a method makeFooFromBar, where you return a foo1 or Foo2 or whatever you like, you will likely do this via a static method in your fooBarChimera class
Remember that you will need to deal with the situation that your converted bar and foo clases are related to one another (you may need to implement some form of collection that has a bar key and foo object, or maybe this should be implemented in your fooBarChimera somewhere).
You may decide that foo and bar also need comparable methods such as, again it may be sensible to include these in the fooBarChimera as a static comparison. This way you only need to write it once in the parent.
couldThisFooBeBar
and
couldThisBarBeFoo
so as at creation time you can compare them and decide if they should be linked automatically, then saving you the operation of conversion if the link already exists in your collection.
You will also need to decide if the conversion is 2 way, a foo can be a bar, but a bar may be compatible with 2 foos.
In this case you may decide to head over to a database that can handle this sort of thing and store your foo and bar objects in it, it may be easier than a customised colection class also, especially if you give each foo and bar a uniquie objectID (guid).
It could become a lot more advanced.... it depends where you intend to take it, sounds like an interesting task you have set yourself, can we ask why you want to do this?
something I might just do for fun.... like when I modelled a legacy DB in terms of java collections, only to realise how stupid I was, and just converted it to a more sensible DB instead.
David
Upvotes: 1
Reputation: 47290
Adapter is the desgin pattern http://en.wikipedia.org/wiki/Adapter_design_pattern for converting one type to another.
Creating new objects from other objects would be "prototype".
Or simply creating new instances uses a "factory" or a "builder".
Upvotes: 1