c# (Csharp) :- Knowledge sharing session (Aggregation and Composition)
Articles Blog

c# (Csharp) :- Knowledge sharing session (Aggregation and Composition)

August 25, 2019

Hello hey Guru can you hear me so what are be learning today and what’s topic today we are going to talk about two important principle of object oriented programming that is aggregation and composition which is use to create a relationship in the object oriented programming so as i said aggregation and composition so what composition means is a part of relationship so what i mean a part of relationship it’s very simple for example hart can not stay with a human body or human body can not survey without a hurt like wise second a car can not run without engine engine is uses without a car this is means a composition and aggregation is also very simple aggregation means has a relationship has a releationship means a person everyday changes his dress it’s shirt means he can survive without shirt also he can were another shirt he can were another T shirt also means shirt can and is particular shirt can survive without a person also means this person is not wearing shirt the another person were that shirt it’s not interdependent it is a isolated so in other words when you saying that composition means both of these guys can not stay with that each other the two entities can not stay without each other while in aggregation the two entities come for a short time and then after words the depart but there are not depended in each other they can survive individual in composition there two entities are dependent on each other and in aggregation the two entities are isolated means not depended in each other so if you can see some code must that would really add value because i have simple nice example this is a simple windows form application which i created in C# so let me just go and show you the demonstration of my code this is my solution explorer something looks like this i have a application windows form application i have added simple class library inside that i have a business layer and data layer so let me go into my data layer i have a two class cls customer and cls product means customer can have a multiple product that is a logic i have just open my customer class let me talk about aggregation part first because it is coming here only as you can see that i have aggregated the data layer here that is obj data layer so i created the object what i am mean i aggregated means this data layer class is not depended on this customer this cls entity this entity is not depending open this entity because if we delete the cls customer also still this class cls data layer class is going to survive because if a class another class comes like a public class employee so probably he can use the data layer class yes he can use the data layer class also something like this this both entities are isolated there are not depended on each other means one entity life ton is not depended upon another life tone of object so they are both isolated we can plug and play this data layer to any entity so in other words the data access layer is very generic component it can be used any where must be the customer class will only used when it will be insert update and delete that’s it only at that time to use it and then go away so this is the simple example of an aggregation now let’s come to the composition part as i told you composition is a part of relationship one entity is life time object it’s depending open another entity life time as i told you i have two class here cls customer and cls product let me just open mu cls product in this cls product constructor i have passed my this object that is cls customer i created the object of cls customer and inside the cls customer when i am loading a products i just created the object of cls product which means let me just show you the code but the question is that does the product really need’s the customer i am just queries that why is it customer class pass to the product class because while loading a product we need to check if the customer is active or not means if is a expired customer mean there is a unnecessary of loading a products of that customer particular customer so this something checks if the customer is active just properties check if the customer is active then load a customer else don’t load a customer the basically what we have saying is that the product class has a property called as customer active which depends on the type of the customer which depends on some data of the customer means the product class will only go and perform certain things if the certain things on the parent are proper like example the customer active here there is a interdependence here in other words if this customer is active the customer class is active the customer property that is customer is active that is this one then it will go and load all the products meand it is interdependence compound the parent class that is cls customer if the client developer want’s to create a object of the product that is something like this interdependence something like this cls product equal to new cls product then he needs that cls customer object also so in other words you have not given in the default construct any more you have saying that whenever you are going to create a product class you have to give me a customer class yes because there he actually checks this customer is active then certain things this one class is interdependence and we can not load a customer without a product class if i take this product class out if i take this product class out it is not going to load because inside here while loading a product that is here that is here let me just debug this application let select the customer inside here while loading a customer i am just creating a object of a product means a customer class also can not load a products without a product object so you needs a product object means a customer class also needs a product object and the load product is a work of the product it can not come in the customer that you have to depend on the product the load the product means you can not put a load product inside the customer that would violet our rule of single responsibility principle also i think Guru what you are saying that basically if the properties of two classes are depended on each other for example here you know that is active decides product should be loaded or not and the same way the customer is using the products to load so when this guys are completely intermingle and or the property are depended on each other and at that time the composition relationship stand to both the entities are interdependent on each other it’e means it is a composition when both are isolated simple it means aggregation and Guru we can get this source code also in-case somebody want’s to have this source code we can always email to them yes you can get this source code OK this is good GURU so it was nice lesson where we talk about aggregation and composition and as GURU said that composition means to object life time are same they are depended on each other they can not stay without each other well aggregation means basically one object both of the object are not depended each other they are decoupled and they only come together fro certain period of time and do certain things and go away THANK YOU VERY MUCH 🙂

Only registered users can comment.

  1. The tutor need a lot of fine tuning for his teaching skills. Need improvement for english as well. I am not a "Cambridge" pass out but still "I have a two class" sounds really awkward.

  2. Sample like car without engine or invoice without header are not showing real meaning of Aggregation and Composition. Use something more natural, like house and rooms (Composition) and your lovely invoice and DAO (Aggregation).

  3. Sir request you to please do not zoom over any function, let your whole code window should visible to complete picture. We can clearly see the coding content as easily.

Leave a Reply

Your email address will not be published. Required fields are marked *