Search code examples
classinheritanceumlassociationsclass-diagram

How I can think about implement UML diagram with association between class and interface


I'm try to think how I can understand this UML diagram and check how I can represent it to code:

enter image description here

What is make me confuse, we have an association relation between Company and Employee interface, and Designer classe is implement the Employee interface, but how we can connect Company with Employee interface?

What I understand (PHP for Example):

class Company {
 ...
}
class GameDevCompany extends Company {
 ...
}
class Designer extends GameDevCompany implements Employee {
 ...
}

If I think about this diagram like this code, its a truth way or I missing something or make something wrong?

Thanks


Solution

  • We have three concepts in this diagram:

    • Specialization: a Company has two specializations which are GameDev Company and Outsourcing Company
    • Realization: an interface Employee can be realized by any class that implements this interface, such as Designer (or Programmer, Artist, Tester, ...)
    • Association: a Company is associated with Employee. The diagram does not say anything about multiplicity. But since this is a very simple model, and based on our knowledge, we could guess that a Company may be associated with no, one, or several Employee.

    In PHP terminology, specialization is inheritance, which is expressed with the extend keyword:

    class Company {
     ...
    }
    class GameDevCompany extends Company {
     ...
    }
    

    In PHP terminology interfaces are implemented rather than realized and this is expressed with keyword implements

    interface Employee {
     ...
    }
    class Designer implements Employee {
     ...
    }  
    

    The association from one Company with potentially several Employee requires that Company has some kind of iterable container such as an array that holds references to the Employee objects. My PHP is very rusty, but I understand that objects in an array are always references which is exactly what is needed for ordinary associations.