java 设计模式之生成器模式
生成器模式:将一个复杂对象的构建与它的表示分离,使同样的构建过程可以创建不同的表示(Separate the construction of a complex object from its representation so that the same construction process can create different representations)。
生成器模式结构中的四种角色。
产品(Product): 所要构建的复杂对象。
抽象生成器(Builder): 抽象生成器是一个接口,该接口除了为创建一个Product对象的各个组件定义若干个方法外,还要定义返回Product对象的方法。
具体生成器(ConcreteBuilder): 实现Buidler接口的类。
指挥者(Director): 指挥者是一个类,该类需含有Builder接口申明的变量,指挥者的职责是负责向用户提供具体的生成器,利用具体生成器构造复杂的Product对象。
代码如下:
Product类:
1 package cn.zj.qiao.designPattern.builder; 2 3 import javax.swing.JButton; 4 import javax.swing.JLabel; 5 import javax.swing.JPanel; 6 import javax.swing.JTextField; 7 8 public class PanelProduct extends JPanel { 9 /** 10 * 11 */ 12 private static final long serialVersionUID = 1L; 13 JButton button; 14 JLabel label; 15 JTextField textField; 16 }
Builder 接口:
1 package cn.zj.qiao.designPattern.builder; 2 3 import javax.swing.JButton; 4 import javax.swing.JLabel; 5 import javax.swing.JPanel; 6 import javax.swing.JTextField; 7 8 public class PanelProduct extends JPanel { 9 /** 10 * 11 */ 12 private static final long serialVersionUID = 1L; 13 JButton button; 14 JLabel label; 15 JTextField textField; 16 }
ConcreteBuilder 如下两个类(ConcreteBuilderOne和ConcreteBuilderTwo):
1 package cn.zj.qiao.designPattern.builder; 2 3 4 import javax.swing.JButton; 5 import javax.swing.JLabel; 6 import javax.swing.JPanel; 7 import javax.swing.JTextField; 8 9 public class ConcreteBuilderOne implements Builder { 10 private PanelProduct panel; //创建需要的容器 11 12 ConcreteBuilderOne(){ 13 panel = new PanelProduct(); 14 } 15 @Override 16 public void buildButton() { 17 panel.button = new JButton("btn"); 18 19 } 20 21 @Override 22 public void buildLabel() { 23 panel.label = new JLabel("lable"); 24 25 } 26 27 @Override 28 public void buildTextField() { 29 panel.textField = new JTextField("hello"); 30 31 } 32 33 @Override 34 public JPanel getPanel() { 35 panel.add(panel.button); 36 panel.add(panel.label); 37 panel.add(panel.textField); 38 return panel; 39 } 40 41 } 42 43 44 package cn.zj.qiao.designPattern.builder; 45 46 import javax.swing.JButton; 47 import javax.swing.JLabel; 48 import javax.swing.JPanel; 49 import javax.swing.JTextField; 50 51 public class ConcreteBuilderTwo implements Builder { 52 private PanelProduct panel; 53 54 ConcreteBuilderTwo(){ 55 panel = new PanelProduct(); 56 } 57 @Override 58 public void buildButton() { 59 panel.button = new JButton("btn"); 60 61 } 62 63 @Override 64 public void buildLabel() { 65 panel.label = new JLabel("label"); 66 67 } 68 69 @Override 70 public void buildTextField() { 71 panel.textField = new JTextField("helo"); 72 73 } 74 75 @Override 76 public JPanel getPanel() { 77 panel.add(panel.textField); 78 panel.add(panel.label); 79 panel.add(panel.button); 80 return panel; 81 } 82 83 }
Director类:
1 package cn.zj.qiao.designPattern.builder; 2 3 import javax.swing.JPanel; 4 5 public class Director { 6 private Builder builder; 7 8 Director(Builder builder){ 9 this.builder = builder; 10 } 11 12 public JPanel constructProduct(){ 13 builder.buildButton(); 14 builder.buildLabel(); 15 builder.buildTextField(); 16 return builder.getPanel(); 17 } 18 }
下面是测试代码:
1 package cn.zj.qiao.designPattern.builder; 2 3 import java.awt.Container; 4 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 8 public class Application { 9 public static void main(String[] args){ 10 Builder builder = new ConcreteBuilderOne(); 11 Director dir = new Director(builder); 12 JPanel panel = dir.constructProduct(); 13 JFrame frameOne = new JFrame("hello"); 14 Container con = frameOne.getContentPane(); 15 con.add(panel); 16 frameOne.setBounds(12, 12, 200, 120); 17 frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frameOne.setVisible(true); 19 20 builder = new ConcreteBuilderTwo(); 21 dir = new Director(builder); 22 panel = dir.constructProduct(); 23 JFrame frameTwo = new JFrame("hello"); 24 con = frameTwo.getContentPane(); 25 con.add(panel); 26 frameTwo.setBounds(222, 12, 200, 120); 27 frameTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 frameTwo.setVisible(true); 29 30 } 31 }
人生最可贵的事情是sb似的坚持于追求……