UML Association vs Aggregation vs Composition

文章推薦指數: 80 %
投票人數:10人

If two classes in a model need to communicate with each other, there must be a link between them, and that can be represented by an association (connector). Home Support Current:UMLAssociationvsAggregationvsComposition UMLAssociationvsAggregationvsComposition AssociationvsAggregationvsComposition Association AssociationvsAggregationvsComposition GeneralizationvsSpecialization GeneralizationvsInheritance RelatedLinks Considerthedifferencesandsimilaritiesbetweentheclassesofthefollowingobjects:pets,dogs,tails,owners. Weseethefollowingrelationships: ownersfeedpets,petspleaseowners(association) atailisapartofbothdogsandcats(aggregation/composition) acatisakindofpet(inheritance/generalization) Thefigurebelowshowsthethreetypesofassociationconnectors:association,aggregation,andcomposition.WewillgoovertheminthisUMLguide. Thefigurebelowshowsageneralization.WewilltalkaboutitlateroninthisUMLguide. LearnUMLwithFreeUMLTool AreyoulookingforaFreeUMLtoolforlearningUMLfaster,easierandquicker?VisualParadigmCommunityEditionisaUMLsoftwarethatsupportsallUMLdiagramtypes.Itisaninternationalaward-winningUMLmodeler,andyetitiseasy-to-use,intuitive&completelyfree. FreeDownload Association Iftwoclassesinamodelneedtocommunicatewitheachother,theremustbealinkbetweenthem,andthatcanberepresentedbyanassociation(connector). Associationcanberepresentedbyalinebetweentheseclasseswithanarrowindicatingthenavigationdirection.Incaseanarrowisonbothsides,theassociationisknownasabidirectionalassociation. Wecanindicatethemultiplicityofanassociationbyaddingmultiplicityadornmentstothelinedenotingtheassociation.TheexampleindicatesthataStudenthasoneormoreInstructors: Asinglestudentcanassociatewithmultipleteachers: TheexampleindicatesthateveryInstructorhasoneormoreStudents: Wecanalsoindicatethebehaviorofanobjectinanassociation(i.e.,theroleofanobject)usingrolenames. AssociationvsAggregationvsComposition Thequestion"Whatisthedifferencebetweenassociation,aggregation,andcomposition"hasbeenfrequentlyaskedlately. AggregationandCompositionaresubsetsofassociationmeaningtheyarespecificcasesofassociation.Inbothaggregationandcompositionobjectofoneclass"owns"objectofanotherclass.Butthereisasubtledifference: Aggregationimpliesarelationshipwherethechildcanexistindependentlyoftheparent.Example:Class(parent)andStudent(child).DeletetheClassandtheStudentsstillexist. Compositionimpliesarelationshipwherethechildcannotexistindependentoftheparent.Example:House(parent)andRoom(child).Roomsdon'texistseparatetoaHouse. CompositionExample: Weshouldbemorespecificandusethecompositionlinkincaseswhereinadditiontothepart-ofrelationshipbetweenClassAandClassB-there'sastronglifecycledependencybetweenthetwo,meaningthatwhenClassAisdeletedthenClassBisalsodeletedasaresult AggregationExample: It'simportanttonotethattheaggregationlinkdoesn'tstateinanywaythatClassAownsClassBnorthatthere'saparent-childrelationship(whenparentdeletedallitschild'sarebeingdeletedasaresult)betweenthetwo.Actually,quitetheopposite!TheaggregationlinkisusuallyusedtostressthepointthatClassAinstanceisnottheexclusivecontainerofClassBinstance,asinfactthesameClassBinstancehasanothercontainer/s. Summingitup- Tosumitupassociationisaverygenerictermusedtorepresentwhenoneclassusedthefunctionalitiesprovidedbyanotherclass.Wesayit'sacompositionifoneparentclassobjectownsanotherchildclassobjectandthatchildclassobjectcannotmeaningfullyexistwithouttheparentclassobject.IfitcanthenitiscalledAggregation. GeneralizationvsSpecialization Generalizationisamechanismforcombiningsimilarclassesofobjectsintoasingle,moregeneralclass.Generalizationidentifiescommonalitiesamongasetofentities.Thecommonalitymaybeofattributes,behavior,orboth.Inotherwords,asuperclasshasthemostgeneralattributes,operations,andrelationshipsthatmaybesharedwithsubclasses.Asubclassmayhavemorespecializedattributesandoperations. SpecializationisthereverseprocessofGeneralizationmeanscreatingnewsub-classesfromanexistingclass. ForExample,aBankAccountisoftwotypes-SavingsAccountandCreditCardAccount.SavingsAccountandCreditCardAccountinheritthecommon/generalizedpropertieslikeAccountNumber,AccountBalance,etc.fromaBankAccountandalsohavetheirspecializedpropertieslikeunsettledpaymentetc. GeneralizationvsInheritance GeneralizationisthetermthatweusetodenoteabstractionofcommonpropertiesintoabaseclassinUML.TheUMLdiagram'sGeneralizationassociationisalsoknownasInheritance.WhenweimplementGeneralizationinaprogramminglanguage,itisoftencalledInheritanceinstead.Generalizationandinheritancearethesame.Theterminologyjustdiffersdependingonthecontextwhereitisbeingused. LearnUML.DrawUML. DoyouwantaUMLtoolthatcanhelpyoulearnUMLfaster&moreeffectively?VisualParadigmCommunityEditionisafreeUMLsoftwarethatsupportsallUMLdiagramtypes.ItsUMLmodelerisaward-winning,easy-to-useandintuitive. FreeDownload RelatedLinks WhatisUnifiedModelingLanguage? ProfessionalUMLtool Turneverysoftwareprojectintoasuccessfulone. TryVisualParadigmFree Weusecookiestoofferyouabetterexperience.Byvisitingourwebsite,youagreetotheuseofcookiesasdescribedinourCookiePolicy. OK



請為這篇文章評分?