Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a difficult time trying to figure out how to model a certain scenario as a UML design class diagram.

Suppose I have the following situation: I have an employee called X who is the CEO of the company. A, B and C reports to X and U,V reports to A.

According to me, there should be an Interface called IEmployee which should have employee's name, designation and empNo. Employee class should implement this IEmployee. Manager class should implement Employee.


What I have tried:

interface IEmployee
{
}
class Employee: IEmployee
{
}
class Manager: Employee
{
}
Posted
Updated 22-Aug-19 23:19pm
Comments
F-ES Sitecore 22-Aug-19 7:42am    
They are both indeed employees so what you have so far makes sense. The next thing you need to think about is how you manage the reporting structure. If it is the case that only managers have people reporting to them you'll need your manager class to have a list of IEmployee objects that represent who reports to them, so the next step is to think about how you can achieve that.
CHill60 22-Aug-19 7:52am    
Alternatively every Employee object has a attribute for ManagedBy. For the CEO this would be null. The Manager class could have a method for determining their direct reports. That way you ensure that an Employee can only ever appear in one manager's list e.g. when they move department you only have to update one object (employee) instead of two (old manager, new manager)
F-ES Sitecore 22-Aug-19 9:02am    
Or you could have a many-to-many implementation between employee and manager. However given this is obviously homework I think it's more likely the desired solution is going to be the one that requires more thought in the UML. Your solution is simply a single "ManagedBy" property and everything else is done in code....this is a UML exercise so they'll want to see some UMLing.
CHill60 22-Aug-19 11:29am    
LOL @ UMLing. There's a new word right there! :-)
Member 11732803 22-Aug-19 12:05pm    
So you think I should add a property in Employee class called "ManagedBy" which has to be of Manager Type and it would be null for the CEO.? @CHill60

Assume everybody in the company is an Employee:
public interface IEmployeeInfo
{
    string Name {set; get;}
    string Address {set; get;}
    decimal Salary { set; get; }
}

public interface IEmployee : IEmployeeInfo
{
    string Title{set; get;}
    IManager EManager { set; get; }
}
    
public interface IManager : IEmployee
{
    List<IEmployee> EmployeesManaged {set; }

    void AddManagedEmployee(IEmployee employee);
    void RemoveManagedEmployee(IEmployee employee);
    bool IsManagedBy(IEmployee employee);
}
Then 'Employee would inherit from IEmployee, and 'Manager would inherit from IManager.

Note the 'set only property 'EmployeesManaged in 'IManager: assuming you want to encapsulate the usage of that List, you can use an explicit interface implementation in the Manager class:
private List<IEmployee> EmployeesManaged { get; set; }

// implicit interface impl.
List<IEmployee> IManager.EmployeesManaged
{
    set => this.EmployeesManaged = value;
}
Then, only internal methods of 'Manager can access the List:
public void AddManagedEmployee(IEmployee employee)
{
    if (IsManagedBy(employee))
    {
        // throw error ?
    }
    else
    {
        EmployeesManaged.Add(employee);
        employee.EManager = this;
    }
}
 
Share this answer
 
Here is an employee example: Composite design pattern in java - Java2Blog[^]
As Java is not that much different from C#, I think you can figure things out from that.

In case you are looking for an UML diagramming tool: best-uml-applications-out-there[^]
 
Share this answer
 
Comments
Member 11732803 22-Aug-19 13:25pm    
The link gave me an insight. I was looking for something like this.Thanks!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900