A Developer's Diary

Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Dec 30, 2012

Exploring Iterator Design Pattern

Intent
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation
Applicability
1. To access an aggregate object's contents without exposing its internal representation.
2. To support multiple traversals of aggregate objects.
3. To provide a uniform interface for traversing different aggregate structures (support polymorphic iteration).
In this post, we will be dealing only with the External Iterators. A typical ExternalIterator interface is as such
package com.devfaqs.designpatterns;

public interface ExternalIterator<E>
{
    public boolean hasNext();
    public E next();
}

Read more ...

Nov 23, 2012

Strategy Design Pattern

Strategy Pattern
The strategy pattern allows you to use different business rules or algorithms depending on the context in which they occur

When should I use the Strategy Pattern?
1. You can perform an operation in different ways
2. You want to select the operation dynamically at the runtime
3. You want to add new ways without modifying the application code

Real World Examples
1. File Compression Techniques
2. Layout Managers used by Containers in Java Swing
3. Comparators used in Arrays sort function

Read more ...

Nov 14, 2012

Template Method Design Pattern

Template Method Pattern
Template method pattern defines the skeleton of an algorithm in a method referred to as the Template Method deferring some steps to subclasses. The pattern is mostly used for building application frameworks where the framework implement the invariant pieces of the application's architecture and provide place holders or hooks for client customizations.

Examples from Java API
1. The Comparable interface defines the compareTo method which is a template method
2. The java applet class provides init, start, stop, paint and destroy hook methods for creating an applet
3. The actionPerformed method in ActionListener interface is a template method
4. The doGet and doPost methods in abstract class HttpServlet are examples of template methods


Read more ...

Oct 29, 2012

Visitor Design Pattern

Definition
A behavioral pattern which lets you define new methods to a class hierarchy without changing the class hierarchy on which it operates
Applicability
1. When the primary class hierarchy is fixed or it is coming from a different vendor and you cannot make any changes to that hierarchy
2. Several different operations have to be performed on all or some of the classes in the hierarchy

Visitor and Composite Pattern normally go hand in hand together. First, we will try to create an example of composite pattern. A composite pattern is a structural pattern which helps to create tree like hierarchial structures. Consider a linux filesystem where every element is considered as a file. We are trying to achieve the same tree structure using Directory, File and Link elements. All of these classes implement the interface FileSystemElement

Read more ...

Oct 8, 2012

Prototype Design Pattern

Definition
A creational pattern which allows creation of new instances by copying existing instance or prototype.

Applicability
1. When the classes to instantiate are specified at the run time
2. When creating a new instance is more expensive due to high computation or a call over the network

Read more ...