2

My use case is that I am making requests to various APIs that return different external data types. Then I have to decorate the data with additional metadata that I retrieve from other APIs. Then I return a response depending on what clients want.

First I am making requests for the data, and based on those results, I am retrieving the appropriate metadata for each piece of data.

My approach so far:

  1. Call APIs and receive external types
  2. Convert external types to an internal interface with Adapter pattern
  3. Call other APIs to retrieve metadata for each data piece
  4. Mutate the adapted internal object with Visitor pattern
  5. Format the internal object appropriately for the client

I'm concerned about #3 (EDIT: I'm concerned about #4, not #3). Is using Visitor pattern on adapted internal types appropriate or is there an easier way? Would it be a good practice to create Visitor operations that each mutate the objects in sequence, in different ways? (i.e. setting fields in the list of data objects)

nuclear
  • 365
  • 5
  • 16
  • Cab you post minimal code? – Ravindra babu Mar 09 '16 at 12:09
  • 1
    To me your 3th and 4th steps are linked together : you seem of being concerned by step #3 but asking questions about the #4 (use of your visitors to mutate objects). Can you give more details about those steps? The number of input types vs number of mutation tasks should be the decisive factor to say if Visitor pattern is best suited. – floppy12 Mar 09 '16 at 14:51
  • Oops I meant I'm concerned about step 4, sorry. Let's say step 1 gets a list of Zoo objects that contain some metadata like a list of animal IDs, but not all of it. Also, there are different APIs for getting Zoos but they are varying types so we adapt them to a consistent internal interface. We are missing details for the animals though, since we only have the animal IDs. We call more APIs and pass on these animal IDs to fill in the empty fields like age, location, etc. Those empty fields are mutated after we retrieve the metadata. Please let me know if this is enough detail thanks – nuclear Mar 10 '16 at 02:44
  • @Ravindrababu I think this is more of a design problem than a coding problem right? Please let me know if you disagree – nuclear Mar 10 '16 at 02:46
  • Mutate the adapted internal object : I think it should be builder pattern. http://stackoverflow.com/questions/34726422/passing-properties-to-factory-method/34752184#34752184 – Ravindra babu Mar 10 '16 at 03:21
  • That sounds like a good suggestion thanks. We can pass an object around to be built gradually. Is my adapter pattern for an internal representation valid? I can make the the adapter trigger the initial building! Then continue to build it! – nuclear Mar 10 '16 at 03:49
  • Adapter is behavioural pattern. It convert one interface to other interface. But you are looking for solution at creational patterns. Point 3 does not need a pattern. You can have a method like getMinifiedInternalObject() on External object and get InternalObject. – Ravindra babu Mar 10 '16 at 04:00
  • http://stackoverflow.com/questions/34280716/how-to-prune-an-object-of-some-of-its-fields-in-java/34281053#34281053 : this will show conversion of one object to other object – Ravindra babu Mar 10 '16 at 04:14
  • small correction. Adapter is structural pattern. – Ravindra babu Mar 10 '16 at 04:23
  • I can't modify the External objects because I don't own the code. That's why I use adapter pattern to adapt varying External types into a consistent internal type/interface. But i think can create a transform function in the builder to accomplish the same thing. I appreciate your insight – nuclear Mar 10 '16 at 04:37

1 Answers1

0

Based on our discussion in chat :

Mutate the adapted internal object with Visitor pattern

Actually your requirement can be fulfilled with Builder pattern instead of Visitor pattern because you have to build object gradually.

Visitor is behavioural pattern and it was meant for different purpose :

Similar operations have to be performed on objects of different types grouped in a structure

Related SE questions:

When would you use the Builder Pattern?

When should I use the Visitor Design Pattern?

Convert external types to an internal interface with Adapter pattern

Adapter is a structural pattern. If you want to convert external interface to internal interface (structure), you can use Adapter pattern.

If you want to transform external object into internal object, you can achieve it without any pattern. You just need a Transform function ( on lines of Builder).

Have a look at related SE question ( object transformation without pattern):

How to prune an object of some of its fields in Java?

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • It sounds like I can pass in the External type to the builder to start building the internal model right? (Instead of using an adapter) Thanks a lot for your insight – nuclear Mar 10 '16 at 04:40
  • Yes. If you are looking only for data, Adapter is not required – Ravindra babu Mar 10 '16 at 04:43