Tuesday, November 6, 2007

Open-Closed principle

This is one of the fundamentally principles of OO design. It basically states “Software entities should be open for extension, but closed for modification.”


For example image you have a Soda machine that gives you 2 types of sodas Coke and Pepsi. Let’s implement it. At first it may look as below.

Imagine few months down the line you got tired of drinking only Coke and Pepsi and wants to add Sprite. You have to create a new method in SodaMachine class called getSoda () that takes Sprite as argument, which means we have to change SodaMachine class whenever you want to add new soda, so SodaMachine class is not closed for modifications.

Now, lets apply OCP, it will look as follows.

Now if you want to add a new drink you do not have to change SodaMachine class at all (see parameter type of getSoda()).


So all you have to do is create a new class called Sprite that implements SodaType interface. This tells us that SodaMachine is closed for modifications but you can still extend its functionality for any other type of sodas.


Monday, November 5, 2007

Resign Patterns

While looking around the web, I cam across this funny link. Many of us agree that patterns help us to design and understand systems better, but on the lighter side http://www.lsd.ic.unicamp.br/~oliva/fun/prog/resign-patterns