建造者模式定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同得表示。(对象创建型模式)。
建造者模式包含四个角色:
其中,对于指挥者类有一些深入讨论:
- 1 省略Director:将construct()方法中的参数去掉,直接在construct()方法中调用buildPartX()方法;
- 2 钩子方法的引入:钩子方法(Hook Method):返回类型通常为boolean类型,方法名一般为isXXX()。
- package builder.livingdecorator;
- //抽象建造者
- public abstract class Decorator {
- // 创建产品对象
- protected Parlour product = new Parlour();
-
- public abstract void buildWall();
-
- public abstract void buildTV();
-
- public abstract void buildSofa();
-
- // 返回产品对象
- public Parlour getResult() {
- return product;
- }
-
- }
- package builder.livingdecorator;
- //具体建造者:具体装修工人1
- public class ConcreteDecorator1 extends Decorator {
-
- @Override
- public void buildWall() {
- // TODO Auto-generated method stub
- product.setWall("w1");
- }
-
- @Override
- public void buildTV() {
- // TODO Auto-generated method stub
- product.setTV("TV1");
- }
-
- @Override
- public void buildSofa() {
- // TODO Auto-generated method stub
- product.setSofa("sf1");
- }
-
- }
- package builder.livingdecorator;
- //具体建造者:具体装修工人2
- public class ConcreteDecorator2 extends Decorator {
-
- @Override
- public void buildWall() {
- // TODO Auto-generated method stub
- product.setWall("w2");
- }
-
- @Override
- public void buildTV() {
- // TODO Auto-generated method stub
- product.setTV("TV2");
- }
-
- @Override
- public void buildSofa() {
- // TODO Auto-generated method stub
- product.setSofa("sf2");
- }
- }
- package builder.livingdecorator;
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.GridLayout;
-
- import javax.swing.BorderFactory;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
-
- //产品:客厅
- public class Parlour {
- private String wall; // 墙
- private String TV; // 电视
- private String sofa; // 沙发
-
- public void setWall(String wall) {
- this.wall = wall;
- }
-
- public void setTV(String TV) {
- this.TV = TV;
- }
-
- public void setSofa(String sofa) {
- this.sofa = sofa;
- }
-
- public void show() {
- JFrame jf = new JFrame("建造者模式测试");
- Container contentPane = jf.getContentPane();
- JPanel p = new JPanel();
- JScrollPane sp = new JScrollPane(p);
- String parlour = wall + TV + sofa;
- String picture = "src/builder/livingdecorator/" + parlour + ".jpg";
- //String picture = "src/structural_patterns/builder/decorator/" +name ;
- JLabel l = new JLabel(new ImageIcon(picture));
- p.setLayout(new GridLayout(1, 1));
- p.setBorder(BorderFactory.createTitledBorder("客厅"));
- p.add(l);
- contentPane.add(sp, BorderLayout.CENTER);
- jf.pack();
- jf.setVisible(true);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
- package builder.livingdecorator;
-
- //指挥者:项目经理
- public class ProjectManager {
- private Decorator builder;
- public ProjectManager(Decorator builder) {
- this.builder = builder;
- }
- // 产品构建与组装方法
- public Parlour decorate() {
- builder.buildWall();
- builder.buildTV();
- builder.buildSofa();
- return builder.getResult();
- }
- }
- package builder.livingdecorator;
- import javax.xml.parsers.*;
- import org.w3c.dom.*;
- import java.io.*;
- public class ReadXML
- {
- public static Object getObject()
- {
- try
- {
- DocumentBuilderFactory dFactory=DocumentBuilderFactory.newInstance();
- DocumentBuilder builder=dFactory.newDocumentBuilder();
- Document doc;
- doc=builder.parse(new File("src/builder/livingdecorator/config.xml"));
- NodeList nl=doc.getElementsByTagName("className");
- Node classNode=nl.item(0).getFirstChild();
- String cName=classNode.getNodeValue();
- System.out.println("新类名:"+cName);
- Class> c=Class.forName(cName);
- Object obj=c.newInstance();
- return obj;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return null;
- }
- }
- }
- "1.0"?>
- <config>
- <className>builder.livingdecorator.ConcreteDecorator1className>
- config>
- package builder.livingdecorator;
-
- import java.awt.*;
- import javax.swing.*;
-
- public class ParlourDecoratorClient {
- public static void main(String[] args) {
- try {
- //Decorator d=new ConcreteDecorator2();
- Decorator d=(Decorator) ReadXML.getObject();
- ProjectManager m=new ProjectManager(d);
- Parlour p=m.decorate();
- p.show();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }