Friday, December 23, 2011

Design Patterns Refresher

The most common design patterns from "Head First Design Patterns" by Eric Freeman and Elisabeth Freeman - the best book ever about design patterns:

Encapsulate algorithms. Allows object's behaviour to be changed at runtime by substituting default algorithms. 

Notify dependents of state changes. A very common pattern, often part of the language, see events and listeners in C#.

Use and extend behaviour of the object being wrapped, while acting as that object to the external world. A decorator object is created with an instance of an object of the same type. When a method is invoked, it calls the embedded object's method, modifies the result, and returns it.

Simple Factory
Encapsulate object creation. Useful when you need to decide at runtime which object to create.

Factory Method
Same purpose as Simple Factory, but more abstract: moves actual object creation to other factory objects. 

Indispensable when there is one of something in the system, like one database, or one set of configuration data. Implemented as a sealed class with private constructor and a static method that returns the only instance.

Encapsulate actions. Every action object has "execute" method. Very useful for a processing system that needs to queue commands and then execute them in order. 

Provide a converter that allows clients to use the interface they expect with an object that has a different interface.

Provide a simplified interface. Used to hide complexity of the underlying system from the external world.

Template Method
Provides extension points or hooks by defining steps of an algorithm and allowing or forcing subclasses to provide implementation for some of them. This pattern is often used in frameworks.

Provides a uniform way of accessing sequentially elements in an aggregate object of any type. Typically found in collections.

Organize objects into tree structures so a group of them can be treated the same way as a single element. Each object in the hierarchy is a component that can have sub-components. User interfaces are usually built as composites.

Encapsulate behaviours of an object in state objects. Current state object can request transition to another state.

Provide a surrogate object that controls access to an object that is remote, needs to be secured or is expensive to create.

No comments:

Post a Comment