Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the design pattern for object to object conversion?

The question is simple. I need to convert the content of a bean into an other bean with different getters and setters. What's the right design pattern for doing this?

I think an adapter pattern is mainly referring to a mapping of a signature or interface to an other. Not a real mapping between objects.

Many people talks about "mapper" but I think this is not a design pattern.

So, is there actually a pattern for mapping an object to an other?

like image 575
user1883212 Avatar asked May 22 '14 11:05

user1883212


Video Answer


3 Answers

I'm not aware of any popular design pattern for object mapping.

I have been using Dozer framework for transfering the content from one bean to another.

It is simple to use and easily integrates with Spring.

Refer: http://dozer.sourceforge.net/documentation/gettingstarted.html

like image 150
Kalyan Avatar answered Sep 20 '22 11:09

Kalyan


A great "design pattern" would be to not tightly couple the mapped objects to each other. A copy constructor, for example, means that if you edit the source class, you also have to edit the destination class, and that can cause all sorts of downstream issues in a large project. Think about it this way. A very frequent mapping is from UI data structures to back-end data structures. You should be able to scrap your UI and not have to change the back-end for a new UI. An MVC design pattern is frequently used for this sort of work.

like image 29
dnellis74 Avatar answered Sep 20 '22 11:09

dnellis74


The related pattern is Assembler where you assemble some object given the contents of another. Since writing assemblers can be tedious and error-prone, you can use an object mapping library to do the work for you.

One object mapper you might check out is ModelMapper, which uses actual code to map properties, fields and methods, in a safe way.

like image 33
Jonathan Avatar answered Sep 22 '22 11:09

Jonathan