Looking for a Design Pattern

I’m writing some code that does a few different mathematical
calculations, then returns a value.  All the calculations use the
same inputs, so there’s the potential for a lot of reuse here. 
So, I’m looking for the pattern that fits what I need.  The Command Pattern
seems very close to what I want, except execute() doesn’t return a
value.  In fact, this description
of the Command Pattern uses it to implement a calculator, but it just
prints the results to the console instead of returning the value. 
In order for me to use the Command Pattern, I’d have to change the
interface of the Command interface
either to add a getter for the value or add a return type for the
execute method. 

So, I need something almost, but not quite, exactly like the Command
Pattern.  How have people implemented this in the past?  I
feel there has to be another pattern out there for this, because it seems so common.

Advertisements

About Lance Finney

Father of two boys, Java developer, Ethical Humanist, and world traveler (when I can sneak it in). Contributor to Grounded Parents.
This entry was posted in Programming. Bookmark the permalink.

6 Responses to Looking for a Design Pattern

  1. Jason Jones says:

    What you’re looking for is the Strategy design pattern. Basically you have several solutions to the same problem and you encode each solution in a implementation class which extends or implements some superclass/interface.

    From there you have options. You can build a factory which chooses the implementation based on some criteria or you can just have a wrapper/proxy which takes the implementation class as an argument and instatiates and invokes the solution class and returns the result.

    I’m sure you can find better explanations than this via google. Good luck!

    Jason

  2. Lance Finney says:

    Thanks, Jason. I’ll look into that.

  3. john f says:

    don’t get hung up on the interfaces of the pattern. make the interface anything you want. Command and Strategy are very similar. Commands usually encapsulate very different processes with a common interface (usually an execute method of some sort). Strategies are expected to do somewhat more similar things, like implementing different algorithms to do the same thing. Strategies can have multiple methods in their common iterface. It’s just a collection of methods to do something in a certain way.

  4. Tom Hawtin says:

    A switch statement?

    Never use a truck load of design patterns where a bent coat hanger will do.

  5. Lance Finney says:

    I’ve gone with a Strategy pattern here, except it doesn’t quite fit the definition perfectly. The Strategy pattern seems to be intended for doing the same thing in different ways (like adding various Borders to a component). However, in this case, I’m trying to do multiple similar things to come up with 11 different calculated values. Syntactically, Strategy works, but not semantically.

    My implementation ended up being what I would have done anyway, but I have a better understanding of how to describe it.

    And while a switch statement would have worked for a couple simple calculations, these are 11 complicated calculations. A switch statement would have smelled way too much.

  6. Adam Waldal says:

    I just was checking out your blog Lance and I could not help but weigh in on the stategy pattern solution. I just finished this same solution for an app that had a different number of formulas for the same problem. The strategy pattern is the appropriate solution.
    And the comment about coat hangars interesting, but I don’t think the guy that invented coat hangars is coding in Java. Coding to interfaces is rarely a bad thing or so Rod Johnson says…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s