一、定义:将一个复杂对象的创建和它的表示方法分离,使得同样的构建过程可以创建不同的表示。(不明白)
二、组成:
1、Product产品类
2、Builder抽象建造者
3、ConcreteBuilder具体建造者
4、Director导演类
三、通用类图:
四、通用代码:(不是很严谨,大体这个意思,导演类通过控制建造者完成产品的组装、最后返回产品)
1、抽象建造者:
package 通用;
public abstract class Builder {
protected Product product=null;
public Builder() {
product=new Product();
}
public abstract void setProductOne();
public abstract void setProductTwo();
public abstract Product createProduct();
}
2、具体建造者
package 通用;
public class ConcreteBuilder extends Builder {
public ConcreteBuilder() {
super();
}
@Override
public void setProductOne() {
System.out.println("该产品应该先做第一件事");
}
@Override
public void setProductTwo() {
System.out.println("该产品应该先做第二件事");
}
@Override
public Product createProduct() {
return super.product;
}
}
3、导演类:
package 通用;
public class Director {
private Builder builder=null;//建造者是导演类的属性
public Director() {
this.builder=new ConcreteBuilder();
}
//导演类通过建造者先负责安排产品组件顺序,再返回最终产品
public Product createProduct() {
builder.setProductOne();
builder.setProductTwo();
return builder.createProduct();
}
}
4、产品类和场景类:
package 通用;
public class Product {
}
package 通用;
public class Client {
public static void main(String[] args) {
Director d=new Director();
Product p=d.createProduct();
}
}
五、具体示例:画小人(JAVA的GUI忘干净了都)
UML:
1、抽象创建者:
package 画笔画画;
import java.awt.Graphics;
import javax.swing.JPanel;
public abstract class PersonBuilder {
protected Graphics g=null;
public PersonBuilder(Graphics g) {
this.g=g;
}
public abstract void setHead();
public abstract void setBody();
public abstract void setArmLeft();
public abstract void setArmRight();
public abstract void setLegLeft();
public abstract void setLegRight();
}
2、具体创建者:
package 画笔画画;
import java.awt.Graphics;
public class ConcretePersonBuilder extends PersonBuilder {
public ConcretePersonBuilder(Graphics g) {
super(g);
}
@Override
public void setHead() {
g.drawOval(50, 20, 30, 30);
g.drawArc(50, 30, 10, 5, 45, 135);
g.drawArc(70, 30, 10, 5, 45, 135);
g.drawArc(60, 35, 10, 5, 200, 135);
}
@Override
public void setBody() {
g.drawRect(55, 50, 20, 50);
}
@Override
public void setArmLeft() {
g.drawLine(55, 50, 40, 100);
}
@Override
public void setArmRight() {
g.drawLine(75, 50, 90, 100);
}
@Override
public void setLegLeft() {
g.drawLine(55, 100, 45, 150);
}
@Override
public void setLegRight() {
g.drawLine(75, 100, 85, 150);
}
}
3、导演类:
package 画笔画画;
public class Director {
private PersonBuilder p=null;
public Director(PersonBuilder p) {
this.p=p;
}
public void createPerson() {
p.setHead();
p.setBody();
p.setArmLeft();
p.setArmRight();
p.setLegLeft();
p.setLegRight();
}
}
4、测试类:
package 画笔画画;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
// 创建窗口对象
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setTitle("画人");
frame.setSize(250, 300);
// 设置窗口关闭按钮的默认操作(点击关闭时退出进程)
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 把窗口位置设置到屏幕的中心
frame.setLocationRelativeTo(null);
frame.setContentPane(new JPanel(){
protected void paintComponent(Graphics g) {
super.paintComponent(g);
PersonBuilder pb = new ConcretePersonBuilder(g);
Director director = new Director(pb);
director.createPerson();
}
});
}
});
}
}
5、运行结果
The end;