Jan 032013
 

We talked about the Facade design pattern yesterday which provides a wrapper over a sub-system (or a set of functionality.

Adapter design pattern also provides a wrapper over the functionality of a class, the difference is that Adapter convert the interface of a class into another interface which clients expect.

Adapter, the structural design pattern, lets classes work together that could not otherwise because of incompatible interfaces. Hence it is an absolute wrapper over a class (and not a wrapper over a functionality as Facade pattern).
Continue reading »

Jan 022013
 

Facade is a structural design pattern. It defines a higher-level interface that makes the subsystem easier to use. 

Facade means the mask (मुखौटा) and the pattern is nothing but a simplified interface to a more complex system by providing  a wrapper for the sub-system.

Consider starting your car, let us see what happens under the hood when you turn the key (or press button) to start your car:

car_engine

  1. First the starter motor comes to life,
  2. then the engine begins to turn and the spark plugs fire.
  3. When the key is turned, the starter motor “turns the engine over,” which means it turns the crankshaft.
  4. Crankshaft, in turn gets the pistons moving in the cylinders.
  5. This starts the engine cycle.
  6. Air and fuel is drawn into the cylinders, it is compressed and the then the spark plugs fire.
  7. This begins combustion.

And your car is now Start. To do all this there are 3 sub-systems:

  1. Battery (marked as ‘1’ in the diagram).
  2. Charging System (marked ‘2’ in the diagram).
  3. Starting system (marked ‘3’ in the diagram).

Imagine if there is no key and you have to start the engine yourself by doing  all the above tasks.. It will be cumbersome.

The key is a FACADE which hides this complicated system of Car-Starting. Continue reading »

Dec 282012
 

Multiton is a Creational design pattern. It is just an extension of Singleton Pattern.

Singleton allows creation of only one object of the class. The constructor is made private and objects are created only thru a static function which returns instance of the object. When this function is called first time, a new object of the class is created and its reference is stored in the class. Subsequent call to the function returns the same object (whose reference is stored).

Hence only one object gets created, while the multiton allows keeping multiple instances (exactly n instances, where n is defined) by maintaining a map of related keys and unique objects.

Note that there can be only one instance per key when implementing the multiton pattern. Also, note that the key does not have to be a string value.

While requesting for an object, you need to pass a key (to the static method which returns the object).

The method will check, if an object corresponding to the key is already generated then that obejct is returned, else a new object is created corresponding to that key and that object is linked with the key and returned.

Essentially, the multiton provides the functionality of a group of singletons. One object per key (rather than one object per application)

Continue reading »

Dec 202012
 

courtesy: wikipedia

Have you gone to bowling ? You have to change your shoes before actually get to the bowling arena.

There are lot of shoes in the shoe-shelf, you just need to pick the shoe that fits you, put it on and start playing.

Once you are done, you return the shoes to the shelf, put on your original shoes and leave.

The shoe shelf is a typical real-life example of Object pool (shoes being the object). Imagine if a guy there says, let me first construct the shoes of your number and you have to wait till he prepares the shoes from scratch. Can you afford that long wait?

Same is the motivation for Object pool design pattern.
Continue reading »

Dec 192012
 

Think of an Interior decorator. He will be performing the below two steps to suggest the decoration of interior to any client:

prototype

For each client that comes to the designer, he will do the above two steps.

The second step (designing the hall) is customized according to requirements of the client, but first step (creating raw hall) is same for everyone. Hence it makes a perfect sense to have it ready as a prototype (i.e clone the same object of the class).
Continue reading »

Dec 172012
 

Let’s understand it by understanding the assembling of computer. When you buy computer (in India), you have two choices,

  1. Buy the entire computer from a manufacturer (like HP or Dell), the way we buy laptops, or,
  2. Get it assembled.

For this post, we are only interested in the second option.

When you are assembling your PC, you have an option of selecting which Mother board will you use (Intel / AMD etc.),  you can choose the CD/DVD drive (Samsung , Asus, Sony, LG), you can choose which Hard disk you will use (Sony, Asus, Segate etc.), you can choose which company’s (and of what size) RAM will you use, and so on..

This way we are assembling our computer. Final product is the computer which consists of small objects (RAM, Hard disk motherboard, DVD drive etc).

Similarly in Software, an object of a big class (say College) will hold objects of smaller classes (say Library, Lab, etc).. while constructing the object of the bigger class, you have to construct the objects of smaller class also. This process of building the object of a bigger class is a very good case for applying the Builder Design Pattern
Continue reading »

Oct 232012
 

The Factory design pattern is an object-oriented Creational design pattern. It implements the code to allow creation of objects the way products are created in factories.

The essence of this pattern is to “Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. Hence, deferring (postponing) the instantiation to subclasses.” A super-class specifies standards and generic behavior (using pure virtual “placeholders” for creation steps), and then delegates the creation details to sub-classes that are supplied by the client.

Factory Method is to creating objects as Template Method is to implementing an algorithm. 
Continue reading »

Sep 142012
 

The Singleton Design Pattern requires Constructors to be defined as private member of the class (Default constructor, Copy constructors and Overloaded Assignment operator should all be defined private in case of C++). This pattern is explained here..

If the programming language you are using does not allow you to define the constructor as private or protected (ex. Actionscript), then how will you implement the Singleton class?

Continue reading »

Sep 082012
 

Little and big endian are two ways of storing multibyte data ( int, float, etc) in memory.

– If machine is big endian, then first byte of binary representation of multi-byte data is stored first.
– If machine is little endian, then last byte of binary representation of multi-byte data is stored first.
Continue reading »