n3rfd
n3rfd

Reputation: 275

dynamic design pattern

I have a class named 'Employee'. Within the class is a method named getAgentsByHierarchy and it returns an array of all the agents with their corresponding ID and some other information.

Array{              
  [0] => Array{
     [0] => code
     [1] => id  
     [2] => name    
     [3] => role
  }
}

I am extending this to two classes named 'Production' and 'Payroll'. These two classes have fetchAll() methods that calls Employee.getAgentsByHierarchy() and fetches all the agents and ONE extra data/field.

So when I call Production.fetchAll(), this is what it should return:

Array{              
 [0] => Array{
    [0] => code
    [1] => id   
    [2] => name 
    [3] => role
    [4] => production_figures_in_digits
 }
}

And when I call Payroll.fetchAll(), this is what it should return:

Array{              
 [0] => Array{
    [0] => code
    [1] => id   
    [2] => name 
    [3] => role
    [4] => payroll_figures_in_digits
 }
}

How do I go about this kind of design ?

My solution would be to include a parameter in the getAgentsByHierarchy() method that checks if the callee is from Production class or from Payroll class so that it would know what data to return. But then I think this way is not OOP way of doing things because if there's a new class that extends Salesforce, i would have to hardcode getAgentsByHierarchy() again to meet the requirements of the new class.

PS. I am new to design pattern, please forgive me with the way i wrote the title.

Upvotes: 0

Views: 380

Answers (2)

Saurabh
Saurabh

Reputation: 7964

In ideal object oriented written application the parent class should never be concerned about the behavior of it's Child classes. Its the child class which is inheriting has to abide by the contracts of parent class.

So the Employee class should not be concerned about the existence of Production, Payroll or any other class inheriting from Employee class.

Ideally getAgentsByHierarchy should be overridden in Production and Payroll (or any other future child class) to embed the information specific to them in the array.

Upvotes: 1

lostyzd
lostyzd

Reputation: 4523

If Production has the information, to lower coupling, do it like this

Production.fetchAll() {
    Employee.getAgentsByHierarchy();
    Production.getFiguresInDigits();
    // some method to combine them then return
}

And the same with Payroll

Upvotes: 0

Related Questions