Tuesday, July 17, 2012

Behavioral Patterns - 4.Iterator Pattern

Nowadays, most of the applications we are developing are dealing with a huge set of data. These sets of data are termed as collections. A collection is nothing but a group of data.
In order to deal with such type of collections we need some mechanism to traverse over the list. Such mechanism can be termed as an Iterator. They are particularly meant for dealing with a set of data.

An Iterator Design pattern provides a design for an iterator to traverse over and deal with a collection of data.
The iterator should provide methods such as first(),next(),previous(),last(),etc in order to fetch the desired data from the collection.This will ease the process of accessing collection data.

For example, we are going to prepare a Name list for students in a class. This list will be our collection.

We are going to use the Student List for MarkSheet preparation. We need an iterator which can sort and iterate the Student Names alphabetically. One more iterator we need, to sort and traverse the list based on the marks.

Here, we have one collection of Student List for which we need two iterators i.e.StudentNameIterator and MarkListIterator.

Let us design our class as mentioned below,


Code Snippet:--------------
Client:

StudentList studentList=new StudentMarkList();
studentList.add(new Student(...));
...
StudentIterator iterator=studentList.createIterator();
iterator.sort();
iterator.next();
iterator.previous();

Monday, June 4, 2012

Behavioral Patterns - 3.Interpreter Pattern

In our day to day coding and development activities, situations may arise where we may need to evaluate/process a sentence or a group of strings in different ways.

A natural language parser could be an example for the above scenario. Such problems, can be handled with the help of Interpreter Pattern.

Generally, we define a Context which contains the sentence that needs to be interpreted. Then we define some interpreters to operate over the context and updates the context with the interpreted result.

There will be an abstraction over the interpreters.Seperate implementations can be written for their own interpretation needs.

For example, we are going to design some encryption algorithms. First of all, we need to define a Context, which contains the string or sentence that needs to be encrypted.

After that, we define an abstraction for the encryption algorithm. Then we define separate implementations for the algorithm that will work over the context and updates it with the encrypted string.

Being built with the Interpreter pattern,it will be a very simple task to change or add an algorithm to the stack with very minimal changes to the code.

Following diagram explains the Interpreter pattern,



Code snippet

Client:

  Context context=new Context(name); //Define the Context
  Cryptography cryptography=new MyCryptography();
  cryptography.interpret(context); 
//Invoke the Interpreter over the context
 
  cryptography=new CustomCryptography(); //Changing algorithm
  cryptography.interpret(context);


Interpretor(Abstraction):

abstract class Cryptography{
 public abstract void interpret(Context context);
//Interpreter method
}


Implementation(Cryptography Algorithm):

 @Override
 public void interpret(Context context) { 
//Interpreter Implementation
  char names[]=context.getName().toCharArray();
  int count=0;
  for(char name:names){
   names[count++]=(char)((int)name+2);
  }
  context.setResult(new String(names));
//Update the Context with result
 }

Sunday, June 3, 2012

Behavioral Patterns - 2.Command Pattern

There could be a need of a programming model which would associate some workflow or operations to an object.

 So that, the task of performing the operation/workflow is delegated to the object.

They can be triggered by a request, wrapped by an external object. We term those objects as Command Objects.

Following are the terminologies most frequently used in Command pattern.

Invoker:- Triggers a command on an object
Receiver:- Receives a request to execute a command.
Client:- Initializes the command and gets the work done.


The workflow that will happen during the executing of a command as follows,
1. Client will initialize and prepare a command with a receiver.
2. Client will initialize the invoker with the command prepared.
3. Client requests the invoker to execute a command
4. Invoker triggers the command
5. The command indentifies the receiver and fires the request over it


For example, we consider a Student/Teacher scenario in a particular class.
The teacher will command the students to either stand/sit. The students will act accordingly to the command issued by the teacher.

Here, Student is the Receiver Object, teacher is an Invoker and Stand/Sit will be a Command object.

The client will prepare the receiver(Student) and associate the command(stand/sit).Then, the invoker(Teacher) will be initialized with the same command(stand/sit) object.

After that, the client requests the invoker(Teacher) to execute the command(Stand/sit). The invoker(Teacher) triggers the command(Stand/Sit). The command identifies the receiver(Student) and gets executed.

