Sep 082011
 

Go around and look for some method to remember Design Pattern, I saw a site summarize each Design Pattern with a line like this:

  • Abstract Factory: Creates an instance of several families of classes
  • Prototype: A fully initialized instance to be copied or cloned
  • Singleton: A class in which only a single instance can exist

But how can you remember it all? I cannot imagine that I can recall “Abstract Factory” is “Creates an instance of several families of classes” at all.

Design Pattern is so abstract and so hard to remember, so each time I write an entry about Design Pattern I do my best to put an easy to remember example. I believe the real life examples will be easier to understand and remember than the descriptions and principles.

Now let’s me summarize some Design Patterns by using Images of real life example.

 Singleton Pattern
searchdaily net java singleton design pattern thumb Remember Design Patterns by Images singleton pattern example images thumb Remember Design Patterns by Images

 

It’s the pattern of single service but multiple user like: there’s one receptionist at a hotel and there are multiple visitors. There is one President in a country and million citizens to manage

 Abstract Factory Pattern

Just think of the abstract Vehicle Factory that ferrari vs audi r8 thumb Remember Design Patterns by Imagescan produce any cars. And there are real Vehicle Factory like one is based in Germany (Audi and Mercedes) or factory is based in Italy like Ferrari and Iveco.

The same type of  products are created by different factories give different result in the end.

Factory Method Pattern

image thumb7 Remember Design Patterns by ImagesThe Factory method is much like Abstract Factory: to create different objects. But the difference is that Factory Method is not based on the different Factories but to base on different Parameters passed to the creator.

Take the calculator for example, base on the type of calculator we select, the different calculators will be created

 

Chain of Responsibility Pattern

The Chain of Responsibility pattern avoids coupling the sender of a request to the receiver, by giving more than one object a chance to handle the request. Mechanical coin sorting banks use the Chain of Responsibility. Rather than having a separate slot for each coin denomination coupled with receptacle for the denomination, a single slot is used. When the coin is dropped, the coin is routed to the appropriate receptacle by the mechanical mechanisms within the bank.

chain of responsibility pattern thumb Remember Design Patterns by Images

Continue reading »

Sep 082011
 

You may hear of “Observer” or “Listener” somewhere especially when working with Swing or AWT. Do they have a difference? Some guys may even confuse between Observer and Listener, while actually they are the same concept in Java.

What is Observer Pattern?

Observer is a Behavior Design Pattern defined by GoF and the definition is maybe like this: “The observer pattern (a subset of the publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.” – from wiki.

Now let me take you to a closer look by observing an example:

observer pattern example thumb Observer Pattern Java Example

As you can read from the diagram, I will use a cellphone (BlackBerry in this case) to demonstrate this pattern. Each time there’s a call or an sms coming, a BlackBerry can notify you by multiple ways: via the ringtone, via a LED indicator or or via the Vibration…I treat these devices as the Observer object which will subscribe the SMSReceiver all the time for new SMS. If new SMS comes, these devices will be notified by the SMSReceiver to work accordingly.

Observer Pattern Example in Java

Now let’s see how we will explain the above example again in Java language.

blackberry sms receiver thumb Observer Pattern Java Example

First I will define the BBObserver object that can make BlackBerry device do something when some event happens.

package net.searchdaily.java.design.pattern.observer;

/**
 * Observer pattern example by searchdaily.net
 * 
 * @author namnvhue
 * 
 */
public interface BBObserver {
	public void handleEvent();
}

Next is for the BBObservable object that can contain a list of subscriber object, and when some other objects tell it, it will notify all other subscribers about the event.

Continue reading »

Aug 202011
 

chain of responsibility design pattern 1 thumb Chain Of Responsibility Pattern Tutorial

If the above diagram doesn’t give you enough definition about the Chain of Responsibility. Here is a good one: The Chain of Responsibility is a Behavior Design Pattern “to establish a chain within a system, so that a message can either be handled at the level where it is first received,
or be directed to an object that can handle it”

java chain of responsibility thumb Chain Of Responsibility Pattern Tutorial

This design pattern will help you to divide an abstract job (Declare in Handler) into specific tasks that can be completed by Concreted Handler.

Now I will give you a small example for better understanding:

java chain of responsibility diagram thumb Chain Of Responsibility Pattern Tutorial

Suppose that we have a Client (Hungry Customer) who enter a Restaurant (Food Processor) and said: “Hey, give me a Vegetarian Dish”. The Clerk (CookerHandler) in the restaurant will ask the Customer for type of Food and then forward the job either Vegetarian Cooker or Non-Vegetarian Cooker, but somehow he gave it to Non-Vegetarian-Cooker first. So in this case, the message first received by the Clerk but forwarded to the specific skillful Cooker later.

Let’s see how we can demonstrate them in Java:

First we define the Dish that will contain the result of  the cooking.

package net.searchdaily.java.design.pattern.chainofresponsibility;

/**
 * Chain of Responsibility Tutorial by http://java.searchdaily.net.
 * 
 * @author namnvhue
 * 
 */
public class Dish {
	private String name;
	private boolean vegetarian;
	private double price;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isVegetarian() {
		return vegetarian;
	}

	public void setVegetarian(boolean vegetarian) {
		this.vegetarian = vegetarian;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

}

Continue reading »