Posts

Showing posts from July, 2011

Composition over Inheritance

We had often heard that composition is better than inheritance. Why is it so? First of all how is one different from the other and what are the similarities in them? Lets say we are writing simulation software for Rocket Launching systems which are to be supplied to different countries. Now these different countries can use them as they want it. The code for our launching system is below: public   class   Launcher {      public   bool  LaunchMissile()     {          Console .WriteLine( "Missile launched" );          return   true ;     } } public   class   SufraceToAirMissileLauncher : Launcher {   } Now, country A uses this code to launch missile as follows: static   void  Main( string [] args)     {          SufraceToAirMissileLauncher  staLauncher =  new   SufraceToAirMissileLauncher ();          bool  isLaunched =   staLauncher.LaunchMissile();     } This is how Inheritance is used. The various launchers can reuse the base Launcher class code to launch m

Observation pattern in C#

Observer design pattern in the most common used pattern in software world.Infact,in the real world also,there are many instances where we are following an observer pattern e.g. subscribing to newspaper edition,waking up in the morning to goto office when the alarm rings,etc. Lets come straight to the intent – The intent of the pattern is to define a  one-to-many  dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Ok,lets begin to see its implementation – Observer pattern revolves around subject and   observers. The subject is someone in whose state observers are interested. Taking the example forward,lets say we have a fictitious adventure company called GreatWhiteCompany which is into the business of adventure activities. I once did a rafting expedition through them and seeing their professionalism decided to subscribe to their newspaper.So,here I am the observer and needs to be notified whenever there is a go