首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

java 设计模式之生成器方式

2013-11-14 
java 设计模式之生成器模式1 package cn.zj.qiao.designPattern.builder 23 import javax.swing.JButton

java 设计模式之生成器模式

 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     @Override16     public void buildButton() {17         panel.button = new JButton("btn");18 19     }20 21     @Override22     public void buildLabel() {23         panel.label = new JLabel("lable");24 25     }26 27     @Override28     public void buildTextField() {29         panel.textField = new JTextField("hello");30 31     }32 33     @Override34     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     @Override58     public void buildButton() {59         panel.button = new JButton("btn");60 61     }62 63     @Override64     public void buildLabel() {65         panel.label = new JLabel("label");66 67     }68 69     @Override70     public void buildTextField() {71         panel.textField = new JTextField("helo");72 73     }74 75     @Override76     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 }

热点排行