Builder design pattern in java | Java @ Desk
Home » , » Builder design pattern in java

Builder design pattern in java

This design pattern breaks the complex object creation into simpler processes. The construction process remains the same to create different representations. Director controls the construction of the object and only the director knows what type of object to create.

This pattern helps the object creation in a step by step manner. It breaks the module into smaller pieces and the smaller pieces in integration gives the complex object.

Consider an example to create a web application to create login page, help page & error page.

For this, create a interface as shown below:

WebAppBuilder.java - This interface contains the smaller process that in together will help in creation of a particular JSP page(complex object creation)

package com.designpatterns.builder;

public interface WebAppBuilder {

    public void createJSP();

    public boolean isDatabaseConnectionReqd();

    public boolean isStaticPage();

    public void createController();

    public boolean isErrorPage();
    
}

WebAppFields.java - This class contains the fields that are required to set the values

package com.designpatterns.builder;

public class WebAppFields {

    public boolean staticPage;

    public boolean databaseConnectionReqd;

    public String userName;

    public String password;

    public String controllerCode;

    public String helpContent;

    public boolean errorPage;

    public boolean isErrorPage() {
        return errorPage;
    }

    public void setErrorPage(boolean errorPage) {
        this.errorPage = errorPage;
    }

    public String getHelpContent() {
        return helpContent;
    }

    public void setHelpContent(String helpContent) {
        this.helpContent = helpContent;
    }

    public boolean isStaticPage() {
        return staticPage;
    }

    public void setStaticPage(boolean staticPage) {
        this.staticPage = staticPage;
    }

    public boolean isDatabaseConnectionReqd() {
        return databaseConnectionReqd;
    }

    public void setDatabaseConnectionReqd(boolean databaseConnectionReqd) {
        this.databaseConnectionReqd = databaseConnectionReqd;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getControllerCode() {
        return controllerCode;
    }

    public void setControllerCode(String controllerCode) {
        this.controllerCode = controllerCode;
    }
}

LoginPageBuilder.java - This class will display steps to create Login page

package com.designpatterns.builder;

public class LoginPageBuilder implements WebAppBuilder {

    @Override
    public void createJSP() {
        // Create login.jsp
        // Create two textboxes and one button
        // First text box for userName field
        // Second text box for password field
        // Create Login Button
        System.out.println("Create JSP");
        System.out.println("Create two text boxes for userName and passWord");
        System.out.println("Create button");
        System.out.println("Validate on click of button that both the fields are non-empty");
        System.out.println("Call a controller on click of button");
    }

    @Override
    public boolean isDatabaseConnectionReqd() {
        return true;
    }

    @Override
    public boolean isStaticPage() {
        return false;
    }

    @Override
    public void createController() {
        // Method call on click of Login button
        // Take userName from WebAppFields bean
        // Take password from WebAppFields bean
        if (isDatabaseConnectionReqd()) {
            // Create connection to database
            System.out.println("Make a connection to database");
        }
        // Validate the user
        System.out.println("Call database and validate the userName and password");
        System.out.println("Navigate to home page for valid user");
        System.out.println("Navigate to login page and display error message");
    }

    @Override
    public boolean isErrorPage() {
        return false;
    }

}

HelpPageBuilder.java - This class will display steps to create Help page

package com.designpatterns.builder;

public class HelpPageBuilder implements WebAppBuilder {

    @Override
    public void createJSP() {
        // Create help.jsp
        // Create helpContent and put it in label
        System.out.println("Create JSP");
        System.out.println("Create Help content and put it in label");
    }

    @Override
    public boolean isDatabaseConnectionReqd() {
        return false;
    }

    @Override
    public boolean isStaticPage() {
        return true;
    }

    @Override
    public void createController() {
        System.out.println("No controller required since its a static help page");
    }

    @Override
    public boolean isErrorPage() {
        // TODO Auto-generated method stub
        return false;
    }

}

ErrorPageBuilder.java - - This class will display steps to create Error page

package com.designpatterns.builder;

public class ErrorPageBuilder implements WebAppBuilder {

    @Override
    public void createJSP() {
        // Create error.jsp
        if (isErrorPage()) {
            System.out.println("Create JSP");
            System.out.println("Set errorPage=\"true\" in JSP to make this JSP an error.jsp");
            System.out
                    .println("Make an entry in web.xml to navigate to this JSP when some exception occur in application");
        }
    }

    @Override
    public boolean isDatabaseConnectionReqd() {
        return true;
    }

    @Override
    public boolean isStaticPage() {
        return false;
    }

    @Override
    public void createController() {
    }

    @Override
    public boolean isErrorPage() {
        return true;
    }

}

AppBuilder.java - - This class will help in displaying the results

package com.designpatterns.builder;

public class AppBuilder {

    private WebAppBuilder webAppBuilder;

    public AppBuilder(WebAppBuilder webAppBuilder) {
        this.webAppBuilder = webAppBuilder;
    }
    public void displaySteps() {
        this.webAppBuilder.createJSP();
        this.webAppBuilder.createController();
    }
}
BuilderMain.java

package com.designpatterns.builder;

public class BuilderMain {

    public static void main(String args[]) {
        System.out.println("Steps to create login page");

        WebAppBuilder loginPageBuilder = new LoginPageBuilder();
        AppBuilder appLoginBuilder = new AppBuilder(loginPageBuilder);
        appLoginBuilder.displaySteps();

        System.out.println("\n\nSteps to create Help page");

        WebAppBuilder helpPageBuilder = new HelpPageBuilder();
        AppBuilder appHelpBuilder = new AppBuilder(helpPageBuilder);
        appHelpBuilder.displaySteps();

        System.out.println("\n\nSteps to create error page");

        WebAppBuilder errorPageBuilder = new ErrorPageBuilder();
        AppBuilder appErrorBuilder = new AppBuilder(errorPageBuilder);
        appErrorBuilder.displaySteps();

    }
}





0 comments:

Post a Comment