1

I will give a simple example to help you understand my question. Suppose we have a rectangle and a Utility class with a method that creates a buffer arround a shape.

The .createBuffer method has required and optional arguments:

  • required: radius

  • optional: direction (for example "inside" or "outside", default is "outside")

... maybe more arguments ...

Every combination of arguments needs a different algorithm.

Which design pattern is appropriate for solving similar problems?

asked Apr 15, 2014 at 15:46
3
  • Check out the builder pattern ... here is a random SO post about this pattern : stackoverflow.com/q/5007355/2001247 Commented Apr 15, 2014 at 15:49
  • possible duplicate of How to avoid excessive method overloading? Commented Apr 15, 2014 at 15:58
  • 1
    Design patterns are all about extension and reusability. You need neither or at least you don't sound like you need it. Commented Apr 15, 2014 at 20:30

2 Answers 2

3

It's not clear to me that you need any design pattern -- at least not in the sense of the "Gang of Four" book.

From your description of the problem:

Every combination of arguments needs a different algorithm

what you need is some way to get from input -> output where input is "combination of arguments" and output is "algorithm". This is essentially just a hash table (or a function), in which:

  • keys: some property of the specific arguments present
  • values: algorithms (the exact implementation could be functions, objects, etc. depending on which language you're using and how convenient each choice is).

The second part of your problem -- "The .createBuffer method has required and optional arguments" -- isn't well-specified enough to give a sure answer, and will also depend on your choice of language. If your language supports optional arguments and default values, this problem nearly solves itself:

def createBuffer(self, radius, direction="outside")
 ...

If you're in Java, you can use reference types and check for null, converting them to default values where necessary. Or even better, try this -- it helps clarify your intention to use nullable types to other programmers!

public void createBuffer(float radius, Optional<Direction> direction) {
 if ( direction.isAbsent() ) {
 direction = new Direction("outside");
 }
 ....
}

As others mentioned, you could also throw the Builder pattern at it, but personally I wouldn't unless I was sure that I needed it, because a single method with a single type signature is simpler and easier to use.

answered Apr 15, 2014 at 17:39
0

I would go with a builder pattern. Where shape is the shape to be buffered, 5 is the required radius, I made up some other things that you may want your buffer to have.

var bufferedShape = new BufferBuilder(shape, 5)
 .Inside()
 .SinglePixel()
 .DropShadow(10)
 .CreateBuffer();

A bit more detail in this post.

Edit: The builder pattern addresses the need for different algorithms in that each call to the different methods of the builder class builds up the final buffered shape class that knows how to draw itself based on the methods called during the building phase.

gnat
20.5k29 gold badges117 silver badges308 bronze badges
answered Apr 15, 2014 at 16:10
1
  • 1
    How does this answer address the other part of the question -- "Every combination of arguments needs different algorithm"? Commented Apr 15, 2014 at 17:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.