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 »

Jul 212011
 

What is Singleton?

Singleton is one of well-known design pattern invented to allow only one instance object being created from a specific class but it will be shared between other processes that use the object. searchdaily net java singleton design pattern thumb Singleton pattern tutorial

When should I use Singleton?

The answer is vary but depend on your need to decide between using Singleton or not. Here I give you some example where they often use Singleton:

  • Application always need a single log file or log console so that logging can be written from every objects/processes that are running, in this case they use Singleton for the log object.
  • Singleton is used in managing database connections. Why? because the number of database connections are a limited and shared resource. It cannot serve more than what it has but the need is often higher than availability so it need to be shared. With one connection manager we can manage to share these db connections between those who need.
  • File manager, print spooler, framework configuration file.. are the same as database connections as I mentioned above so the same technique will be applied.

How to create Singleton pattern?

  1. Make private all constructor even the default constructor because we will not allow object creation from the outside.
  2. Create a private static instance of the class. This is the only object of the class that will return to the caller.
  3. Because the instance is static then finally we will create a synchronized static method that return the instance. Continue reading »