This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Design Pattern Needed  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for a design pattern that allows a method to return an object along with a message. I have a singleton service object with various methods that return various objects.

public boolean doSomething()
{
return true;
}

but I also need to return a message.

public boolean doSomething()
{
String message;
// I need to return this message also
message = "Success"
return true;
}

I had one solution that returned an object that had the return object in it and the message. But, that doesn't seem like the correct way of doing it. I can't put the message as a member of the service class since many threads will be using this service object.

private String message;

public boolean getMessage()
{
return message;
}

public boolean doSomething()
{
message = "Success"
return true;
}

Is there a design pattern that does what I am looking for?

Thanks,

Warren



 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a callback, where the caller has the message and setMessage(String) method. You'd end up with the following:


The Messagable interface (terrible name, but couldn't really think of a better one ;-) just has the setMessage behaviour which will allow the Service to set the message in its caller without having to worry about adding wrapper objects or concurrency issues when holding the message itself (as you point out in your post).

The signature of your service class method would then be:


How practicable this approach is will depend on how many callers access this service and whether you have control (from a code-ownership perspective) of all the callers. If it's a lot or the service is used by many departments / customers then making them all implement your new interface may not be that easy and you may have to persuade the callers of the service to change their signature. But it's something to think about anyway and those issues are going to be with you for most solutions to this problem.

Hope this helps.

 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Warren Bell wrote:
I had one solution that returned an object that had the return object in it and the message. But, that doesn't seem like the correct way of doing it.


Why do you think it's not a correct way?
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!