The following class diagram explains the above description,


Code Snippet:

Client:

Student student=new Student();
Command standCommand=new Stand(student);
  //Associate the Receiver to the command
Command sitCommand=new Sit(student); //Associate the Receiver to the command


Teacher teacher=new Teacher(standCommand,sitCommand); //Initialize the Invoker with the command

teacher.sitDown(); //Request the Invoker to trigger the Command
teacher.standUp(); //Request the Invoker to trigger the Command

Invoker:

public Teacher(Command standCommand, Command sitCommand) { //Initialize the Invoker with the command
  this.standCommand = standCommand;
  this.sitCommand = sitCommand;
 }


public String standUp(){
  return standCommand.execute();
//Trigger the Command
 }

 public String sitDown(){
  return sitCommand.execute();
//Trigger the Command
 }

Command:

class Sit implements Command{
 private Student student;  
// Command with the Receiver
...
}

public String execute() {
  return student.sit(); //Command Execution
 }

Wednesday, May 30, 2012

Behavioral Patterns - 1.Chain of Responsibility

At times we may need to deal with different objects performing different tasks based on a defined workflow. There may be a need for delegation and processing the tasks between various objects.

Such kind of problems can be solved by the "Chain of Responsibility" pattern.

Consider a situtation where we may need to handle the bank loan approval process.A bank may have approving authorties at various levels like Manager,Director,President,etc.

The approval limit may vary across the levels. For Example, a Manager has a maximum approval limit of Rs.10,000, for a Director it will be RS.50,000 and for a President it will be more than RS.50,000.

Whenever a Manager receives a request for approving Rs.1,00,000, he/she will delegate the request to their immediate predecessor. Here it will be delegated to the Director. Since, the requested approval amount exceeds the Directors limit, it will be furthur delegated to the President. The President can approve the amount.

Here, the responsibility of approval is being delegated to various persons based on their nature. This can be termed as "Chain Of Responsibility".

Please refer the below class diagram for more details on this pattern,



