Wednesday 2 September 2015

UML Association Types









Compostion
We refer association between two objects as composition, when one class owns other class and other class can not meaningfully exist, when it's owner destroyed, for example Human class is composition of several body parts including HandLeg and Heart. When human object dies, all it's body part ceased to exist meaningfully, this is one example of CompositionAnother example of Composition is Car and it's part e.g. engines, wheels etc. Individual parts of car can not function, when car is destroyed. While in case of Aggregation, including object can exists without being part of main object e.g. a Player which is part of a Team, can exists without team and can become part of other teams as well. Another example of Aggregation is Student in School class, when School closed, Student still exist and then can join another School or so.  In UML notation, composition is denoted by a filled diamond, while aggregation is denoted by an empty diamond, which shows their obvious difference in terms of strength of relationship. Composition is more stronger than Aggregation.  In Short, relationship between two objects is referred as association, and an association is known as composition when one object owns other, while an association is known as aggregation when one object usesother object.

Read more: http://javarevisited.blogspot.com/2014/02/ifference-between-association-vs-composition-vs-aggregation.html#ixzz3kbzDRNCY


Read more: http://javarevisited.blogspot.com/2014/02/ifference-between-association-vs-composition-vs-aggregation.html#ixzz3kbyrMqzS


Read more: http://javarevisited.blogspot.com/2014/02/ifference-between-association-vs-composition-vs-aggregation.html#ixzz3kbwmlh5G
Relationships
Relationships provide a pathway for communication between objects
If two objects need to communicate, there should be a link between the two.

Three types of relationships are:
  • Association
  • Aggregation / Composition
  • Dependency
  • An association is a bi-directional connection between classes
    An association is shown as a line connecting the related classes
    An Aggregation / Composition is a stronger form of relationship where the relationship is between a whole and its parts
    An Aggregation is shown as a line connecting the related classes with an unfilled diamond next to the class representing the whole. For Composition, the dialog will be filled.
    A dependency relationship is a weaker form of relationship showing a relationship between a client and a supplier where the client does not have semantic knowledge of the supplier
    A dependency is shown as a dashed line pointing from the client to the supplier


    Both Composition and Aggregation are form of association between two objects, but there is subtle difference between composition and aggregation, which is also reflected by their UML notation. 

    Read more: http://javarevisited.blogspot.com/2014/02/ifference-between-association-vs-composition-vs-aggregation.html#ixzz3kbwRL6lt

    Relationships

    AssociationDefines a relationship between classes. Compositon and Aggregation are types of associations
    Compositionthe Employee is encapsulated within the Company . There is no way for the outside world to get a reference to the Employee. The Employee is created and destroyed with the company
    AggregationThe Company also performs its functions through an Employee, but the Employee is not always an internal part of the Company . Employees can be exchanged, or even completely removed. As the employee is injected, the Employee reference can live outside the Company.
    DependencyThe company does not hold the employee reference. It receives an employee reference only to the scope an operation. Company is dependent on the Employee object to perform an operation
    AbstractionDefines the basic operations the implementer should adher to. Employee interface lists the general behavior of an employee
    RealizationA class implements the behavior defined by the other other class or interface
    GeneralizationA class which is a special form of a parent class

    Generalization

    01public class SWEngineer extends Engineer
    02{
    03 public void work()
    04 {
    05  System.out.println('SW Engineer working');
    06 }
    07 public void off()
    08 {
    09  System.out.println('SW Engineer is off today');
    10 }
    11
    12 public void quit()
    13 {
    14  System.out.println('SW Engineer is quitting');
    15 }
    16
    17}

    Realisation

    1public abstract class Engineer implements Employee
    2{
    3 public void work();
    4 public void off();
    5 public void quit();
    6}

    Abstraction

    1public interface Employee{
    2 public void work();
    3 public void off();
    4 public void quit();
    5}

    Dependency

    1final class Company{
    2  void assign(Employee emp) {
    3    if (emp != null)
    4      emp.work();
    5  }
    6}

    Composition

    01final class Company{
    02private final Employee Employee;
    03{
    04    Company(EmpDetails details) {
    05    engine = new Employee(details);
    06  }
    07   void assign() {
    08      emp.work();
    09   }
    10}

    Aggregation

    01final class Company{
    02  private Employee engine;
    03  void addEmployee(Employee emp) {
    04    this.emp = emp;
    05  }
    06  void assign() {
    07    if (emp != null)
    08      emp.work();
    09  }
    10}
    Composition : Since Engine is part-of Car, relationship between them is Composition. Here is how they are implemented between Java classes.

    public class Car {
        //final will make sure engine is initialized
        private final Engine engine;  
           
        public Car(){
           engine  = new Engine();
        }
    }
    
    class Engine {
        private String type;
    }

    Aggregation : Since Organization has Person as employees, relationship between them is Aggregation. Here is how they look like in terms of Java classes

    public class Organization {
        private List employees;
    }
    
    
    public class Person {
        private String name;   
    }

    No comments:

    Post a Comment