Design Patterns Tutorial

What is the design pattern?

If a problem occurs over and over again, a solution to that problem has been used effectively. That solution is described as a pattern. The design patterns are language-independent strategies for solving common object-oriented design problems. When you make a design, you should know the names of some common solutions. Learning design patterns is good for people to communicate each other effectively. In fact, you may have been familiar with some design patterns, you may not use well-known names to describe them. SUN suggests GOF (Gang Of Four--four pioneer guys who wrote a book named "Design Patterns"- Elements of Reusable Object-Oriented Software), so we use that book as our guide to describe solutions. Please make you be familiar with these terms and learn how other people solve the code problems.

How many design patterns?

Many. A site says at least 250 existing patterns are used in OO world, including Spaghetti which refers to poor coding habits. The 23 design patterns by GOF are well known, and more are to be discovered on the way.
1. Creational Patterns 2. Structural Patterns 3. Behavioral Patterns 4. J2EE Patterns
--- Abstract Factory --- Adapter --- Chain of Responsibility --- MVC
--- Builder --- Bridge --- Command --- Business Delegate
--- Factory Method --- Composite --- Interpreter --- Composite Entity
--- Prototype --- Decorator --- Iterator --- Data Access Object
--- Singleton --- Fa├žade --- Mediator --- Front Controller
--- Memento --- Intercepting Filter
--- Service Locator
--- Transfer Object

Bridge Design Pattern

“Decouple an abstraction from its implementation so that the two can vary independently” is the intent for bridge design pattern as stated by GoF. Bridge design pattern is a modified version of the notion of “prefer composition over inheritance”. Problem and Need for Bridge Design Pattern When there are inheritance hierarchies creating concrete implementation, you loose [...]

Facade Design Pattern

GoF definition for facade design pattern is, “Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.” How do we infer the above definition? Think of a component that solves a complex business problem. That component may expose lot of [...]

Decorator Design Pattern

To extend or modify the behaviour of ‘an instance’ at runtime decorator design pattern is used. Inheritance is used to extend the abilities of ‘a class’. Unlike inheritance, you can choose any single object of a class and modify its behaviour leaving the other instances unmodified. In implementing the decorator pattern you construct a wrapper [...]


Adapter Design Pattern

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. The name says it all. Interfaces may be incompatible but the inner functionality should suit the need. In real world the [...]

Singleton Design Pattern

Singleton design pattern is the first design pattern I learned (many years back). In early days when someone asks me, “do you know any design pattern?” I quickly and promptly answer “I know singleton design pattern” and the question follows, “do you know anything other than singleton” and I stand stumped! A java beginner will [...]

Prototype Design Pattern

When creating an object is time consuming and a costly affair and you already have a most similar object instance in hand, then you go for prototype pattern. Instead of going through a time consuming process to create a complex object, just copy the existing similar object and modify it according to your needs. Its [...]

Builder Design Pattern

Builder pattern is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object. For example, you can consider construction of a home. Home is the [...]

Abstract Factory Design Pattern

Factory of factories. To keep things simple you can understand it like, you have a set of ‘related’ factory method design pattern. Then you will put all those set of simple factories inside a factory pattern. So in turn you need not be aware of the final concrete class that will be instantiated. You can [...]

Factory Method Design Pattern

A factory method pattern is a creational pattern. It is used to instantiate an object from one among a set of classes based on a logic. Assume that you have a set of classes which extends a common super class or interface. Now you will create a concrete class with a method which accepts one [...]