Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Is this an Adapter, Factory or even something else?

 
Ranch Hand
Posts: 47
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a Java EE application which takes input data from a JAX-RS-Resource, passes them through the application and at some point it calls a SOAP client which classes gets generated. But the problem is, that I have multiple clients which represent different start times. There's a "ClientParameters" class which assigns the input data to the client data. But the problem is: I have this class almost exactly the same 4 times. They mostly do assignments and converting. So it looks like this:



So the idea is to replace the InputData parameter with a "ClientValues" class which already holds the correctly formatted data, so that the ClientParameters only calls getters, nothing more.

First I thought I'd create a "ClientValuesAdapter" which simply contains a static method which creates the "ClientValues". Inside we have lots of pure functions that can be tested easily. Something like this:


A friend pointed out that this is not really an Adapter but a Factory. I agree with this mostly, but most definitions on the web use inheritance. I don't see any advantage in using inheritance or non-static methods here. But I don't want to use any naming which may confuse other developers. Most patterns seem to be coupled with inheritance which is sometimes just not necessary.

What would you call it? Is it an Adapter, a Factory or even something else? I was thinking about calling it ClientValuesMapper instead.

I was also thinking about changing it so it conforms the Adapter definition. But I think pure functions are better to test.
 
Marshal
Posts: 72657
317
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for the “beginning” forum. Moving.

Why do you want a function to turn a boolean to 1/0 in text? Why did you call it isSinglePerson, since it doesn't take a person nor does it test whether that person is single?
 
Christian Wansart
Ranch Hand
Posts: 47
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why do you want a function to turn a boolean to 1/0 in text?


The SOAP endpoints often uses strings like "0", "1" and sometimes also "1" and "2" as boolean values. Don't ask me why, this is an older system I have to work with.

Campbell Ritchie wrote:Why did you call it isSinglePerson, since it doesn't take a person nor does it test whether that person is single?


I guess the naming here is not good enough. I get several boolean values from the JAX-RS resource which need to be converted to the SOAP values. So depending on true or false I have to set "0" or "1".
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can understand “0” and “1”, but “1” or “2” rather confuses me
Call the method to01String() and it will tell you accurately what it does.
 
Christian Wansart
Ranch Hand
Posts: 47
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not just you. But I can't change it so I have to live with it. :-)

Okay, good point.

But still: Would you call this a factory?

I was already thinking about rewriting it to make it a real Adapter like this:


There is not so much difference to the version above. (I'd probably extract the isSingle to something like to01String().) I have 1 class less since I don't need the ClientValues class anymore. But the Adapter class now has state. Perhaps this is better than my first approach? I am not sure.
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is ages since I read a patterns book, but I don't think that is a factory. Find out who gave you such dreadful method names to work with and . . . . think of something.
reply
    Bookmark Topic Watch Topic
  • New Topic