Learn Organization, Constitution, Aggregation and you will Inheritance during the C#
In this article we’ll try to understand how to implement dating within the c-clear playing with object oriented principles for example inheritance, connection, aggregation and you can composition which have a straightforward analogy and cause having fun with c-sharp.
Many of these dating is dependant on “was a good” relationships, “has-a” matchmaking and you can “part-of” dating. Therefore i want to determine you this package by you to definitely.
Inheritance predicated on Try-A romance
Inheritance into the C# was a dad-son relationships where i carry out a unique class by using established group password (we.age. recycling measures, characteristics or any other variables). Genetics allows us to recycle of one’s code once again once more function father or mother category would be adopted to the number of boy categories. Genetics spends special profile called “:” anus and come up with a romance.
Within a lot more than analogy our very own mother or father are an effective PQRCompany. SalesManager is a member of staff functions inside PQRCompany and SalesTeam are working on the PQRCompany.Generally there are a daddy-son dating anywhere between PQRCompany-SalesManager and you may PQRCompany-SalesTeam, due to the fact SalesManager and you will SalesTeam really works not as much as exact same organization. Today help place this throughout the coding style.
In lieu of playing with a friends identity as the a grandfather you can additionally use “employee” since a father category because the SalesManager and you can SalesTeam try both teams merely.But it is elective it will alter predicated on condition when the you would like you can keep “Company” as the a dad or “Employee” as the a daddy. Making this exactly how we need use father or mother-guy relationships within our password depending on the circumstance.
Association is dependant on Possess-A love
Relationship is based on “has-a” matchmaking concept. Allow me to describe you the definition using a straightforward analogy. Consider i’ve a couple of more kinds Class1 and a Class2 whenever those two entities share for every other people target for the majority of really works and also at https://datingranking.net/local-hookup/wyoming/ the same time frame they are able to is present in the place of each anybody else dependancy (which have their unique life) there should not be any unmarried proprietor each other should be another away from each other following version of dating is known because the “has-a” relationships we.e. Organization.
Inside example we are going to extract connection principle. SalesManager uses a keen employeecard to get in on the company. Right here we must do several groups “SalesManager” and you will “EmployeeCard” as SalesManager is an employee out of a buddies and providers uses an enthusiastic employeecard so you can authorize their employees therefore each other need to a beneficial various other agencies and at a same we wish to violate unmarried obligation principle setting we can’t render employeecard class jobs in order to salesmanager category.
Unmarried obligations idea states that each and every class want to do her jobs such as for instance Membership category need to do bookkeeping really works and you may Personnel group is to take care of personnel information.
When you see “SalesManager” classification having fun with “EmployeeCard” target to evaluate consent techniques and you may “EmployeeCard” spends “SalesManager” target to check on whether a member of staff try energetic or perhaps not. So each other entities show for each and every anyone else target for the majority of work however, they’re not determined by one another as we are able to have fun with same “EmployeeCard” object various other departments out-of a friends including having “SalesTeam” category, “AccountTeam” group, “Staff” class and stuff like that along with the same way “SalesManager” target may be used in different metropolises instance getting “SalesTeam” category, “ProductSales” class etc.
So these entities features their own lifetime out of object and also at same date they display for each other people target to have some performs. 1st part would be the fact there no proprietor one another entities try equivalent. Whilst you can see i’ve effectively made an association relationship.
Aggregation lies in Keeps-A relationship
Aggregation depends is on “has-a” matchmaking idea. Here one another entities meet for many really works and then rating separated however, right here one to entity must be an owner as well as an exact same big date they must be separate regarding both (having individual life). In relationship idea there isn’t any manager but in aggregation one one to entity will likely be a proprietor to other entity.
Contained in this a lot more than scenario i’ve a couple of agencies “SalesManager” and you can “SalesTeam”. According to the real-world scenario “SalesManager” must be a manager so you can “SalesTeam” setting SalesManager will simply use the target from SalesTeam showing his cluster ideas and at a same day SalesTeam has actually very own target life time function that isn’t influenced by SalesManager. SalesTeam may be placed under BranchHead or CompanyHead. so let me put it about programming build.
Once you see within our more than password that SalesManager classification uses SalesTeam object to show records of their operating group. Here SalesManager class is a holder because spends SalesTeam target for some time as well as an exact same date those two groups features independent of any most other. This means i have effortlessly produced a beneficial aggregation dating.
If you have indexed i implement aggregation inside the three-tier buildings mode operating layer group we spends analysis level category target to show ideas this is when company layer will be a proprietor of data level. Making this including an aggregation dating.
Composition is dependent on Part-Out of Relationship – Passing Relationship
Composition is actually a beneficial “part-of” relationships. In this matchmaking one another organizations try interdependent of each other particularly a muscles is dependant on cardio and viceversa cardio was dependent on looks borh are useless in the place of each other.
Within over code you will find a couple categories “SalesManager” and you can “clsProduct” respectively. As we know typically sales-movie director income will be based upon device selling and you may product offering was along with dependent on sales-movie director. So both are interdependent for each most other. Same manner i generated a layout relationship in which each other agencies life go out is founded on one another.
When you see we can’t load people factors playing with “clsProduct” category while the we truly need “SalesManager” target to evaluate if you to “SalesManager” complete early in the day month address or perhaps not same manner “SalesManager” category plus cannot stream issues rather than “clsProduct” object so we try not to develop “LoadProducts()” approach within the “SalesManager” as it have a tendency to violate all of our solitary obligations idea.So this is how we possess successfully produced constitution relationship.
So this is about relationships within the target established coding. When you yourself have one doubts or query be sure to let me know through your rewarding opinion of course you like this post be sure to share with your buddies or acquaintances. Thanks a lot.
Theo Healthplus.vn
Chưa có bình luận