一、以建造小人为例
建造小人一
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawPerson extends Frame{
public void paint(Graphics g) {
Color c=g.getColor();
g.setColor(Color.BLACK);
g.fillOval(50, 30, 30, 30);//头
g.fillRect(60, 50, 10, 50);//身体
g.drawLine(60, 50, 40, 100);//左手
g.drawLine(70, 50, 90, 100);//右手
g.drawLine(60, 100, 45, 150);//左腿
g.drawLine(70, 100, 85, 150);//右腿
g.setColor(c);
}
public void lauchFrame() {
this.setLocation(400,300);
this.setSize(700,500);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBackground(Color.WHITE);
setVisible(true);
}
}
public class Main{
public static void main(String[] args) {
DrawPerson person=new DrawPerson();
person.lauchFrame();
}
}
建造小人二:增加胖瘦小人类
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class PersonThinBuilder {
private Graphics g;
public PersonThinBuilder(Graphics g) {
super();
this.g = g;
}
public void build() {
Color c = g.getColor();
g.fillOval(50, 50, 30, 30);// 头
g.fillRect(60, 80, 10, 50);// 身体
g.drawLine(60, 80, 40, 130);// 左手
g.drawLine(70, 80, 90, 130);// 右手
g.drawLine(60, 130, 45, 180);// 左腿
g.drawLine(70, 130, 85, 180);// 右腿
g.setColor(c);
}
}
class PersonFatBuilder {
private Graphics g;
public PersonFatBuilder(Graphics g) {
super();
this.g = g;
}
public void build() {
Color c = g.getColor();
g.fillOval(50, 50, 30, 30);// 头
g.fillOval(80, 80, 30, 50);// 身体
g.drawLine(60, 80, 40, 130);// 左手
g.drawLine(70, 80, 90, 130);// 右手
g.drawLine(60, 130, 45, 180);// 左腿
g.drawLine(70, 130, 85, 180);// 右腿
g.setColor(c);
}
}
class DrawPerson extends Frame {
public void paint(Graphics g) {
PersonThinBuilder p=new PersonThinBuilder(g);
p.build();
}
public void lauchFrame() {
this.setLocation(400, 300);
this.setSize(700, 500);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBackground(Color.WHITE);
setVisible(true);
}
}
public class Main{
public static void main(String[] args) {
DrawPerson person=new DrawPerson();
person.lauchFrame();
}
}
二、建造者模式
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
abstract class PersonBuilder { // 人的抽象类
Graphics g;
public PersonBuilder(Graphics g) {
super();
this.g = g;
}
public abstract void buildHead();
public abstract void buildBody();
public abstract void bulidArmLeft();
public abstract void buildArmRight();
public abstract void buildLegLeft();
public abstract void buildLegRight();
}
class PersonThinBuilder extends PersonBuilder {//瘦子类继承人的抽象类
public PersonThinBuilder(Graphics g) {
super(g);
// TODO Auto-generated constructor stub
}
@Override
public void buildHead() {
// TODO Auto-generated method stub
g.fillOval(50, 50, 30, 30);
}
@Override
public void buildBody() {
// TODO Auto-generated method stub
g.fillRect(60, 80, 10, 50);
}
@Override
public void bulidArmLeft() {
// TODO Auto-generated method stub
g.drawLine(60, 80, 40, 130);
}
@Override
public void buildArmRight() {
// TODO Auto-generated method stub
g.drawLine(70, 80, 90, 130);
}
@Override
public void buildLegLeft() {
// TODO Auto-generated method stub
g.drawLine(60, 130, 45, 180);
}
@Override
public void buildLegRight() {
// TODO Auto-generated method stub
g.drawLine(70, 130, 85, 180);
}
}
class PersonFatBulider extends PersonBuilder {//胖子类继承人的抽象类
public PersonFatBulider(Graphics g) {
super(g);
// TODO Auto-generated constructor stub
}
@Override
public void buildHead() {
// TODO Auto-generated method stub
g.fillOval(50, 50, 30, 30);
}
@Override
public void buildBody() {
// TODO Auto-generated method stub
g.fillOval(70, 80, 10, 50);
}
@Override
public void bulidArmLeft() {
// TODO Auto-generated method stub
g.drawLine(60, 80, 40, 130);
}
@Override
public void buildArmRight() {
// TODO Auto-generated method stub
g.drawLine(70, 80, 90, 130);
}
@Override
public void buildLegLeft() {
// TODO Auto-generated method stub
g.drawLine(60, 130, 45, 180);
}
@Override
public void buildLegRight() {
// TODO Auto-generated method stub
g.drawLine(70, 130, 85, 180);
}
}
class PersonDirector {//控制建造过程
private PersonBuilder pb;
public PersonDirector(PersonBuilder pb) {
super();
this.pb = pb;
}
public void createPerson() {
pb.buildHead();
pb.buildBody();
pb.bulidArmLeft();
pb.buildArmRight();
pb.buildLegLeft();
pb.buildLegRight();
}
}
class DrawPerson extends Frame {
public void paint(Graphics g) {
PersonThinBuilder ptb = new PersonThinBuilder(g);
PersonDirector pdThin = new PersonDirector(ptb);
pdThin.createPerson();
}
public void lauchFrame() {
this.setLocation(400, 300);
this.setSize(700, 500);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBackground(Color.WHITE);
setVisible(true);
}
}
public class Main {
public static void main(String[] args) {
DrawPerson person = new DrawPerson();
person.lauchFrame();
}
}
三、建造者模式基本代码
import java.util.List;
import java.util.ArrayList;
//Product类——产品类,由多个部件组成
class Product{
List<String> parts=new ArrayList<String>();
public void Add(String part) {
parts.add(part);
}
public void show() {
System.out.println("产品 创建");
for(String part:parts)
System.out.println(part);
}
}
//Builder类——抽象建造者类,确定产品由两个部件PartA和PartB组成,并声明一个得到产品建造后结果的方法GetResult()。
abstract class Builder{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}
//具体建造者类
class ConcreteBuilder1 extends Builder{
private Product product=new Product();
public Product GetResult() {
return product;
}
@Override
public void BuildPartA() {
// TODO Auto-generated method stub
product.Add("部件A");
}
@Override
public void BuildPartB() {
// TODO Auto-generated method stub
product.Add("部件B");
}
}
class ConcreteBuilder2 extends Builder{
private Product product=new Product();
public Product GetResult() {
return product;
}
@Override
public void BuildPartA() {
// TODO Auto-generated method stub
product.Add("部件X");
}
@Override
public void BuildPartB() {
// TODO Auto-generated method stub
product.Add("部件Y");
}
}
//指挥者类
class Director{
public void Construct(Builder builder) {
builder.BuildPartA();
builder.BuildPartB();
}
}
//客户端代码,客户不知道具体的创建类
public class Main {
public static void main(String[] args) {
Director director=new Director();
Builder b1=new ConcreteBuilder1();
Builder b2=new ConcreteBuilder2();
director.Construct(b1);
Product p1=b1.GetResult();
p1.show();
director.Construct(b2);
}
}