大话设计模式-建造者模式

本文转载自点击打开链接
需求
画一个小人。
实现
级别1

import java.awt.*;
import java.awt.event.*;

public class DrawPerson extends Frame  
{  
    public void paint(Graphics g)  
    {  
        Color c = g.getColor();  
        g.setColor(Color.BLACK);  
        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);  
    }  
    public void lauchFrame()  
    {  
        this.setLocation(400, 300);  
        this.setSize(800, 600);  
        this.addWindowListener(new WindowAdapter()  
        {  
            public void windowClosing(WindowEvent e)  
            {  
                System.exit(0);  
            }  
        });  
        this.setBackground(Color.WHITE);  
        setVisible(true);  
    }  
    public static void main(String[] args)  
    {  
        DrawPerson person = new DrawPerson();  
        person.lauchFrame();  
    }  
}    

级别2
import java.awt.*;

public class PersonFatBuilder  
{  
    private Graphics g;  
    public PersonFatBuilder(Graphics g)  
    {  
        this.g = g;  
    }  
    public void build()  
    {  
        Color c = g.getColor();  
        g.setColor(Color.BLACK);     
        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);  
    }  
} 
import java.awt.*;

public class PersonThinBuilder  
{  
    private Graphics g;  
    public PersonThinBuilder(Graphics g)  
    {  
        this.g = g;  
    }  
    public void build()  
    {  
        Color c = g.getColor();  
        g.setColor(Color.BLACK);     
        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);  
    }  
}  
import java.awt.*;
import java.awt.event.*;

public class Main extends Frame  
{  
    public void paint(Graphics g)  
    {  
    	PersonFatBuilder p = new PersonFatBuilder(g);  
        p.build();  
    }  
    public void lauchFrame()  
    {  
        this.setLocation(400, 300);  
        this.setSize(800, 600);  
  
        this.addWindowListener(new WindowAdapter()  
        {  
            public void windowClosing(WindowEvent e)  
            {  
                System.exit(0);  
            }  
        });  
        this.setBackground(Color.WHITE);  
        setVisible(true);  
    }  
    public static void main(String[] args)  
    {  
        Main drawPerson = new Main();  
        drawPerson.lauchFrame();  
    }  
}  
这样写确实达到了可以复用这两个画小人程序的目的。但比如现在需要你加一个高个小人,你会不会因为编程不注意,让他缺胳膊少腿呢?最好的办法是规定,凡是建造小人,都必须要有头和身体,以及两手两脚。
import java.awt.*;
//画人的抽象类  

public abstract class PersonBuilder  
{  
  protected Graphics g;  
  public PersonBuilder(Graphics g)  
  {  
      this.g = g;  
  }  
  public abstract void buildHead();  
  public abstract void buildBody();  
  public abstract void buildArmLeft();  
  public abstract void buildArmRight();  
  public abstract void buildLegLeft();  
  public abstract void buildLegRight();  
}  
import java.awt.*;
//胖子类继承人的抽象类  

public class PersonFatBuilder extends PersonBuilder  
{  
  public PersonFatBuilder(Graphics g)  
  {  
      super(g);  
  }  
  public void buildArmLeft()  
  {  
      g.drawLine(60, 80, 40, 130);
  }  
  public void buildArmRight()  
  {  
      g.drawLine(70, 80, 90, 130);
  }  
  public void buildBody()  
  {  
      g.fillRect(60, 80, 10, 50);
  }  
  public void buildHead()  
  {  
      g.fillOval(50, 50, 30, 30);
  }  
  public void buildLegLeft()  
  {  
      g.drawLine(60, 130, 45, 180);
  }  
  public void buildLegRight()  
  {  
      g.drawLine(70, 130, 85, 180); 
  }  
}   
import java.awt.*;
//瘦子类继承人的抽象类  

