Call me crazy, but you may not need a subclass to deal with various payment methods here. Instead, consider using the Strategy to determine salary / commission sales. So you have the Employee
class and its subclass SalesEmployee
; each of them will have the property of a PayStrategy
object, which can have specific subclasses of WagePayStrategy
and CommissionPayStrategy
. If you define the correct methods on PayStrategy
(it can be an interface or an abstract class), you can have Employee
instances that define the strategy objects that the employee pays for certain resources (hours, sales, etc.) ..
Consider the following trivial example:
interface PayStrategy { public float getPay(float hoursWorked, float salesMade); } class WageStrategy implements PayStrategy { public float getPay(float hoursWorked, float salesMade) { return hoursWorked * 8.5; } } class CommissionStrategy implements PayStrategy { public float getPay(float hoursWorked, float salesMade) { return salesMade * 0.5 + hoursWorked * 5.0; } } abstract class Employee { public PayStrategy payStrategy;
Please note how you can change the payment strategy for any given employee without requiring another class, giving you more flexibility and encapsulation around this algorithm.
source share