Python Tutorial for Beginners 31 - Python Aggregation + Difference in Aggregation and Composition



 hey guys welcome to the next blog in Python tutorial for beginners in the last blog we have seen how to use composition between classes in Python in this blog I'm going to show you how to use class aggregation between classes in Python now if you haven't watched my last blog in which I have shown you how to use class composition in Python I will highly recommend you to watch that blog because I will be using the same code to demonstrate how aggregation works in Python so in the last blog we have created two classes one is an employed loss and other is the salary class and then we have used the celery class inside the employee class and we have seen how composition works in Python so employee class is delegating some part of its responsibilities to the celery class and that is called the composition now in composition the relationship between the classes which have some association is represented by a key word part of so celery is the part of employee so the key word we use in composition is part of now let's see how we can use aggregation instead of composition between these two classes so let's say I will not use this salary class inside the employee class but what I'm going to do is I'm going to pass an instance of celery here so I'm going to just write celery and this will be the object of celery class which I'm going to pass in the init method of the employee class so let's create an object of the celery so I'm going to just write salary here is equal to the salary class and we will instantiate the celery class by these two argument which we have given in the last blog to the employee class so I'm going to just copy these two arguments and pay it here and I'm going to remove this comma here so now what we are doing here is we are first of all instantiating the salary class and now the salary class we can pass to the constructor of the employee class and this is the object which we have passed using the salary class object and then I'm going to take the salary class object and I'm going to assign the salary class object to the member variable of the employee class which is obj underscore salary now because we are passing the salary object that means this member variable is the salary member object and we can call any public method from the salary class using this member variable now so let's run the code and let's see what happens and we get the same result as we have seen in the last blog in the composition example so what we have done differently here so instead of using this salary class inside the employee class we have first created an instance of the salary class and then we have passed this instance to the employee constructor which can be used inside the employee class and this type of relationship is called aggregation now let's see what are the special properties of the aggregation the first property is the relationship between the associated classes in aggregation is defined by the keyword has a so employee has a salary because now we are first of all creating the salary object and then passing it to the employee so now we are not directly using salary inside the employee class the second property of aggregation is the associated classes have uni-directional Association so we are just passing salary object to the employee class we are not passing employee object to the salary class so it's a unidirectional Association only salary can be passed to employee not the other way around and the third property of aggregation we have created the celery object and the employee object so both these objects are independent of each other so if one object dies the other survived so both the object are able to survive individually so these are the three properties of aggregation now let's see the difference between the composition and the aggregation so I'm going to split the screen so I'm going to just right click on this tab and I will say split vertically and here I'm going to open the example of the composition so on the right hand side I have the example of composition and on the left hand side I have the example of aggregation let me just maximize this so you can see it and now let's discuss about the differences between the composition and aggregation so the relationship in composition is defined by a keyword called part of so salary is the part of employee in aggregation there is no part of relationship but the relationship is defined by the keyword called has a so now in aggregation we are defining the instance of salary and then passing it to the employee class and salary is not the part of the employee class we are just passing it to the employee class so aggregation we use has our relationship and in composition we use part of relationship the second difference is when you delete the employee object the salary object will be automatically be deleted so salary object is dependent on the employee class but in aggregation the salary object and the employee object are individually independent so they can survive individually if one object dies then it doesn't matter that the other object dies because we have created these object individually now in composition both salary and employee are interdependent or each other but in aggregation the relationship is unidirectional so the association between the employee and the salary object is unidirectional we can only pass the salary to the employee class but we cannot pass the employee object to the salary class so these are the three major differences between composition and the aggregation I hope you have learned something new this time and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News