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.
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
Just think of the abstract Vehicle Factory that can 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.
The 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
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.
The Builder pattern separates the construction of a complex object from its representation, so that the same construction process can create different representation. This pattern is used by fast food restaurants to construct children’s meals. Children’s meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, coke, and toy car). Note that there can be variation in the contents of the children’s meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants.
To remember this pattern you should think of Robot Cloning or Toy Cloning using Doraemon Magic Cloning Mirror.
This is the method of creating object by allowing them to create duplicates of themselves.
The easiest way to remember this pattern is to remember the Universal Adapter.
You create the adapter to transform resource so that other adapter can get from it. That all you should know about Adapter Pattern.
The good example for Composite Pattern should be the robot Voltron which is created by combining five Lion robots.
“The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object” is the only description you should remember
It’s the way of wrapping and hiding the complicated details from the client. Instead it gives the client a simpler interface to do the job.
And the best example of this should be what Sun has applied this pattern into EJB Session Façade.
The best Pattern for adding more features to an existing class without changing it should be the Decorate Pattern which utilize both extending and wrapping of classes.
The good example of this pattern is to extend and add rich features to a simple Text Editor like Notepad.
For you guys study RMI in college may hear of Stub and Skeleton before realize the Proxy Pattern.
The good example to remember is about the Bank and the ATM. Where the ATM is actually is representative of the Bank but most of the work will be dispatched to the only bank to process.
Observer Pattern maybe heard later than Event and Listener by some people. The best example for Observer Pattern is the Publish Subscribe cycle.
Another real life example of Observer Pattern is the mailman. You don’t need to go to the office all the time, all the day to ask for new mail. Instead whenever there’s a new email for you. The mailman will come to your house and knock on your door.
The same thing for your mobile phone to notify you about sms message or new incoming call.
(to be continued…)