Java Tutorial/Design Pattern/Builder Pattern
Версия от 17:44, 31 мая 2010; (обсуждение)
Builder pattern: build a house
import java.util.LinkedList;
import java.util.List;
public class BuilderDemo {
public static void main(String[] args) {
HouseBuilder builder = new SimpleHouseBuilder();
HouseBuildingCompany houseBuildingCompany = new HouseBuildingCompany();
House house = houseBuildingCompany.build(builder);
System.out.println(house);
}
}
interface HouseBuilder {
void buildBasement();
void buildWalls();
void buildRoof();
House getHouse();
}
class HouseBuildingCompany {
public House build(HouseBuilder builder) {
builder.buildBasement();
builder.buildWalls();
builder.buildRoof();
return builder.getHouse();
}
}
class SimpleHouseBuilder implements HouseBuilder {
private House house;
public SimpleHouseBuilder() {
this.house = new House();
}
public void buildBasement() {
this.house.setBasement(new Basement());
}
public void buildWalls() {
for (int i = 0; i < 4; i++)
this.house.addWall(new Wall());
}
public void buildRoof() {
this.house.setRoot(new Roof());
}
public House getHouse() {
return this.house;
}
}
class Basement {
@Override
public String toString() {
return "Basement";
}
}
class Wall {
@Override
public String toString() {
return "Wall";
}
}
class Roof {
@Override
public String toString() {
return "Roof";
}
}
class House {
private Basement basement;
private List<Wall> walls;
private Roof roof;
public House() {
this.walls = new LinkedList<Wall>();
}
public void addWall(Wall wall) {
this.walls.add(wall);
}
public void setRoot(Roof roof) {
this.roof = roof;
}
public void setBasement(Basement basement) {
this.basement = basement;
}
@Override
public String toString() {
return "House {" + this.basement + ", " + this.walls + ", " + this.roof + "}";
}
}
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 ((Integer) itr.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 ((Integer) itr.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;
}
}
Example of the Builder pattern
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
class Office extends ArrayList<OfficeUnit> {
}
class Word extends Office {
}
class PPT extends Office {
}
class Excel extends Office {
}
class OfficeUnit {
private String s;
public OfficeUnit(String s) {
this.s = s;
}
public String toString() {
return s;
}
}
class DocumentPage extends OfficeUnit {
public DocumentPage(String s) {
super(s);
}
}
class PPTSlide extends OfficeUnit {
public PPTSlide(String s) {
super(s);
}
}
class ExcelWorkbook extends OfficeUnit {
public ExcelWorkbook(String s) {
super(s);
}
}
class OfficeBuilder {
public void buildBase() {
}
public void addOfficeUnit(OfficeUnit item) {
}
public Office getFinishedMedia() {
return null;
}
}
class WordBuilder extends OfficeBuilder {
private Word b;
public void buildBase() {
System.out.println("Building word framework");
b = new Word();
}
public void addOfficeUnit(OfficeUnit chapter) {
System.out.println("Adding word page " + chapter);
b.add(chapter);
}
public Office getFinishedMedia() {
return b;
}
}
class PPTBuilder extends OfficeBuilder {
private PPT m;
public void buildBase() {
System.out.println("Building PPT");
m = new PPT();
}
public void addOfficeUnit(OfficeUnit article) {
System.out.println("Adding slide" + article);
m.add(article);
}
public Office getFinishedMedia() {
return m;
}
}
class ExcelBuilder extends OfficeBuilder {
private Excel w;
public void buildBase() {
System.out.println("Building Excel");
w = new Excel();
}
public void addOfficeUnit(OfficeUnit webItem) {
System.out.println("Adding workbook" + webItem);
w.add(webItem);
}
public Office getFinishedMedia() {
return w;
}
}
class OfficeDirector {
private OfficeBuilder mb;
public OfficeDirector(OfficeBuilder mb) {
this.mb = mb;
}
public Office produceMedia(List input) {
mb.buildBase();
for (Iterator it = input.iterator(); it.hasNext();)
mb.addOfficeUnit((OfficeUnit) it.next());
return mb.getFinishedMedia();
}
};
public class BuildOfficeDemo {
public static void main(String[] args) {
List input = Arrays.asList(new OfficeUnit[] { new OfficeUnit("item1"), new OfficeUnit("item2"),
new OfficeUnit("item3"), new OfficeUnit("item4"), });
OfficeDirector buildBook = new OfficeDirector(new WordBuilder());
Office word = buildBook.produceMedia(input);
String result = "word: " + word;
System.out.println(result);
OfficeDirector buildMagazine = new OfficeDirector(new PPTBuilder());
Office ppt = buildMagazine.produceMedia(input);
result = "PPT: " + ppt;
System.out.println(result);
OfficeDirector buildWebSite = new OfficeDirector(new ExcelBuilder());
Office excel = buildWebSite.produceMedia(input);
result = "Excel: " + excel;
System.out.println(result);
}
}