This is a mirror of official site: http://jasper-net.blogspot.com/

The Gang of Four patterns as everyday objects

| Thursday, April 28, 2011
A System Metaphor is what eXtreme Programming use to describe an architecture: for example you can think of particular application as production lines, or flying planes, or other kinds of machines. A metaphor has its limits in describing a concept, but it's really fast in being understood by programmers who are naive to the concept.

The original Gang of Four design patterns can be explained as real, everyday (and sometimes science-fictional) objects and actions. In this list, sometimes I use other software as the metaphor, as you will be already familiar with it if you are reading here. But I try to stick to physical objects and systems when possible.

Creation patterns
An Abstract Factory is a factory conforming to some specification, like a phone factory. The concrete phones you can buy are returned by this Factory. However, the standardization is not only in the interaction with the phones (each has 0-9 keys and a call button) but also in the interaction with the Factory (for example suppliers for large companies should follow standard supplier agreements).

A Builder is a very patient architect (the one that builds houses, not software): you tell him that you want the kitchen on this floor, and two bathroom and so on, but he cares about making it work and draw a complete design which accounts for electric wiring, pipes, structural integrity and so on.

A Factory Method is a cake mix; most of the work is already done and the recipe is written, but you may (or must) provide the missing ingredients when required by the recipe. The missing ingredient may also not be edible: the mold where you put the dough can be considered an abstract Factory Method.

A Prototype is a gremlin. Once you have it, you put it in the water and you can instantly create another one (beware not to feed your Prototype pattern after midnight.) Other metaphors are cell division, or any cloned animal like the sheep Dolly (actually only if you intervene on the genes it's a Prototype pattern: if you only create exact copies it's not very useful.)

A Singleton has no correspondence in real life, because it is in fact a bad idea. It will be like a sheet of paper that when written manages files in your pc, or a credit card that charges itself without a PoS. Some says titles such as the US president can only be held by one person, so they are Singleton. Yet there are many presidents (Clinton, Bush, Obama...) and if you ship products in Europe they don't really need a reference to the US president.

Structural patterns
An Adapter can be simply an AC adapter for laptops, but also any kind of electrical adapter between different outlet types. Every geek has lots of adapters in his house so this is the easiest pattern to explain.

A Bridge is an operating system which uses drivers (thus, every OS). The interface for the client (fopen() and similar functions) are evolved separately from drivers for hard-disks, optical devices, and Flash memories.

Continuing with the electrical metaphors, a Composite is a power strip, where you can plug in many devices with a single outlet. In mathematics instead, every arithmetic expressions is a Composite which can be combined with other ones to build a longer, more complex expression.

A Decorator is a layer of colored glass: in particular sunglasses and augmented reality (or 3D) glasses fit the pattern. They do not change your interface with the world (pair of eyes), but they are interchangeable, add some kind of behavior or filtering and can be overlapped in any order.

A Facade is a customer support desk, or even a wedding planner: he hides from you the complexity of a large subsystem. The wedding planner orders flowers, makes reservations, organizes everything for you.

A Flyweight: preforked processed on the web server, although that says nothing about the idiomatical implementation of flyweight we use in oo languages

A Proxy is each SSH client you run to access a shell on another machine. Also VNC and other graphical proxies are equivalent.

Behavioral patterns 
A Chain of Responsibility can be thought of as a military chain of command, where each request may be escalated to the superior officer. Also some business are an example, at least the ones that let you say "Can I talk to your boss?"

Read more: Agile Zone

Posted via email from Jasper-net

0 comments: