0

I'm using an API providing access to a special server environment. This API has a wide range of Data objects you can retrieve from it. For Example APICar

Now I'd like to have "my own" data object (MyCar) containing all information of that data object but i'd like to either leave out some properties, augment it, or simply rename some of them.

This is because i need those data objects in a JSON driven client application. So when someone changes the API mentioned above and changes names of properties my client application will break immediatly.

My question is:

Is there a best practice or a design pattern to copy objects like this? Like when you have one Object and want to transfer it into another object of another class? I've seen something like that in eclipse called "AdapterFactory" and was wondering if it's wide used thing.

To make it more clear: I have ObjectA and i need ObjectB. ObjectA comes from the API and its class can change frequently. I need a method or an Object or a Class somewhere which is capable of turning an ObjectA into ObjectB.

Chris
  • 7,675
  • 8
  • 51
  • 101
  • 2
    http://oodesign.com/ This site describes various widely used patterns. Maybe you find something suiting there. I guess what you want are a bunch of adapters and maybe a facade. The renaming and hiding maybe found in the proxy pattern. Just some "at first glance" tips. It could well be that you will need to combine some patterns to achieve the complete requirements you describe. – Fildor Jan 18 '13 at 07:46
  • [This question](http://stackoverflow.com/questions/3319002/automapper-for-java) immediately came to search. And got closed. –  Jan 18 '13 at 16:00
  • And where is the relation to this question? – Chris Jan 18 '13 at 16:06

1 Answers1

3

I think you are looking for Design Pattern Adapter

It's really just wrapping an instance of class A in an instance of class B, to provide a different way of using it / different type.

"I think" because you mention copying issues, so it may not be as much a class/type thing as a persistence / transmission thing.

Depending on your situation you may also be interested in dynamic proxying, but that's a Java feature.

Anders Johansen
  • 10,165
  • 7
  • 35
  • 52
  • tried to clarify my question. I don't think that adapter helps, because i need to specify the "process of copying" myself. – Chris Jan 18 '13 at 08:02
  • I found what i was looking for under "A further form of runtime Adapter pattern". I think this is the way to achieve what i want. – Chris Jan 18 '13 at 08:14
  • Yeah, that solution seems to be a kind of dynamic proxying: They build an adaptor dynamically at runtime. You often need this in a configurable system. – Anders Johansen Jan 18 '13 at 08:41