aspose file tools*
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
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design Pattern Needed" Watch "Design Pattern Needed" New topic
Author

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?

Thanks,

Warren




Warren Bell
Brett Maclean
Greenhorn

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.


http://www.ontruenorth.com
Follow us on Twitter: http://twitter.com/truenorth_buzz
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
 
wood burning stoves
 
subject: Design Pattern Needed