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?

jorgeb
  • 11
  • 2
  • Check out the builder pattern ... here is a random SO post about this pattern : http://stackoverflow.com/q/5007355/2001247 – ElDuderino Apr 15 '14 at 15:49
  • possible duplicate of [How to avoid excessive method overloading?](http://programmers.stackexchange.com/questions/235096/how-to-avoid-excessive-method-overloading) – gnat Apr 15 '14 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. – Euphoric Apr 15 '14 at 20:30

2 Answers2

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.

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
  • 21,442
  • 29
  • 112
  • 288
Froome
  • 808
  • 6
  • 11
  • 1
    How does this answer address the other part of the question -- "Every combination of arguments needs different algorithm"? –  Apr 15 '14 at 17:40