BUILDER(建造)模式
类别: JSP教程
目的:把一个复杂对象的构造过程和它的创建过程分离
描述:就是把一个复杂对象的过程和部件分开,比如说创建一个汽车
的过程,一个类Builder描述了创建各种部件的过程,另外一个类Concrete描述了组装各种部件的过程
那么我们先生成Builder对象,我们或者在创建过程中创建各种部件,或者在Concrete的组装部件的之
前创建各种部件,然后组装。在Concrete中有个concrete方法,它描述组装或者创建并组装的过程,
我们甚至根据参数的不同动态的构造部件。
/*******************************************************/
package builder;
public interface BuilderInterface {
public void buildPartA();
public void buildPartB();
public void buildPartC();
public Product getResult();
}
/******************************************************************/
package builder;
public class BuilderRealize implements BuilderInterface {
private Product result = ProductFactory.getInstance();
public void buildPartA() {
result.addPart("Part A");
}
public void buildPartB() {
result.addPart("Part B");
}
public void buildPartC() {
result.addPart("Part C");
}
public Product getResult() {
return result;
}
}
/***************************************************************************/
package builder;
public class Direct {
private BuilderInterface builder = null;
public Direct(BuilderInterface builder) {
this.builder = builder;
}
public void construct(int j) {
if (j == 1) {
builder.buildPartA();
}
else if (j == 2) {
builder.buildPartA();
builder.buildPartB();
}
else {
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
}
}
}
/***********************************************************************************/
package builder;
import java.util.*;
public class Main {
public static void main(java.lang.String[] args) {
//在此处插入用来启动应用程序的代码。
BuilderRealize builder = new BuilderRealize();
Direct direct = new Direct(builder);
direct.construct(2);
Product product = builder.getResult();
Collection col = product.getResult();
Iterator ite = col.iterator();
while (ite.hasNext()) {
System.out.println(ite.next());
}
}
}
/***********************************************************************/
package builder;
import java.util.*;
public interface Product {
public void addPart(String part);
public Collection getResult();
}
/***********************************************************************/
package builder;
public class ProductFactory extends ProductRealize {
public static Product getInstance() {
return new ProductRealize();
}
}
/***********************************************************************/
package builder;
import java.util.*;
public class ProductRealize implements Product {
private ArrayList carResult = new ArrayList();
public void addPart(String part) {
carResult.add(part);
}
public Collection getResult() {
return carResult;
}
}
描述:就是把一个复杂对象的过程和部件分开,比如说创建一个汽车
的过程,一个类Builder描述了创建各种部件的过程,另外一个类Concrete描述了组装各种部件的过程
那么我们先生成Builder对象,我们或者在创建过程中创建各种部件,或者在Concrete的组装部件的之
前创建各种部件,然后组装。在Concrete中有个concrete方法,它描述组装或者创建并组装的过程,
我们甚至根据参数的不同动态的构造部件。
/*******************************************************/
package builder;
public interface BuilderInterface {
public void buildPartA();
public void buildPartB();
public void buildPartC();
public Product getResult();
}
/******************************************************************/
package builder;
public class BuilderRealize implements BuilderInterface {
private Product result = ProductFactory.getInstance();
public void buildPartA() {
result.addPart("Part A");
}
public void buildPartB() {
result.addPart("Part B");
}
public void buildPartC() {
result.addPart("Part C");
}
public Product getResult() {
return result;
}
}
/***************************************************************************/
package builder;
public class Direct {
private BuilderInterface builder = null;
public Direct(BuilderInterface builder) {
this.builder = builder;
}
public void construct(int j) {
if (j == 1) {
builder.buildPartA();
}
else if (j == 2) {
builder.buildPartA();
builder.buildPartB();
}
else {
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
}
}
}
/***********************************************************************************/
package builder;
import java.util.*;
public class Main {
public static void main(java.lang.String[] args) {
//在此处插入用来启动应用程序的代码。
BuilderRealize builder = new BuilderRealize();
Direct direct = new Direct(builder);
direct.construct(2);
Product product = builder.getResult();
Collection col = product.getResult();
Iterator ite = col.iterator();
while (ite.hasNext()) {
System.out.println(ite.next());
}
}
}
/***********************************************************************/
package builder;
import java.util.*;
public interface Product {
public void addPart(String part);
public Collection getResult();
}
/***********************************************************************/
package builder;
public class ProductFactory extends ProductRealize {
public static Product getInstance() {
return new ProductRealize();
}
}
/***********************************************************************/
package builder;
import java.util.*;
public class ProductRealize implements Product {
private ArrayList carResult = new ArrayList();
public void addPart(String part) {
carResult.add(part);
}
public Collection getResult() {
return carResult;
}
}
-= 资 源 教 程 =-
文 章 搜 索