Monday, May 24, 2010

Types of Programming Design Patterns

There are many design patterns that are categorized based on the creation of objects, composition of objects and communication between objects. When we hear the keyword 'types' we tend to think one type can be a replacement for the other(or atleast i think so :-) ). However this is not true. Every type is unique in its own way and suited best for different situations. Okay, Now the design patterns are broadly classified into three types:

  • Creational Patterns

  • Structural Patterns

  • Behavioural Patterns

  • The Creational Patterns deal with how the objects are created. These patterns show us different ways of creating objects so that the program does not depend on the way these objects are created. It helps us to delegate the creation task to a class saving us from the hard coded way of creating the objects using the "new" keyword. There are many different Creational patterns. Some of the popular patterns are:

    The Factory Pattern in which we have a simple decision making class which creates objects for us depending on some conditional parameter we pass to that class' method.

    The Abstract Factory Pattern which is one level above the factory pattern. The abstract factory returns objects which are themselves pure factory objects which in turn gives us the required objects.

    The Builder Pattern which creates complex objects step by step.

    The Prototype Pattern which creates duplicate objects by cloning.

    The popular Singleton Pattern which creates one(and only one) object of a class.

    The Structural Patterns represents how classes and objects are combined to form larger structures. Here the class patterns use inheritance to accomplish the their task and Object patterns include other objects within them to accomplish the task. Let see som of the structural patterns:

    The Adapter pattern which can be used to link two classes using an Interface.

    The Facade Pattern in which a single class can represent an underlying subsytem.

    The Proxy Pattern where a class takes the responsibility of a complex class. This can be better understood if one s familiar with the RMI in Java.

    The Decorator pattern which can add functionalities to objects dynamically.

    The Behavioural Patterns represent the different ways of communication between classes. Some of the behavioral patterns are:

    The Chain of Responsibility Pattern in which the command/message is passed through objects until the message is recognized and acted upon.

    The Command Pattern in which a command or action gets executed when an event occurs on an object.

    The Observer Pattern in which a number of concerned classes are notified of a change in a class.

    The State Pattern in which the states that the command passes through are remembered for future action.

    The Iterator pattern which provides a way to traverse a list of data in a class.

    We will see each of these patterns in detail in the further posts. I am going to use Java as a means of providing examples to better understand these patterns. Ofcourse, the reader can use any language as the examples are extremely simple and provided with rudimentary class diagrams.

    No comments:

    Post a Comment