Following code snippet can provide a clear idea over the implementation,

 public void approve(double amount) {
  System.out.println("Manager checking the limits");
  if(amount<=10000)
      System.out.println("Approved!");
  else{
     System.out.println("Approval Limit Exceeds! Delegating to "+predecessor);
     predecessor.approve(amount); //Delegating the responsibility to the predecessor
  }


Code snippet for the client could be,
 
  Approver president=new President(null);
  Approver director=new Director(president);
  Approver manager=new Manager(director);
  manager.approve(200000);

Monday, May 28, 2012

Structural Patterns - 7.Proxy Pattern

Proxy pattern filters and customizes the data you want from the main big subject. Assume, we have a subject with nearly 200 properties into it. But frequently we may need 10 or 20 of them.

So, we will end-up in creating a proxy for the subject by filtering the 10 or 20 properties alone from the main subject.

Whenever we need those properties we may access the proxy instead of the main subject.

This will help us to get way from creating costly objects and business calls.

For example, Student will be our subject which contains data like attendance, marks, age,etc. But we may not interest in all the data except the attendance information. Instead of using the entire Student object, we will create a Proxy to the Student which provides the attendance details alone.

Proxy can add more functionalties before or after getting the attendance details from the Student Object.

Following diagram explains this pattern a bit more,


We can use the proxy object whenever we need to process the attendance for the student.
This proxy will internally communicate with the Student object with a singleton pattern.

Proxy Code snippet in Client:

  Student abc=new ProxyStudent();
  Student def=new ProxyStudent(); //Create Proxies for Student


In the Proxy Class,

public String getAttendance() {
  if(realStudent==null){  //Ensure singleton object for the Subject
   realStudent=new RealStudent();
  }
  return realStudent.getAttendance();
 }


 

Thursday, May 17, 2012

Structural Patterns - 6.FlyWeight Pattern

Have you ever imagined that you are standing in between a huge array of army in the battlefield? The army may consists of horses,elephants,chariots and big mass of soldiers.
Seeing such a scene itself brings a mammoth outlook before your eyes.


How about drafting a design to accomodate the army?

An army may contain thousands/lakhs of soldiers. Think about creating an object for each and every soldier. We have elephants,horses,chariots and soldiers on foot assembled in a disciplined structure with their positions.

How about designing an order of positions for this army?

At first sight, it really looks like a costly effort. Because, we will end up in creating lakhs/thousands of objects to represent the army.

A solution to this type of problems could be a Flyweight Pattern. It helps us to minimize the number of objects created by generalizing the common pattern observed
among them.


This pattern is a collaboration of Factory Method and Singleton design patterns.

Now, let us think a way to simplify the object structure by recognizing the patterns from the army. We can identify some classifications among the soldiers like elephants, horses, chariots and normal soldiers. But, we need to maintain position for each of them.

First, we generalize the soldiers based on their nature, which could be the intrinsic property.So, we will end up in creating just four objects(chariots, elephants, horses, soldiers).

Then, the position to which they need to occupy ,could be more dynamic and explicitly specified.

For example, let us consider we have 1000 soliders in an army.

It consists of 100 chariots, 200 Elephants, 200 Horses, 500 Soldiers and they were positioned in rows 4,3,2 and 1 respectively.

We construct 4 objects for the types of soldiers in an army and we dynamically initialize their position in the battlefield.

Following design explains this approach in a better way,



Soldier is the Flyweight object constructed using the SoldierFactory which is a Flyweight Factory. This flyweight object is a singleton for each and every soldier type.

After receiving the flyweight object client can decide where to position each and every soldier.

FlyweightFactory Code snippet,

private static HashMap<String,Soldier> soldiers=new HashMap<String,Soldier>();
 public static Soldier getSoldier(String type){
  Soldier soldier=soldiers.get(type);
  if(soldier==null){
   soldier=new Soldier(type);
   soldiers.put(type, soldier);
  }
  return soldier;
 }


Client code,

   Soldier soldier;
   for(int i=1;i<=1000;i++){
   if(i<=100){
    soldier=SoldierFactory.getSoldier("Chariot");
    soldier.setPosition(4);
   }
   else if(i<=300){
    soldier=SoldierFactory.getSoldier("Elephant");
    soldier.setPosition(3);
   }
   else if(i<=500){
    soldier=SoldierFactory.getSoldier("Horse");
    soldier.setPosition(2);
   }
   else{
    soldier=SoldierFactory.getSoldier("Soldier");
    soldier.setPosition(1);
   }  


This will result in creating just 4 objects and configure them 1000 times with their position.

Monday, May 14, 2012

Structural Patterns - 5.Facade Pattern

Facade is an exterior view/face of a building. We can understand the whole structure of the building by its exterior view.

But internally, they have lot of rooms, wiring, plumbing, interior decorations,etc., which we may not interested to know.

Day to day we deal with complex business functionalities. We seperate our business requirements into different classes and maintain the inter-operability between them.

We should be able to re-use such components within our application or from an external application whenever there is a need.

Our component may have a complex design with many business processings but we need to provide a simpler interface for other systems to use it. Such type of an interface is a Facade.
A railway reservation system is a complex process involving hundreds of rules or policies while booking a ticket. They have seperate systems to handle the list of stations,trains,quotas,preferences,etc.

We may not have much interest over the internal happenings of the system. We will end up in providing the details about the passenger and the seat preference.

The system will perform the complex processing and generate the ticket for us based on the data provided.


The following class diagram explains the Facade pattern,



Here, the Ticket class offers a simple method bookTicket() which is the facade for the complex ticket reservation process.

 A single call to the bookTicket() will communicate with different systems like stations, preferences, ticket classes, quotas, etc., and generates the ticket for the passenger.

Refer the following code snippet for more details,

// Constructor 

  public Ticket() {
     stations=new Stations();
     train=new Train();
     quota=new Quota();
     preference=new Preference();
 }


// Facade for booking the ticket

public void bookTicket(){
 ticket.setClass(train.getTravelClass());
 ticket.setFromStation(stations.getFrom());
 ticket.setToStation(stations.getTo());
 ticket.setPreference(preference.getPreference());
 ticket.setQuota(quota.getQuota());
 ticket.generate();
}