Sunday, October 22, 2006

Creational Design Patterns

Design Patterns Series Part 1

Abstract Factory

Intent
  • Create an interface to
  • decouple creation of families of related or dependent objects
  • without specifying the concrete classes
Adaptability
  • clients should be decoupled with how its products are created
  • class library author should have a means of hiding the actual implementations of the products in the class library from the client
  • client should have the ability to switch between multiple families of products
  • a need to enforce that a family of related products need to be used together
Participants and example
  • Client
  • Abstract factory
  • Abstract product
  • Concrete factory
  • Concrete product
A standard example in this case is the usage of Look and feel algorithms used in windowing applications. Client selects a particular look and feel, which essentially boils down to the selection of the concrete factory. It then creates the various UI widgets using the abstract factory interfaces and then uses the widgets using the product interfaces. To change the look and feel, all the client needs to do is select a different concrete factory.

Advantages and Disadvantages
  • Isolates concrete classes
  • Makes exchanging product families easy
  • Promotes consistency among products
  • Supporting new kinds of product is difficult

Builder

Intent
  • Decouple the creation process of a complex object from
  • its internal representation
Adaptability
  • algorithm of creating a complex object should be independent of the parts that make up the object and how they are constituted
  • construction process must allow different representation of the object that is being constructed
Participants and examples
  • Client
  • Director
  • Builder
  • Concrete builder
  • Product
An example is text processing application wanting to save its document in various formats. For example, it might want to save its document in RTF, PDF, TeX, HTML format etc. So, in this case, the client, which is the text processing application instructs its save menu handler, which is the director, to save contents in various formats, which is the actual end product, thus decoupling the creation process of the various formats from the internal representation of the various formats. This is done using the builder interface. The director uses the builder interface to drive the creation process creating various parts of the end products, while the concrete builder object assembles the various parts leading to the creation of the end product.

Advantages and Disadvantages
  • Finer control over construction process where products are constructed in a step by step fashion
Factory Method