Caveman's Blog

My commitment to learning.

Design Patterns

with one comment


Design patterns are an important facet of software engineering. I thought it might be a good idea to highlight them patterns on this page and explain about them in detail in my regular posts.

Design Patterns has been one of THE buzz words used in almost all the software shops for a while now and will remain so for quite some time. It is tantamount that you know at-least a few of them, lest you should live in oblivion. Knowing design patterns and learning how to implement them will definitely increase your marketability, which is a good thing; right ?

In software engineering, a design pattern is a general reusable high quality solution to a commonly occurring problem. A design pattern is not a finished product that can be transformed directly into code. A design pattern is a description or template for how to solve a problem that can be used in many different situations [1]. They are not language specific and hence can be implemented using any language of choice.

What is a pattern?

In simple terms a Pattern is a generalized reoccurring solution to the situation/problem. Let me explain with a simple example.

Example 1:
What would one do when the hinge of your door make a squeaking noise?
You would pour a few drops of oil on the hinge to get rid of the noise.

Example 2:
What would one do when your key is hard to turn in your lock?
You would pour a few drops of oil in the lock to make the turning of the key more smoother.

Example 3:
What would one do when an nut got stuck to an iron bolt so hard that you are unable to unscrew the nut?
You would pour a few of drops oil on the screw and try to open them.

Problem: Metal pieces of a part/gadget get struck or get tight because of rust.
Pattern: We can use oil to loosen up the parts. This is a reoccurring solution to the problem mentioned above.

Hope I have made some sense with this example. Similarly all the design patterns are reoccurring solutions to problems that are encountered during Software design/development. The essence of defining and implementing these patterns is to avoid reinventing the wheel

The design patterns gain popularity after the book ” Design Patterns: Elements of Reusable Object-Oriented Software (ISBN 0-201-63361-2 published in 1995)” has been written by four authors; Erich Gamma, Ralph Johnson, Richard Helm and John Vlissides, famously called as the “Gang of four” and hence the name “GoF” or “gang of four” patterns. The “gang of four” have originally grouped design patterns into the categories Creational Patterns, Structural Patterns, and Behavioral Patterns. Another classification has also introduced the notion of architectural design pattern which may be applied at the architecture level of the software such as the Model-View-Controller pattern [1].

Note: This is an ongoing effort and please excuse me if you find this article rather incomplete

Dependency Injection
Dependency injection is a software design pattern that allows a choice of component to be made at run-time rather than compile time. This can be used, for example, as a simple way to load plugins dynamically or to choose mock objects in test environments vs. real objects in production environments. [6]

Inversion of Control
In software engineering, Inversion of Control (IoC) is an object-oriented programming practice where the object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis. [7]

Gang of Four Design Patterns:

Creational Patterns
These patterns create objects for you, instead of you having to instantiate objects directly.

Factory Method
This provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided. The essence of the Factory Pattern is to “Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.” [4]

Abstract Factory
Provides an interface to create and return one of several families of related objects. In other words an Abstract Factory is a factory that would return one of several factories.

Builder
Separates the construction of a complex object from its representation, so that several different representations can be created depending on the needs of the program.

Prototype
Prototype starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances.

Singleton
Provides a class of which there can be no more than one instance, and provides a single global point of access to that instance. This is not definitely a very good pattern unless used properly. The reason being, the object generate from this class in essence a global object/variable and this is not a good thing in programming world.

Structural Patterns

These patterns will help you compose groups of objects into larger structures.

Adapter

Bridge

Composite

Decorator

Façade
It is used to make a single class represent an entire subsystem

Flyweights

Proxy

Behavioral Patterns

These patterns will help you define the communication between objects in your system and how the flow is controlled.

Chain of Responsibility

Command
The command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method [5].

Interpreter

Iterator

Mediator

Memento

Template Method

A template method defines the program skeleton of an algorithm. The algorithm itself is made abstract, and the subclasses override the abstract methods to provide concrete behavior.

Observer

This is a pattern where a publisher allows a subscriber to subscribe [2]. The observer pattern (sometimes known as publish/subscribe pattern) is a design pattern used in computer programming to observe the state of an object in a program. It is related to the principle of implicit invocation. This pattern is mainly used to implement a distributed event handling system. In some programming languages, the issues addressed by this pattern are handled in the native event handling syntax. This is a very interesting feature in terms of real-time deployment of applications.

State

Strategy
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Template Method

Visitor

 
 

References:

1. Design Pattern – Wikipedia
2. Code Project – Design Your Soccer Engine…
3. The Design Patterns Java Companion – Free online book by James W. Cooper
4. Factory method pattern – Wikipedia.
5. Command Pattern – Wikipedia
6. Dependency Injection – Wikipedia
7. Inversion of Control – Wikipedia

Written by cavemansblog

April 2, 2008 at 6:21 pm

One Response

Subscribe to comments with RSS.

  1. I have scenario based question and I want a required help please tell me how many pattern I required to solve my problem?
    “We are creating an application for shape drawing. We have to support multiple shapes like circle, rectangle, square, polygon etc. These shapes may are related to each other like square is a type of rectangle where rectangle is also kind of polygon. We want to
    have an option of theme as well in our application. This theme option will allow us to draw shapes with some filled content like marble theme will allow us to create shapes filled with marbles where flower theme will allow us to create shape filled with flowers.
    By default user can create shapes with borders with no fill. In order to provide convenience to user we have an imaginary shape like Frame. The idea to introduce frame is to hold different shapes so user can move or apply effects in one go.”
    Your task is to create classes / interfaces for given problem.

    Arun

    December 8, 2011 at 11:38 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: