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;
            }
    }

}

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in J2SE. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s