mardi 12 février 2019

What are the pros and cons of having a 'manager' do something with an object or having object doing something itself?

I've been exploring the convenience of adding functionality to objects that previously would be handled by 'managers' or 'controllers'. I was just worried if there were any issues working this way. The main issue I can see is coupling.

As an example, lets use firing an employee from a business.

Previously I would work like this:

class Employee
{
    // Does employee stuff
}

class Business
{
    public List<Employee> employees
}

class BusinessManagement
{
    Business buisness
    public void FireEmployee(Employee employee)
    {
        business.employees.Remove(employee);
    }
}

Now I have been trying this:

class Employee
{
    Business business
    Fire()
    {
        business.employees.Remove(this);
    }
}

class Business
{
    public List<Employee> employees
}

The latter method is nice and convenient because you just need a reference to the employee and you can fire it wherever you like. You also don't need the management class.

Aucun commentaire:

Enregistrer un commentaire