JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Builder Design Pattern
  • Use the Builder design pattern when you want client code to have control over the construction process but want to be able to end up with different kinds of
    objects (each of which is built by a different type of
    builder).
  • For example, this is the pattern you want when you’re building robots using the same construction process but want to be able to end up with different kinds of
    robots — all the client code has to do is to load different builders; the construction process stays the same
Purpose
  • Separate the construction of a complex object from its representation so that the same
    construction process can create different representations

Applications

  • The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
  • The construction process must allow different representations for the object that's constructed
Examples of Builder Pattern

  • The Builder pattern separates the construction of a complex object from its
    representation so that the same construction process can create different
    representations.
  •  This pattern is used by fast food restaurants to construct children's meals.
  •  Children's meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy car).

Example:-

Builder.java
package simple;

public class Builder {
public static void main(String[] arg) {
TextArea t = new TextArea.Builder("myTextAreaName",
 
"Here comes text").cols(50).rows(9).align("center").make();
System.out.println(t);
}
}

BuilderPattern.java
package simple;

public class BuilderPattern {
public static void main(String[] arg) {
TextArea t = new TextArea.Builder("myTextAreaName",
 
"Here comes text").cols(50).rows(9).align("center").make();
System.out.println(t);
}
}

TextArea.java
package simple;
class TextArea {
private final int cols;
private final int rows;
private final String align;
private final String name;
private final String text;
  public static class Builder {
    //  Required field
    private final String text;
    private final String name;
//Also make default parameters
    private int cols = 20;
    private int rows = 5;
    private String align = "left";
    public Builder(String name, String text) {
      this.text = text;
      this.name = name;
    }
    public Builder cols(int value) {
      cols = value;
      return this//this key point
in builder pattern it returns this

    }
    public Builder rows(int value) {
      rows = value;
      return this;
    }
    public Builder align(String value) {
      align = value;
      return this;
    }
    public TextArea make() {
      return new TextArea(this);
    }
  }

  
  private TextArea(Builder build) {
    cols = build.cols;
    rows = build.rows;
    align = build.align;
    name = build.name;
    text = build.text;
  }
public String toString() {
StringBuilder sb = new StringBuilder("<textarea");
sb.append(" name=").append(name);
sb.append(" cols=").append(cols);
sb.append(" rows=").append(rows);
sb.append(" align=").append(align);
sb.append(">").append(text).append("</textarea>");
return sb.toString();
}
}

Output:-


Download Zip code

Example:

House.java
package HouseBullding;
import java.util.*;
class House {
    private String type = null;
    private List features = new ArrayList();
    public House() {
    }
    public House(String type) {
        this.type = type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getType() {
        return type;
    }
    public void setProgress(String s) {
        features.add(s);
    }
    public String toString() {
        StringBuffer ff = new StringBuffer();
        String t = type.substring(6);
        ff.append(t + "\n ");
        for (int i = 0; i < features.size(); i ++) {
             ff.append(features.get(i"\n ");
        }
        return ff.toString();
    }
}

HouseBullding.java
package HouseBullding;
abstract class HouseBuilder {
    protected House house = new House();
    
    protected String showProgress() {
        return house.toString();
    }

    abstract public void buildFoundation();
    abstract public void buildFrame();
    abstract public void buildExterior();
    abstract public void buildInterior();
}

OneStoryHouse.java
package HouseBullding;

class OneStoryHouse extends HouseBuilder {
    
    public OneStoryHouse(String features) {
        house.setType(this.getClass() " " + features);
    }
    public void buildFoundation() {
        //doEngineering()
        //doExcavating()
        //doPlumbingHeatingElectricity()
        //doSewerWaterHookUp()
        //doFoundationInspection()
        house.setProgress("foundation is done");
    }

    public void buildFrame() {
        //doHeatingPlumbingRoof()
        //doElectricityRoute()
        //doDoorsWindows()
        //doFrameInspection()
        house.setProgress("frame is done");
    }

    public void buildExterior() {
        //doOverheadDoors()
        //doBrickWorks()
        //doSidingsoffitsGutters()
        //doDrivewayGarageFloor()
        //doDeckRail()
        //doLandScaping()
        house.setProgress("Exterior is done");
    }

    public void buildInterior() {
        //doAlarmPrewiring()
        //doBuiltinVacuum()
        //doInsulation()
        //doDryWall()
        //doPainting()
        //doLinoleum()
        //doCabinet()
        //doTileWork()
        //doLightFixtureBlinds()
        //doCleaning()
        //doInteriorInspection()
        house.setProgress("Interior is under going");
    
}

TwoStoryHouse.java
package HouseBullding;

class TwoStoryHouse extends HouseBuilder {
  
    public TwoStoryHouse(String features) {
        house.setType(this.getClass() " " + features);
    }
    public void buildFoundation() {
        //doEngineering()
        //doExcavating()
        //doPlumbingHeatingElectricity()
        //doSewerWaterHookUp()
        //doFoundationInspection()
        house.setProgress("foundation is done");
    }

    public void buildFrame() {
        //doHeatingPlumbingRoof()
        //doElectricityRoute()
        //doDoorsWindows()
        //doFrameInspection()
        house.setProgress("frame is under construction");
    }

    public void buildExterior() {
        //doOverheadDoors()
        //doBrickWorks()
        //doSidingsoffitsGutters()
        //doDrivewayGarageFloor()
        //doDeckRail()
        //doLandScaping()
        house.setProgress("Exterior is waiting to start");
    }

    public void buildInterior() {
        //doAlarmPrewiring()
        //doBuiltinVacuum()
        //doInsulation()
        //doDryWall()
        //doPainting()
        //doLinoleum()
        //doCabinet()
        //doTileWork()
        //doLightFixtureBlinds()
        //doCleaning()
        //doInteriorInspection()
        house.setProgress("Interior is not started yet");
    }
}

WorkShop.java
package HouseBullding;


class WorkShop {
    //force the order of building process
    public void construct(HouseBuilder hb) {
        hb.buildFoundation();
        hb.buildFrame();
        hb.buildExterior();
        hb.buildInterior();
    }
}

TestBulider.java
package HouseBullding;
public class TestBulider  {
    
    public static void main(String[] args) {
       try
       {
       HouseBuilder one = new OneStoryHouse("2 bedrooms,
 2.5 baths, 2-car garage, 1500 sqft"
);
       HouseBuilder two = new TwoStoryHouse("4 bedrooms,
4 baths, 3-car garage, 5000 sqft"
);

       WorkShop shop = new WorkShop();
       shop.construct(one);
       shop.construct(two);
          
   
       System.out.println("Check house building
progress: \n"
);
       System.out.println(one.showProgress());
       System.out.println(two.showProgress());
       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
   }
}

Output:-


Download Zip code