Decorator design pattern in java - Java @ Desk

Friday, June 14, 2013

Decorator design pattern in java

As the name suggest, it decorates some basic behaviour of an object. It is used to extend the behaviour of certain object. In implementing the decorator pattern you construct a wrapper around an object by extending its behavior. The wrapper will do its job before or after and delegate the call to the wrapped instance.

Use decorator pattern, to add responsibility to objects without affecting other objects.

As an implementation, consider a scenario of employees working in an organization. The basic of all employees is that they are getting SALARY in their pay structure.
Additional, on the basis of post/position in the organization each employee gets different benefits. These benefits implementation is a decorator which will be wrapped around basic SALARY pay.

PayStructure.java(Component) - It is a blueprint.
package com.designpatterns.decorator;

public interface PayStructure {

    public String getPaymentStructure();
}
GeneralStaffEmployee.java(ConcreteComponent) - It is a basic class around which additional responsibilities will be attached dynamically.
package com.designpatterns.decorator;

public class GeneralStaffEmployee implements PayStructure {

    private String paymentStructure;
    
    public GeneralStaffEmployee(String paymentStructure) {
        this.paymentStructure = paymentStructure;
    }
    
    @Override
    public String getPaymentStructure() {
        return paymentStructure;
    }

}
EmployeeBenefitsDecorator.java(Decorator)
package com.designpatterns.decorator;

public abstract class EmployeeBenefitsDecorator implements PayStructure {
    PayStructure originalPayStructure;
}
ITEmployee.java(Concrete Decorator) - It added benefits for IT employees
package com.designpatterns.decorator;

public class ITEmployee extends EmployeeBenefitsDecorator {

    private String paymentStructure;

    public ITEmployee(PayStructure payStructure) {
        originalPayStructure = payStructure;
    }

    @Override
    public String getPaymentStructure() {
        paymentStructure = addBenefits(originalPayStructure.getPaymentStructure());
        return paymentStructure;
    }
    
    private String addBenefits(String originalPayment) {
        StringBuffer payStructure = new StringBuffer(originalPayment);
        payStructure.append("\nPay variable amount");
        payStructure.append("\nDeduct PF from payment");
        payStructure.append("\nDeduct tax");
        return payStructure.toString();
    }
}
HREmployee.java(Concrete Decorator) - It added benefits for HR employees
package com.designpatterns.decorator;

public class HREmployee extends EmployeeBenefitsDecorator {

    private String paymentStructure;

    public HREmployee(PayStructure payStructure) {
        originalPayStructure = payStructure;
    }

    @Override
    public String getPaymentStructure() {
        paymentStructure = addBenefits(originalPayStructure.getPaymentStructure());
        return paymentStructure;
    }
    
    private String addBenefits(String originalPayment) {
        StringBuffer payStructure = new StringBuffer(originalPayment);
        payStructure.append("\nPay phone bills");
        payStructure.append("\nDeduct PF from payment");
        payStructure.append("\nDeduct tax");
        payStructure.append("\nIncentives for successful resource hiring");
        return payStructure.toString();
    }
}
AdminEmployee.java(Concrete Decorator) - It added benefits for Admin employees
package com.designpatterns.decorator;

public class AdminEmployee extends EmployeeBenefitsDecorator {

    private String paymentStructure;

    public AdminEmployee(PayStructure payStructure) {
        originalPayStructure = payStructure;
    }

    @Override
    public String getPaymentStructure() {
        paymentStructure = addBenefits(originalPayStructure.getPaymentStructure());
        return paymentStructure;
    }
    
    private String addBenefits(String originalPayment) {
        StringBuffer payStructure = new StringBuffer(originalPayment);
        payStructure.append("\nPay phone bills");
        payStructure.append("\nDeduct PF from payment");
        payStructure.append("\nDeduct tax");
        payStructure.append("\nCost cutting benefits");
        return payStructure.toString();
    }
}
DirectorEmployee.java(Concrete Decorator) - It added benefits for Director employees
package com.designpatterns.decorator;

public class DirectorEmployee extends EmployeeBenefitsDecorator {

    private String paymentStructure;

    public DirectorEmployee(PayStructure payStructure) {
        originalPayStructure = payStructure;
    }

    @Override
    public String getPaymentStructure() {
        paymentStructure = addBenefits(originalPayStructure.getPaymentStructure());
        return paymentStructure;
    }
    
    private String addBenefits(String originalPayment) {
        StringBuffer payStructure = new StringBuffer(originalPayment);
        payStructure.append("\nPay phone bills");
        payStructure.append("\nDeduct PF from payment");
        payStructure.append("\nDeduct tax");
        payStructure.append("\nProvide shares of companies");
        payStructure.append("\nReimbursement petrol bills");
        payStructure.append("\nProvide pick and drop facility");
        return payStructure.toString();
    }
}
PayStructureMain.java
package com.designpatterns.decorator;

public class PayStructureMain {

    public static void main(String args[]) {
        PayStructure payStructure = new GeneralStaffEmployee("Pay Salary");
        System.out.println("\nPay structure for general staff employee : \n" + payStructure.getPaymentStructure());
        
        
        PayStructure itPayStructure = new ITEmployee(payStructure);
        System.out.println("\n\nPay structure for IT employee : \n" +itPayStructure.getPaymentStructure());
        
        PayStructure hrPayStructure = new HREmployee(payStructure);
        System.out.println("\n\nPay structure for HR employee : \n" +hrPayStructure.getPaymentStructure());
        
        PayStructure adminPayStructure = new AdminEmployee(payStructure);
        System.out.println("\n\nPay structure for Admin employee : \n" +adminPayStructure.getPaymentStructure());
        
        PayStructure directorPayStructure = new DirectorEmployee(payStructure);
        System.out.println("\n\nPay structure for Director : \n" +directorPayStructure.getPaymentStructure());
    }
}






No comments:

Post a Comment