Sunday, May 11, 2014

OOPs , Part 9 : A use case to understand advantage of interface and composition over inheritance.

In my last post understanding interface best way  i have put a scenario and solution code to understand the use of two mantras of design pattern.

Here in this post I will be improving my code and Adopt the best practices to use the Interface and composition while coding.

Point to focus :
  1. From the given scenario we can visualize , there is common functionality of calculating the salary which is provided my every category class. Just every category class provides its logic in its own way.
  2. So what we can do, the common services offered by different service provider classes(different Category class) can be abstracted out and declared as a separate interface. 
  3. Each of the service provider classes can be designed as implementers of this common interface. 
Advantage :
  1. Objects of different service provider classes can be treated as objects of the interface type.
  2. This will enables the client to use different types of service provider objects in a loosely manner without requiring any changes. 
  3. The client does not need to be altered even when a new service provider is designed as part of the class hierarchy.
Example with code :

here our first type is an interface :SalaryCalculator.java
package in.technoscience.best;
public interface SalaryCalculator {
  public double getSalary();
}

second class is : CategoryA . java implementing SalaryCalculator interface

package in.technoscience.best;

public class CategoryA implements SalaryCalculator {
 double baseSalary;
 double overTime;

 public CategoryA(double base, double overTime) {
  baseSalary = base;
  overTime = this.overTime;
 }

 public double getSalary() {
  return (baseSalary + overTime);
 }
}

third class will be : CategoryB. java implementing the SalaryCalculator interface

package in.technoscience.best;

public class CategoryB implements SalaryCalculator {
 double salesAmount;
 double baseSalary;
 final static double commission = 0.02;

 public CategoryB(double sa, double base) {
  baseSalary = base;
  salesAmount = sa;
 }

 public double getSalary() {
  return (baseSalary + (commission * salesAmount));
 }
}

fourth class will be : Employee class

package in.technoscience.best;
public class Employee {
  SalaryCalculator empType; // interface type 
  String name;

  public Employee(String s, SalaryCalculator c) {
    name = s;
    empType = c;
  }

  public void display() {
    System.out.println("Name=" + name);
    System.out.println("salary= " + empType.getSalary());
  }
}

Now a class to test the application : MainApp.java

package in.technoscience.best;

public class MainApp {
  public static void main(String [] args) {
    SalaryCalculator cA = new CategoryA(10000, 200); // runtime polymorphism
    Employee emp = new Employee ("technoscience",cA);
    emp.display();

    cA = new CategoryB(20000, 800);
    emp = new Employee ("technoscience2",cA);
    emp.display();
  }
}

Explanation: 
The changes I have made in this code :
  1. The common salary calculating service provided by different objects has been abstracted out to a separate SalaryCalculator interface.
  2. Each of the CategoryA and the CategoryB classes has been designed as implementers of the SalaryCalculator interface.
  3. The Employee class implementation needs to be changed to accept a salary calculator service provider of type SalaryCalculator.
Advantage :
  1. With these changes, the main application object MainApp can now create objects of different types of salary calculator classes and use them to configure different Employee objects. 
  2. Because the Employee class, accepts objects of the SalaryCalculator type, it can be used with an instance of any SalaryCalculator implementer class (or its subclass).
  3. In the future if we want to add any new category then we can write a new class implementing that interface and then in MainApp. java we can deal accordingly and we will be getting proper result.
you can download the source code from my git repository : My Git

No comments:

Post a Comment