How to make class diagrams using Composition
A company has a number of employees, company also has several projects that it needs to work on. Each employee may be assigned to one or more projects, or may not be assigned to a project at all. A project must have at least one employee assigned, and may have any number of employees assigned. An employee's billing rate may vary by project and the company wishes to record the applicable billing rate for each employee when assigned to a particular project. At the end of each month, company mails a cheque to each employee who has worked on a project during that month. The cheque amount is based on billing rate and hours logged for each project assigned to the employee.
Before we move on to making class diagram for this particular problem, let's discuss what is composition relation and how we use it. For example we have ClassA and ClassB these classes will have composition relationship if ClassA object is composed of one or more ClassB objects and further in composition relationship if a ClassA object dies then all ClassB objects that this particular ClassA object has will die automatically. For instance a building class can have many room class objects but when the building object dies all the room objects it contains must die too. Composition has a solid black diamond symbol.
To make a class diagram we first look for particular nouns, in our problem statement we have company, employee and project. We can say that company has employees and company has projects further if a company project dies then all associated employee objects will die as well, so here we can use composition relation between company and employee and also between company and projects. But, there is something more in our class diagram ie. we have an association between Employee and Project classes and that is not an ordinary association rather this association has a function that checks the billing rate of each employee assigned to the job and also sends the cheque at the end of the month. So, we will be using association classes to show that function but if you don't know how association classes work I encourage you to read the tutorial I wrote on Association Classes (Part 1 and Part 2).
Please feel free to use the comments form below if you have any questions or need more explanation on anything. Use the icons below to share this tutorial with your friends.