uml - Differentiate aggregation and composition relationship using source code -
is possible differentiate between composition , aggregation relationship reading source code?
i tried find patterns , have listed them below.
i taking example this site explain assume pattern
composition
public class engine { . . . } public class car { engine e = new engine(); ....... }
aggregation
public class address { . . . } public class person { private address address; public person(address address) { this.address = address; } . . . }
i find these patterns differentiate
composition (is part of )
defined field of class.
- example: [engine e] engine defined field e of class car
instantiated , assigned within class.
- example: [engine e = new engine();] engine instantiated inside class
aggregation (has a)
defined field of class
- example: [private address address;] address defined field address of class person
instatiated out side class
- example: [address address = new address();] address instatiated outside person.
assigned in constructor sending instance argument constructor.
- example: [person person = new person(address);] instance of address passed argument through constructor , assigned in constructor of class person.
can consider these differentiate aggregation , composition relation?
are there more constraints used differentiate?
not really, because there's not unique way implement each kind of association (in fact, problem have 3 kinds of associations, "normal associations", "aggregations" , "compositions").
if language has pointers try guess if engine defined in car pointer programmer wrote piece of code suggesting softer relationship (aggregation or association) between cars , engines since removing car not imply losing engine object.
Comments
Post a Comment