Friday, August 12, 2011

JAVA CODE FOR ABSTRACT FACTORY PATTERN


/*

 Java Design Pattern Name :-)  ABSTRACT FACTORY PATTERN

 Definition :-) ABSTRACT FACTORY PATTERN is a bit higher than FACTORY PATTERN . A 
                     FACTORY PATTERN return any subclass from the factory of classes while
                     ABSTRACT FACTORY  PATTERN returns factory of classes and that factory later returns
                     any subclass.
 
 */

/**
 *
 * @author RK
 */

 abstract class Department{

    public abstract Details getDepartmentName();
    public abstract Details getHeadName();
    public abstract Details getTotalEmployees();
   
}

class Details{

    public String information;

    Details(String information) {
        this.information = information;
    }

    public String getInformation() {
        return information;
    }
 
}

class Account extends Department {

    @Override
    public Details getDepartmentName() {
        return new Details(" ACCOUNT DEPARTMENT ");
    }

    @Override
    public Details getHeadName() {
        return new Details(" Mr.RAJ KIRPAL SINH J.  ");
    }

    @Override
    public Details getTotalEmployees() {
        return new Details(" 11 ");
    }

}

class Purchase extends Department{

    @Override
    public Details getDepartmentName() {
        return new Details(" PURCHASE DEPARTMENT ");
    }

    @Override
    public Details getHeadName() {
        return new Details(" Mr. KUNAL MEHTA ");
    }

    @Override
    public Details getTotalEmployees() {
        return new Details(" 20 ");
    }

}

class Sales extends  Department {

    @Override
    public Details getDepartmentName() {
        return new Details(" SALES DEPARTMENT ");
    }

    @Override
    public Details getHeadName() {
        return new Details(" Mr.ACOUNT  ");
    }

    @Override
    public Details getTotalEmployees() {
        return new Details(" 25 ");
    }

}

public class AbstractDesignPattern {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] RK) {
        // TODO code application logic here

        AbstractDesignPattern start=new AbstractDesignPattern();
        Department departmentOBJ=start.getDepartment("P");
        System.out.println("Department Name : "+departmentOBJ.getDepartmentName().getInformation());
        System.out.println("Head of Department  : "+departmentOBJ.getHeadName().getInformation());
        System.out.println("Total employees in department : "+departmentOBJ.getTotalEmployees().getInformation());

    }

    public Department getDepartment(String charCode){
            if(charCode.equals("A")){
                return  new Account();
            }else if(charCode.equals("S")){
                return new Sales();
            }else if(charCode.equals("P")){
                return new Purchase();
            }else{
                return null;
            }
    }

}

No comments:

Post a Comment