public class PersonThinBuilder extends PersonBuilder  
{  
  public PersonThinBuilder(Graphics g)  
  {  
      super(g);  
  }  
  public void buildArmLeft()  
  {  
      g.drawLine(60, 80, 40, 130);  
  }  
  public void buildArmRight()  
  {  
      g.drawLine(70, 80, 90, 130);
  }  
  public void buildBody()  
  {  
      g.fillRect(60, 80, 10, 50);
  }  
  public void buildHead()  
  {  
      g.fillOval(50, 50, 30, 30);
  } 
  public void buildLegLeft()  
  {  
      g.drawLine(60, 130, 45, 180);
  }  
  public void buildLegRight()  
  {  
      g.drawLine(70, 130, 85, 180);
  }  
}  
public class PersonDirector  
{  
    private PersonBuilder pb;  
    public PersonDirector(PersonBuilder pb)  
    {  
        this.pb = pb;  
    }  
    public void createPerson()  
    {  
        pb.buildHead();  
        pb.buildBody();  
        pb.buildArmLeft();  
        pb.buildArmRight();  
        pb.buildLegLeft();  
        pb.buildLegRight();  
    }  
}  
import java.awt.*;
import java.awt.event.*;

public class Main extends Frame  
{  
    public void paint(Graphics g)  
    {  
        Color c = g.getColor();  
        g.setColor(Color.BLACK);  
        PersonThinBuilder ptb = new PersonThinBuilder(g);  
        PersonDirector pdThin = new PersonDirector(ptb);  
        pdThin.createPerson();  
        g.setColor(c);  
    }  
    public void lauchFrame()  
    {  
        this.setLocation(400, 300);  
        this.setSize(800, 600);  
        this.addWindowListener(new WindowAdapter()  
        {  
            public void windowClosing(WindowEvent e)  
            {  
                System.exit(0);  
            }  
        });  
        this.setBackground(Color.WHITE);  
        setVisible(true);  
    }  
    public static void main(String[] args)  
    {  
        Main drawPerson = new Main();  
        drawPerson.lauchFrame();  
    }  
}  
建造者模式,在当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时适用的模式。这样,通过调用PersonDirector类就可以根据用户的选择一步一步地建造小人了,而建造的过程是在指挥者里已经提前制定好了的,用户就不需要知道了,而且,由于这个过程每一步都是一定要做的,那就不会让少画了一只手,少画了一条腿的问题出现了。

大话设计模式-建造者模式_第1张图片

//Product类由多个部件组成  
public 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  
public abstract class Builder  
{  
    public abstract void buildPartA();  
    public abstract void buildPartB();  
    public abstract Product getResult();  
}  
//ConcreteBuilder1类—具体建造者类  
public class ConcreteBuilder1 extends Builder  
{  
    private Product product = new Product();  
    public void buildPartA()  
    {  
        product.add("部件A");  
    }  
    public void buildPartB()  
    {  
        product.add("部件B");  
    }  
    public Product getResult()  
    {  
        return product;  
    }  
  
}  
// ConcreteBuilder2类—具体建造者类  
public class ConcreteBuilder2 extends Builder  
{  
    private Product product = new Product();  
    public void buildPartA()  
    {  
        product.add("部件X");  
    }  
    public void buildPartB()  
    {  
        product.add("部件Y");  
    }  
    public Product getResult()  
    {  
        return product;  
    }  
  
}  
//Director类—指挥类  
public class Director  
{  
    public void construct(Builder builder)  
    {  
        builder.buildPartA();  
        builder.buildPartB();  
    }  
}  
//客户端代码  
public class Main  
{  
    public static void main(String[] args)  
    {  
        Builder builder1 = new ConcreteBuilder1();  
        Builder builder2 = new ConcreteBuilder2();  
        Director director = new Director();  
        director.construct(builder1);  
        director.construct(builder2);  
        Product product1 = builder1.getResult();  
        Product product2 = builder2.getResult();  
        product1.show();  
        product2.show();  
    }  
}











你可能感兴趣的:(大话设计模式-建造者模式)