Showing posts with label Behavioural design pattern. Show all posts
Showing posts with label Behavioural design pattern. Show all posts
Tuesday, 7 January 2014

The visitor design pattern

The visitor design pattern provides a method of separating an algorithm on an object and the object's actual class implementation. This allows the programmer to easily follow the open/closed principle;

software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification

Object-Oriented Software Construction, Bertrand Meyer

That is, modifying an entity's behaviour without modifying the underlying source code. Following the open/closed principle provides many quality-related benefits as the original code never changes.

Benefits

  • Follows the open/closed principle
  • Allows a new operation to be defined without changing the implementation of the class
  • A visitor object can have state

Drawbacks

  • If a new visitable object is added then all visitors need to be modified
Saturday, 15 December 2012

The facade design pattern

The facade design pattern is a very simple pattern that provides a simplified interface to other code that may not be structured the same way. If we look facade up in the dictionary, this is one of the definitions we get:

An outward appearance that is maintained to conceal a less pleasant or creditable reality.

Google dictionary

This is the primary purpose of the pattern; to conceal a piece of code that isn't very nice to use and replace it with something better. That's all it is really, a class that calls code elsewhere.

Benefits

  • Can change a badly-designed or hard to use API into an easy to use API
  • Can merge multiple APIs into a single API
  • If all calls to a function are done through a facade then it is very easy to refactor

Drawbacks

  • Could possibly add unnecessary complexity if overused or used incorrectly