It's not a secret anymore!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Design Pattern Needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design Pattern Needed" Watch "Design Pattern Needed" New topic

Design Pattern Needed

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
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?



Warren Bell
Brett Maclean

Joined: May 01, 2009
Posts: 22
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.
Follow us on Twitter:
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
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?

SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Design Pattern Needed
Similar Threads
omit else-clause when returning value in if-clause ?
return in catch block?
Why ModelDriven not working?
Null or Empty object (Return Type)
any thread that has a reference to another thread can execute any method of that othe