Posts

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)     {          SufraceToAirMissileLaunc...

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...

Merging of linked lists

Image
In this article ,I will be discussing some problems based on merging of linked lists. Problem statement : We had 2 linked lists - a1-a2-a3.....aN and b1-b2-b3..........bN We want to merge it into a single linked list - a1-b1-a2-b2-a3-b3...........an-bn Solution : We would be using the linked list we had already developed in previous blogs. I am again showing the structure of this list below : There would be a simple function in this list which takes 2 nodes and merges them both into the main linked list.The given function is : The simple strategy here is to iterate through the 2 lists and keep on adding current node of of each list to the original linked list. Below is a test program to test it. One variation of the above problem could be to write a function in LinkedList class which accepts a linked list and merges it with itself. Problem statement : Merging of 2 sorted linked lists. Given 2 sorted single linked lists - first : 1-2-3-4-5-8-100-120 second : 5-7-11-34...

Some basic linked list problems

Image
In my earlier  post  , we had seen how to implement a basic linked list in C#. We also saw both stack and queue based additions to the linked list. In this post we are going to look at some of the basic linked list problems which will prepare us for more adventure ahead.We will start with basic node class. We would keep adding functions in the LinkedList class as we program. Right now the list is empty with only 2 nodes- head and current which both point to null.Below I am giving the code for simple insertion and printing of nodes,once they are added. Below I am giving code for some functions which can be added to this class. These are : Node Find Find a node in the list.It returns the node when it is found.If the corresponding node is not found null is returned. The next problem is to remove a node from the list. Here we would create a small helper function which would return a previous node. The given functions are below : The next problem is Reversing a list . ...