Builder pattern with interface : Builder Pattern « Design Pattern « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Design Pattern » Builder Pattern 
34.6.2.Builder pattern with interface
import java.util.ArrayList;
import java.util.Iterator;

public class TestRobotBuilder {
  public static void main(String args[]) {
    Machine builder;
    RobotBuildable robot;
    String response = "a";

    builder = new Cooker();
    // builder = new AutomotiveRobotBuilder();

    // Start the construction process.

    builder.addStart();
    builder.addTest();
    builder.addAssemble();
    builder.addStop();

    robot = builder.getRobot();

    robot.go();
  }
}

interface Machine {
  public void addStart();

  public void addGetParts();

  public void addAssemble();

  public void addTest();

  public void addStop();

  public RobotBuildable getRobot();
}

interface RobotBuildable {
  public void go();
}

class Cooker implements Machine {
  CookerBuildable robot;
  ArrayList<Integer> actions;

  public Cooker() {
    robot = new CookerBuildable();
    actions = new ArrayList<Integer>();
  }

  public void addStart() {
    actions.add(new Integer(1));
  }

  public void addGetParts() {
    actions.add(new Integer(2));
  }

  public void addAssemble() {
    actions.add(new Integer(3));
  }

  public void addTest() {
    actions.add(new Integer(4));
  }

  public void addStop() {
    actions.add(new Integer(5));
  }

  public RobotBuildable getRobot() {
    robot.loadActions(actions);
    return robot;
  }

}

class Car implements Machine {
  CarBuildable robot;
  ArrayList<Integer> actions;

  public Car() {
    robot = new CarBuildable();
    actions = new ArrayList<Integer>();
  }

  public void addStart() {
    actions.add(new Integer(1));
  }

  public void addGetParts() {
    actions.add(new Integer(2));
  }

  public void addAssemble() {
    actions.add(new Integer(3));
  }

  public void addTest() {
    actions.add(new Integer(4));
  }

  public void addStop() {
    actions.add(new Integer(5));
  }

  public RobotBuildable getRobot() {
    robot.loadActions(actions);
    return robot;
  }

}

class CookerBuildable implements RobotBuildable {
  ArrayList<Integer> actions;

  public CookerBuildable() {
  }

  public final void go() {
    Iterator itr = actions.iterator();

    while (itr.hasNext()) {
      switch ((Integeritr.next()) {
      case 1:
        start();
        break;
      case 2:
        getParts();
        break;
      case 3:
        assemble();
        break;
      case 4:
        test();
        break;
      case 5:
        stop();
        break;
      }
    }
  }

  public void start() {
    System.out.println("Starting....");
  }

  public void getParts() {
    System.out.println("Getting flour and sugar....");
  }

  public void assemble() {
    System.out.println("Baking a cookie....");
  }

  public void test() {
    System.out.println("Crunching a cookie....");
  }

  public void stop() {
    System.out.println("Stopping....");
  }

  public void loadActions(ArrayList<Integer> a) {
    actions = a;
  }
}

class CarBuildable implements RobotBuildable {
  ArrayList<Integer> actions;

  public CarBuildable() {
  }

  public final void go() {
    Iterator itr = actions.iterator();

    while (itr.hasNext()) {
      switch ((Integeritr.next()) {
      case 1:
        start();
        break;
      case 2:
        getParts();
        break;
      case 3:
        assemble();
        break;
      case 4:
        test();
        break;
      case 5:
        stop();
        break;
      }
    }
  }

  public void start() {
    System.out.println("Starting....");
  }

  public void getParts() {
    System.out.println("Getting a carburetor....");
  }

  public void assemble() {
    System.out.println("Installing the carburetor....");
  }

  public void test() {
    System.out.println("Revving the engine....");
  }

  public void stop() {
    System.out.println("Stopping....");
  }

  public void loadActions(ArrayList<Integer> a) {
    actions = a;
  }
}
34.6.Builder Pattern
34.6.1.Example of the Builder pattern
34.6.2.Builder pattern with interface
34.6.3.Builder pattern: build a house
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.