软件设计模式与体系结构实验——2.2-1生成器模式的应用

1.实验目的

  1. 掌握生成器模式的特点
  2. 分析具体问题,使用生成器模式进行设计。

2.实验内容

【作业2.2-1】在例2.5的设计中,添加一个经济型房屋生成器类,命名为EconHouseBuilder。注意经济型房屋的面积比较小,卧室、卫生间和车库的数量较少,切不包含花园和游泳池。设计并且写出实现代码,具体要求参见光盘的相应作业部分。

3.模式UML图

在这里插入图片描述

4.模式添加代码(JAVA语言实现)

HouseBuyerGUI.class类

public static final String ECONOMY_HOUSE = "Economy House";
//添加
cmbHouseType.addItem(ECONOMY_HOUSE);
else if(str.equals(HouseBuyerGUI.ECONOMY_HOUSE)){
    
    
    builder = new EconHouseBuilder();
 }

EconHouseBuilder.class类

public void addUIComponents(){
    
    
    houseGUI = new JPanel();
     houseGUI.setLayout(new GridLayout(4,3));
 JLabel label1 = new JLabel("Area:");
 JLabel label2 = new JLabel("Bed room number:");
 JLabel label3 = new JLabel("Bathroom number:");
 JLabel label4 = new JLabel("Garage type:");

 //Add code here
     JRadioButton areaBtn1 = new JRadioButton(SMALL_AREA);
     JRadioButton areaBtn2 = new JRadioButton(BIG_AREA);
     JRadioButton bedroomBtn1 = new JRadioButton(LESS_BEDROOM);
     JRadioButton bedroomBtn2 = new JRadioButton(MORE_BEDROOM);
     JRadioButton bathroomBtn1 = new JRadioButton(LESS_BATHROOM);
     JRadioButton bathroomBtn2 = new JRadioButton(MORE_BATHROOM);
     JRadioButton grageBtn1 = new JRadioButton(SMALL_GARAGE);
     JRadioButton grageBtn2 = new JRadioButton(BIG_GARAGE);

     ButtonGroup areaGroup = new ButtonGroup();
     ButtonGroup bedroomGroup = new ButtonGroup();
     ButtonGroup bathroomGroup = new ButtonGroup();
     ButtonGroup garageGroup = new ButtonGroup();


     areaGroup.add(areaBtn1);
     areaGroup.add(areaBtn2);
     bedroomGroup.add(bedroomBtn1);
     bedroomGroup.add(bedroomBtn2);
     bathroomGroup.add(bathroomBtn1);
     bathroomGroup.add(bathroomBtn2);
     garageGroup.add(grageBtn1);
     garageGroup.add(grageBtn2);



     houseGUI.add(label1);
     houseGUI.add(areaBtn1);
     houseGUI.add(areaBtn2);
     houseGUI.add(label2);
     houseGUI.add(bedroomBtn1);
     houseGUI.add(bedroomBtn2);
     houseGUI.add(label3);
     houseGUI.add(bathroomBtn1);
     houseGUI.add(bathroomBtn2);
     houseGUI.add(label4);
     houseGUI.add(grageBtn1);
     houseGUI.add(grageBtn2);


     areaBtn1.addActionListener(new AreaListener());
     areaBtn2.addActionListener(new AreaListener());
     bedroomBtn1.addActionListener(new BedroomListener());
     bedroomBtn2.addActionListener(new BedroomListener());
     bathroomBtn1.addActionListener(new BathroomListener());
     bathroomBtn2.addActionListener(new BathroomListener());
     grageBtn1.addActionListener(new GarageListener());
     grageBtn2.addActionListener(new GarageListener());






 }

/* Build up a whole object incrementally */
public void buildType(){
    
    
house.setType(ECONOMY_TYPE);
}
public void buildArea(){
    
    
    house.setArea(area);
// Add code here
}
public void buildBedroom(){
    
    
    house.setBedroom(bedroom);
   // Add code here
}
public void buildBathroom(){
    
    
    house.setBathroom(bathroom);
// Add code here
}
public void buildGarage(){
    
    
    house.setGarage(garage);
// Add code here
}
public void buildGarden(){
    
    
    house.setGarden(garden);
// Add code here
}
public void buildSwimmingpool(){
    
    
    house.setSwimmingPool(swimmingPool);
   // Add code here
}

5.完整代码

(1)Director


//This is the director in the builder pattern

public class Director{
    
    
   private HouseBuilder hsBuilder;

   public void setHouseBuilder(HouseBuilder hb){
    
    
      hsBuilder = hb;
   }
   public House getHouse(){
    
    
      return hsBuilder.getHouse();
   }

   public void constructWholeHouseObj() {
    
    
	  hsBuilder.createNewHouseProduct();
	  hsBuilder.buildType();
	  hsBuilder.buildArea();
	  hsBuilder.buildBedroom();
	  hsBuilder.buildBathroom();
	  hsBuilder.buildGarage();
	  hsBuilder.buildGarden();
	  hsBuilder.buildSwimmingpool();
   }
}

(2)EconHouseBuilder


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class EconHouseBuilder extends HouseBuilder{
    
    
   public static final String SMALL_AREA = "180 sq meters";
   public static final String BIG_AREA = "210 sq meters";
   public static final String LESS_BEDROOM = "2";
   public static final String MORE_BEDROOM = "3";
   public static final String LESS_BATHROOM = "1";
   public static final String MORE_BATHROOM = "2";
   public static final String SMALL_GARAGE = "1-car garage";
   public static final String BIG_GARAGE = "2-car garage";
   public static final String ECONOMY_TYPE = "Economy-type house";

   public EconHouseBuilder(){
    
    
	  area = null;
      bedroom = null;
      bathroom = null;
      garage = null;
      garden = null;
      swimmingPool = null;
   }
   public void addUIComponents(){
    
    
      houseGUI = new JPanel();
  	  houseGUI.setLayout(new GridLayout(4,3));
	  JLabel label1 = new JLabel("Area:");
	  JLabel label2 = new JLabel("Bed room number:");
	  JLabel label3 = new JLabel("Bathroom number:");
	  JLabel label4 = new JLabel("Garage type:");

	  //Add code here
       JRadioButton areaBtn1 = new JRadioButton(SMALL_AREA);
       JRadioButton areaBtn2 = new JRadioButton(BIG_AREA);
       JRadioButton bedroomBtn1 = new JRadioButton(LESS_BEDROOM);
       JRadioButton bedroomBtn2 = new JRadioButton(MORE_BEDROOM);
       JRadioButton bathroomBtn1 = new JRadioButton(LESS_BATHROOM);
       JRadioButton bathroomBtn2 = new JRadioButton(MORE_BATHROOM);
       JRadioButton grageBtn1 = new JRadioButton(SMALL_GARAGE);
       JRadioButton grageBtn2 = new JRadioButton(BIG_GARAGE);

       ButtonGroup areaGroup = new ButtonGroup();
       ButtonGroup bedroomGroup = new ButtonGroup();
       ButtonGroup bathroomGroup = new ButtonGroup();
       ButtonGroup garageGroup = new ButtonGroup();
       
       areaGroup.add(areaBtn1);
       areaGroup.add(areaBtn2);
       bedroomGroup.add(bedroomBtn1);
       bedroomGroup.add(bedroomBtn2);
       bathroomGroup.add(bathroomBtn1);
       bathroomGroup.add(bathroomBtn2);
       garageGroup.add(grageBtn1);
       garageGroup.add(grageBtn2);
       
       houseGUI.add(label1);
       houseGUI.add(areaBtn1);
       houseGUI.add(areaBtn2);
       houseGUI.add(label2);
       houseGUI.add(bedroomBtn1);
       houseGUI.add(bedroomBtn2);
       houseGUI.add(label3);
       houseGUI.add(bathroomBtn1);
       houseGUI.add(bathroomBtn2);
       houseGUI.add(label4);
       houseGUI.add(grageBtn1);
       houseGUI.add(grageBtn2);
       
       areaBtn1.addActionListener(new AreaListener());
       areaBtn2.addActionListener(new AreaListener());
       bedroomBtn1.addActionListener(new BedroomListener());
       bedroomBtn2.addActionListener(new BedroomListener());
       bathroomBtn1.addActionListener(new BathroomListener());
       bathroomBtn2.addActionListener(new BathroomListener());
       grageBtn1.addActionListener(new GarageListener());
       grageBtn2.addActionListener(new GarageListener());
   }

  /* Build up a whole object incrementally */
  public void buildType(){
    
    
	 house.setType(ECONOMY_TYPE);
  }
  public void buildArea(){
    
    
      house.setArea(area);
	 // Add code here
  }
  public void buildBedroom(){
    
    
      house.setBedroom(bedroom);
     // Add code here
  }
  public void buildBathroom(){
    
    
      house.setBathroom(bathroom);
	 // Add code here
  }
  public void buildGarage(){
    
    
      house.setGarage(garage);
	 // Add code here
  }
  public void buildGarden(){
    
    
      house.setGarden(garden);
	 // Add code here
  }
  public void buildSwimmingpool(){
    
    
      house.setSwimmingPool(swimmingPool);
     // Add code here
  }

  //This method returns user chosen requests
  //as a string to be displayed on screen
  public String getUserRequest(){
    
    
	 String usrRequest = null;
	 if((area==null)||(bedroom==null)||(bathroom==null)||(garage==null)){
    
    
	    usrRequest = "Incomplete items";
	 }
	 else{
    
    
	    usrRequest = ECONOMY_TYPE
                     +"\nArea =" + area
                     +"\nBedroom number= " + bedroom
                     + "\nBathroom number = " + bathroom
                     + "\nGarage type = " + garage;

	 }
     return usrRequest;
  }
}// end class

(3)House

package com.xusheng.House221;

import java.util.*;


class House{
    
    
   private String type = null;
   private String area;
   private String bedroom;
   private String bathroom;
   private String garage;
   private String garden;
   private String swimmingPool;
   private List features = new ArrayList();

   final int LuxAreaUnitPrice = 8000;
   final int LuxBedrmUnitPrice = 7000;
   final int LuxBathrmUnitPrice = 5000;
   final int LuxGarage1Price = 20000;
   final int LuxGarage2Price = 40000;
   final int LuxSmallGardenPrice = 20000;
   final int LuxBigGardenPrice = 40000;
   final int LuxSmallSwimpoolPrice = 30000;
   final int LuxBigSwimpoolPrice = 80000;

   final int NormAreaUnitPrice = 6000;
   final int NormBedrmUnitPrice = 5000;
   final int NormBathrmUnitPrice = 3000;
   final int NormGarage1Price = 15000;
   final int NormGarage2Price = 28000;
   final int NormSmallGardenPrice = 15000;
   final int NormBigGardenPrice = 30000;

   final int EconAreaUnitPrice = 4000;
   final int EconBedrmUnitPrice = 3500;
   final int EconBathrmUnitPrice = 2000;
   final int EconGarage1Price = 10000;
   final int EconGarage2Price = 20000;

   public House(){
    
    
   }
   public void setType(String type){
    
    
       this.type = type;
   }
   public void setArea(String area){
    
    
	   this.area = area;
   }
   public void setBedroom(String bedroom){
    
    
	   this.bedroom = bedroom;
   }
   public void setBathroom(String bathroom){
    
    
	   this.bathroom = bathroom;
   }
   public void setGarage(String garage){
    
    
	   this.garage = garage;
   }
   public void setGarden(String garden){
    
    
	   this.garden = garden;
   }
   public void setSwimmingPool(String swimmingPool){
    
    
	   this.swimmingPool = swimmingPool;
   }
   public String getType(){
    
    
       return type;
   }
   public String getArea(){
    
    
	   return area;
   }
   public String getBedroom(){
    
    
	   return bedroom;
   }
   public String getBathroom(){
    
    
	   return bathroom;
   }
   public String getGarage(){
    
    
	   return garage;
   }
   public String getGarden(){
    
    
	   return garden;
   }
   public String getSwimmingPool(){
    
    
	   return swimmingPool;
   }

   public int getHousePrice(){
    
    
	  int totalPrice = 0;
	  int intArea = 0;
	  int intBedrmNum = 0;
	  int intBathroomNum = 0;
	  int garagePrice = 0;
	  int gardenPrice = 0;
	  int swimpoolPrice = 0;

	  if(area != null){
    
    
	     String[] a = area.split("\\s");
	     intArea = Integer.parseInt(a[0]);
	  }
	  if(bedroom != null){
    
    
	     intBedrmNum = Integer.parseInt(bedroom);
	  }
	  if(bathroom != null){
    
    
	   	 intBathroomNum = Integer.parseInt(bathroom);
	  }

	  if(type.compareTo(EconHouseBuilder.ECONOMY_TYPE)==0){
    
    
		 if(garage.compareTo(EconHouseBuilder.SMALL_GARAGE)==0){
    
    
		    garagePrice = EconGarage1Price;
		 }
		 else if(garage.compareTo(EconHouseBuilder.BIG_GARAGE)==0){
    
    
		    garagePrice = EconGarage2Price;
		 }
		 totalPrice =  intArea* EconAreaUnitPrice
		                + intBedrmNum*EconBedrmUnitPrice
		                + intBathroomNum*EconBathrmUnitPrice
		                + garagePrice;
	   }
	   else if(type.compareTo(NormHouseBuilder.NORMAL_TYPE)==0){
    
    
	      if(garage.compareTo(NormHouseBuilder.SMALL_GARAGE)==0){
    
    
	         garagePrice = NormGarage1Price;
		  }
	   	  else if(garage.compareTo(NormHouseBuilder.BIG_GARAGE)==0){
    
    
	   		 garagePrice = NormGarage2Price;
		  }
		  if(garden.compareTo(NormHouseBuilder.SMALL_GARDEN)==0){
    
    
		  	 gardenPrice = NormSmallGardenPrice;
		  }
		  else if(garden.compareTo(NormHouseBuilder.BIG_GARDEN)==0){
    
    
		  	 gardenPrice = NormBigGardenPrice;
		  }
	   	  totalPrice =  intArea* NormAreaUnitPrice
	   		            + intBedrmNum*NormBedrmUnitPrice
	   		            + intBathroomNum*NormBathrmUnitPrice
	   		            + garagePrice
	   		            + gardenPrice;
	   }
	   else if(type.compareTo(LuxHouseBuilder.LUXURY_TYPE)==0){
    
    
	      if(garage.compareTo(LuxHouseBuilder.SMALL_GARAGE)==0){
    
    
	   	     garagePrice = LuxGarage1Price;
	   	  }
	   	  else if(garage.compareTo(LuxHouseBuilder.BIG_GARAGE)==0){
    
    
	   	     garagePrice = LuxGarage2Price;
	   	  }

	   	  if(garden.compareTo(LuxHouseBuilder.SMALL_GARDEN)==0){
    
    
			 gardenPrice = LuxSmallGardenPrice;
		  }
		  else if(garden.compareTo(LuxHouseBuilder.BIG_GARDEN)==0){
    
    
			 gardenPrice = LuxBigGardenPrice;
	   	  }

	   	  if(swimmingPool.compareTo(LuxHouseBuilder.SMALL_SWIMING_POOL)==0){
    
    
		     swimpoolPrice = LuxSmallSwimpoolPrice;
		  }
		  else if(swimmingPool.compareTo(LuxHouseBuilder.BIG_SWIMING_POOL)==0){
    
    
			 swimpoolPrice = LuxBigSwimpoolPrice;
	   	  }
	   	  totalPrice =  intArea* LuxAreaUnitPrice
	   	   		        + intBedrmNum*LuxBedrmUnitPrice
	   	   		        + intBathroomNum*LuxBathrmUnitPrice
	   	   		        + garagePrice
	   	   		        + gardenPrice
	   	   		        + swimpoolPrice;
	   }
	   return totalPrice;
   }
}

(4)HouseBuilder

package com.xusheng.House221;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


abstract class HouseBuilder{
    
    
   protected House house;
   protected JPanel houseGUI;
   protected String area;
   protected String bedroom;
   protected String bathroom;
   protected String garage;
   protected String garden;
   protected String swimmingPool;

   public House getHouse() {
    
    
      return house;
   }
   public void createNewHouseProduct() {
    
    
      house = new House();
   }

   public abstract void buildType();
   public abstract void buildArea();
   public abstract void buildBedroom();
   public abstract void buildBathroom();
   public abstract void buildGarage();
   public abstract void buildGarden();
   public abstract void buildSwimmingpool();

   public JPanel getSearchUI(){
    
    
	  return houseGUI;
   }

   //This listener sets-up area
     class AreaListener implements ActionListener{
    
    
        public void actionPerformed(ActionEvent e){
    
    
           area = (String)e.getActionCommand();
        }
     }
     //This listener sets-up bedroom number
     class BedroomListener implements ActionListener{
    
    
        public void actionPerformed(ActionEvent e){
    
    
           bedroom = (String)e.getActionCommand();
        }
     }
     //This listener sets-up bathroom number
     class BathroomListener implements ActionListener{
    
    
        public void actionPerformed(ActionEvent e){
    
    
           bathroom = (String)e.getActionCommand();
        }
     }
     //This listener sets-up garage number
     class GarageListener implements ActionListener{
    
    
        public void actionPerformed(ActionEvent e){
    
    
           garage = (String)e.getActionCommand();
        }
     }
     //This listener sets-up garden area
     class GardenListener implements ActionListener{
    
    
        public void actionPerformed(ActionEvent e){
    
    
           garden = (String)e.getActionCommand();
        }
     }
     //This listener sets-up swimming pool size
     class SwPoolListener implements ActionListener{
    
    
        public void actionPerformed(ActionEvent e){
    
    
           swimmingPool = (String)e.getActionCommand();
        }
  }

   public abstract String getUserRequest();
   public abstract void addUIComponents();
}

(5)HouseBuyerGUI

package com.xusheng.House221;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class HouseBuyerGUI extends JPanel{
    
    
   private JScrollPane btnPane;
   private JScrollPane houseInfoPane;
   private JTextArea txtHouseInfo;
   private JSplitPane bigSplitPane;
   private JSplitPane upSplitPane;
   private JPanel downPanel;
   private JComboBox cmbHouseType;
   private JPanel buttonPanel;
   private JPanel houseOptPanel;
   private ButtonHandler bh;

   static final Dimension minimumSize = new Dimension(230, 200);
   public static final String SUBMIT= "Submit";
   public static final String EXIT = "Exit";
    //添加
   public static final String ECONOMY_HOUSE = "Economy House";
   public static final String NORMAL_HOUSE = "Normal House";
   public static final String LUXURY_HOUSE = "Luxury House";
   public static final String BLANK = "Choose House Type";

   public HouseBuyerGUI(){
    
    
      super(new GridLayout(1,0));
	  txtHouseInfo=new JTextArea(6, 20);
	  txtHouseInfo.setFont(new Font("Arial", Font.BOLD, 14));
	  txtHouseInfo.setLineWrap(true);
      txtHouseInfo.setBackground(Color.pink);
      txtHouseInfo.setText("House Information");

      bh = new ButtonHandler();

      setupLowerPanel();
	  setupUpperLeftPanel();
      buildUpScrollGUI();
   }

   private void setupLowerPanel(){
    
    
      downPanel = new JPanel();
      downPanel.setBackground(Color.gray);
	  JButton btnSubmit = new JButton(HouseBuyerGUI.SUBMIT);
	  btnSubmit.setMnemonic(KeyEvent.VK_G);
	  JButton btnExit = new JButton(HouseBuyerGUI.EXIT);
	  btnExit.setMnemonic(KeyEvent.VK_X);
	  btnSubmit.addActionListener(bh);
	  btnExit.addActionListener(bh);

      downPanel.add(btnSubmit);
      downPanel.add(btnExit);
   }
   private void setupUpperLeftPanel(){
    
    
      cmbHouseType = new JComboBox();
      cmbHouseType.addItem(BLANK);

      cmbHouseType.addItem(NORMAL_HOUSE);
      cmbHouseType.addItem(LUXURY_HOUSE);
      //添加
      cmbHouseType.addItem(ECONOMY_HOUSE);

      JLabel lblHouseType = new JLabel("House Type:");
      JLabel lblHouseOptions = new JLabel("Options:");
      cmbHouseType.addActionListener(bh);

      //For layout purposes, put the buttons in a separate panel
      buttonPanel = new JPanel();
      // houseOptPanel is an empty panel for holding a HouseGUI dynamically
      // houseOptPanel will be put on buttonPanel
      houseOptPanel = new JPanel();
      houseOptPanel.setPreferredSize(new Dimension(360, 180));

      //****************************************************
      GridBagLayout gridbag = new GridBagLayout();

      buttonPanel.setLayout(gridbag);
      GridBagConstraints gbc = new GridBagConstraints();

      buttonPanel.add(cmbHouseType);
      buttonPanel.add(houseOptPanel);

      gbc.insets.top = 5;
      gbc.insets.bottom = 5;
      gbc.insets.left = 5;
      gbc.insets.right = 5;

      gbc.anchor = GridBagConstraints.WEST;
      gbc.gridx = 0;
      gbc.gridy = 0;
      gridbag.setConstraints(cmbHouseType, gbc);
      gbc.gridx = 0;
      gbc.gridy = 1;
      gridbag.setConstraints(houseOptPanel, gbc);
   }

   public void showHouseInfo(String str){
    
    
      txtHouseInfo.setText(str);
   }
   public String getHouseType(){
    
    
      return (String) cmbHouseType.getSelectedItem();
   }
   public JComboBox getHouseTypeCombox(){
    
    
      return cmbHouseType;
   }
   public void displayNewGUI(JPanel panel){
    
    
      houseOptPanel.removeAll();
      houseOptPanel.add(panel);
      houseOptPanel.validate();
      validate();
   }
   private void buildUpScrollGUI(){
    
    
      btnPane = new JScrollPane(buttonPanel);
	  btnPane.setMinimumSize(minimumSize);
	  houseInfoPane = new JScrollPane(txtHouseInfo);

	  upSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	  upSplitPane.setDividerLocation(390);
	  upSplitPane.setPreferredSize(new Dimension(600, 280));

	  upSplitPane.setLeftComponent(btnPane);
	  upSplitPane.setRightComponent(houseInfoPane);
	  bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upSplitPane, downPanel);
	  bigSplitPane.setDividerLocation(280);

	  add(bigSplitPane);
	  setSize(new Dimension(600, 300));
      setVisible(true);
  }

  private static void createAndShowGUI(){
    
    
     JFrame.setDefaultLookAndFeelDecorated(true);
     JFrame frame = new JFrame("Builder Pattern-House Sale Software");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     HouseBuyerGUI newContentPane = new HouseBuyerGUI();
     newContentPane.setOpaque(true);
     frame.setContentPane(newContentPane);

     frame.pack();
     frame.setVisible(true);
  }

  static public void main(String argv[]){
    
    
	 SwingUtilities.invokeLater(new Runnable(){
    
    
	    public void run() {
    
    
		   createAndShowGUI();
		}
        });
  }
  class ButtonHandler implements ActionListener{
    
    
     HouseBuilder builder;

     public void actionPerformed(ActionEvent e) {
    
    
        if (e.getActionCommand().equals(EXIT)) {
    
    
           System.exit(1);
        }
        if (e.getActionCommand().equals(SUBMIT)) {
    
    
		   if(builder != null){
    
    
              //For displaying user's request
              String usrRequest = builder.getUserRequest();
              showHouseInfo(usrRequest);


		      Director director = new Director();

		      //decide which builder to use
		      director.setHouseBuilder(builder);

		      //For constructing the whole House object
		      director.constructWholeHouseObj();

		      //get a House object
		      House hsObj = director.getHouse();

		      //Show house object
		      int housePrice = hsObj.getHousePrice();
		      txtHouseInfo.append("\n House Price: "+housePrice);
	       }
        }
	    if (e.getSource() == getHouseTypeCombox()) {
    
    
           String selection = getHouseType();
           if ( (selection.equals("") == false) && (selection.equals(BLANK) == false)) {
    
    
              BuilderFactory factory = new BuilderFactory();

              //Get a concrete builder,eg., Object NormHouseBuilder
              //and add this component to the GUI
              builder = factory.getUIBuilder(selection);
              builder.addUIComponents();

              //Get and display the currently chosen Gui component
              JPanel UIObj = builder.getSearchUI();
              displayNewGUI(UIObj);
              buttonPanel.repaint();
           }
        }
     }
  }
}

class BuilderFactory{
    
    
   public HouseBuilder getUIBuilder(String str){
    
    
      HouseBuilder builder = null;

      if (str.equals(HouseBuyerGUI.NORMAL_HOUSE)){
    
    
         builder = new NormHouseBuilder();
      }
      else if (str.equals(HouseBuyerGUI.LUXURY_HOUSE)){
    
    
	     builder = new LuxHouseBuilder();
      }else if(str.equals(HouseBuyerGUI.ECONOMY_HOUSE)){
    
    
          builder = new EconHouseBuilder();
       }
      return builder;
   }
}

(6)LuxHouseBuilder

package com.xusheng.House221;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sun.java.swing.plaf.windows.*;
import java.util.*;

class LuxHouseBuilder extends HouseBuilder {
    
    
   public static final String SMALL_AREA = "450 sq meters";
   public static final String BIG_AREA = "600 sq meters";
   public static final String LESS_BEDROOM = "5";
   public static final String MORE_BEDROOM = "8";
   public static final String LESS_BATHROOM = "5";
   public static final String MORE_BATHROOM = "8";
   public static final String SMALL_GARAGE = "4-car garage";
   public static final String BIG_GARAGE = "6-car garage";
   public static final String SMALL_GARDEN = "200-Sqm garden";
   public static final String BIG_GARDEN = "350-Sqm garden";
   public static final String SMALL_SWIMING_POOL = "20m X 10m";
   public static final String BIG_SWIMING_POOL = "50m X 20m";
   public static final String LUXURY_TYPE = "Luxury-type house";


   public LuxHouseBuilder(){
    
    
   	  area = null;
      bedroom = null;
      bathroom = null;
      garage = null;
      garden = null;
      swimmingPool = null;
   }
   public void addUIComponents(){
    
    
      houseGUI = new JPanel();
      houseGUI.setLayout(new GridLayout(6,3));
  	  JLabel label1 = new JLabel("Area:");
  	  JLabel label2 = new JLabel("Bed room number:");
  	  JLabel label3 = new JLabel("Bathroom number:");
  	  JLabel label4 = new JLabel("Garage type:");
  	  JLabel label5 = new JLabel("Garden type:");
  	  JLabel label6 = new JLabel("Swimming pool type:");

  	  JRadioButton areaBtn1 = new JRadioButton(SMALL_AREA);
  	  JRadioButton areaBtn2 = new JRadioButton(BIG_AREA);
  	  JRadioButton bedroomBtn1 = new JRadioButton(LESS_BEDROOM);
  	  JRadioButton bedroomBtn2 = new JRadioButton(MORE_BEDROOM);
  	  JRadioButton bathroomBtn1 = new JRadioButton(LESS_BATHROOM);
  	  JRadioButton bathroomBtn2 = new JRadioButton(MORE_BATHROOM);
  	  JRadioButton grageBtn1 = new JRadioButton(SMALL_GARAGE);
  	  JRadioButton grageBtn2 = new JRadioButton(BIG_GARAGE);

  	  JRadioButton gardenBtn1 = new JRadioButton(SMALL_GARDEN);
  	  JRadioButton gardenBtn2 = new JRadioButton(BIG_GARDEN);

  	  JRadioButton swPoolBtn1 = new JRadioButton(SMALL_SWIMING_POOL);
  	  JRadioButton swPoolBtn2 = new JRadioButton(BIG_SWIMING_POOL);

  	  ButtonGroup areaGroup = new ButtonGroup();
  	  ButtonGroup bedroomGroup = new ButtonGroup();
  	  ButtonGroup bathroomGroup = new ButtonGroup();
  	  ButtonGroup garageGroup = new ButtonGroup();
  	  ButtonGroup gardenGroup = new ButtonGroup();
  	  ButtonGroup swPoolGroup = new ButtonGroup();


  	  areaGroup.add(areaBtn1);
  	  areaGroup.add(areaBtn2);
  	  bedroomGroup.add(bedroomBtn1);
  	  bedroomGroup.add(bedroomBtn2);
  	  bathroomGroup.add(bathroomBtn1);
  	  bathroomGroup.add(bathroomBtn2);
  	  garageGroup.add(grageBtn1);
  	  garageGroup.add(grageBtn2);

  	  gardenGroup.add(gardenBtn1);
  	  gardenGroup.add(gardenBtn2);

  	  swPoolGroup.add(swPoolBtn1);
  	  swPoolGroup.add(swPoolBtn2);

  	  houseGUI.add(label1);
      houseGUI.add(areaBtn1);
  	  houseGUI.add(areaBtn2);
  	  houseGUI.add(label2);
  	  houseGUI.add(bedroomBtn1);
  	  houseGUI.add(bedroomBtn2);
  	  houseGUI.add(label3);
  	  houseGUI.add(bathroomBtn1);
  	  houseGUI.add(bathroomBtn2);
  	  houseGUI.add(label4);
  	  houseGUI.add(grageBtn1);
  	  houseGUI.add(grageBtn2);
      houseGUI.add(label5);
  	  houseGUI.add(gardenBtn1);
  	  houseGUI.add(gardenBtn2);

  	  houseGUI.add(label6);
	  houseGUI.add(swPoolBtn1);
  	  houseGUI.add(swPoolBtn2);

  	  areaBtn1.addActionListener(new AreaListener());
  	  areaBtn2.addActionListener(new AreaListener());
      bedroomBtn1.addActionListener(new BedroomListener());
      bedroomBtn2.addActionListener(new BedroomListener());
  	  bathroomBtn1.addActionListener(new BathroomListener());
  	  bathroomBtn2.addActionListener(new BathroomListener());
  	  grageBtn1.addActionListener(new GarageListener());
      grageBtn2.addActionListener(new GarageListener());

      gardenBtn1.addActionListener(new GardenListener());
      gardenBtn2.addActionListener(new GardenListener());

      swPoolBtn1.addActionListener(new SwPoolListener());
      swPoolBtn2.addActionListener(new SwPoolListener());
  }

  /* Build up a whole object incrementally */
  public void buildType(){
    
    
	 house.setType(LUXURY_TYPE);
  }
  public void buildArea(){
    
    
	 house.setArea(area);
  }
  public void buildBedroom(){
    
    
     house.setBedroom(bedroom);
  }
  public void buildBathroom(){
    
    
	 house.setBathroom(bathroom);
  }
  public void buildGarage(){
    
    
	 house.setGarage(garage);
  }
  public void buildGarden(){
    
    
	 house.setGarden(garden);
  }
  public void buildSwimmingpool(){
    
    
	 house.setSwimmingPool(swimmingPool);
  }

  //This method returns user chosen requests
  //as a string to be displayed on screen
  public String getUserRequest(){
    
    
  	 String usrRequest = null;
  	 if((area==null)||(bedroom==null)||(bathroom==null)||(garage==null)
  	                ||(garden==null)||(swimmingPool==null)){
    
    
  	    usrRequest = "Incomplete items";
  	 }
  	 else{
    
    
  	    usrRequest = LUXURY_TYPE
                     +"\nArea =" + area
                     +"\nBedroom number = " + bedroom
                     +"\nBathroom number = " + bathroom
                     +"\nGarage type = " + garage
                     +"\nGarden="+garden
                     +"\nSwimmingPool="+swimmingPool;

  	 }
     return usrRequest;
  }
}// end class


(7)NormHouseBuilder

package com.xusheng.House221;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sun.java.swing.plaf.windows.*;
import java.util.*;

class NormHouseBuilder extends HouseBuilder{
    
    
   public static final String SMALL_AREA = "220 sq meters";
   public static final String BIG_AREA = "330 sq meters";
   public static final String LESS_BEDROOM = "4";
   public static final String MORE_BEDROOM = "6";
   public static final String LESS_BATHROOM = "2";
   public static final String MORE_BATHROOM = "4";
   public static final String SMALL_GARAGE = "2-car garage";
   public static final String BIG_GARAGE = "3-car garage";
   public static final String SMALL_GARDEN = "40-Sqm garden";
   public static final String BIG_GARDEN = "60-Sqm garden";
   public static final String NORMAL_TYPE = "Normal-type house";


   public NormHouseBuilder(){
    
    
      area = null;
      bedroom = null;
      bathroom = null;
      garage = null;
      garden = null;
      swimmingPool = null;
   }
   public void addUIComponents(){
    
    
      houseGUI = new JPanel();
  	  houseGUI.setLayout(new GridLayout(5,3));
	  JLabel label1 = new JLabel("Area:");
	  JLabel label2 = new JLabel("Bed room number:");
	  JLabel label3 = new JLabel("Bathroom number:");
	  JLabel label4 = new JLabel("Garage type:");
	  JLabel label5 = new JLabel("Garden type:");

	  JRadioButton areaBtn1 = new JRadioButton(SMALL_AREA);
	  JRadioButton areaBtn2 = new JRadioButton(BIG_AREA);
	  JRadioButton bedroomBtn1 = new JRadioButton(LESS_BEDROOM);
	  JRadioButton bedroomBtn2 = new JRadioButton(MORE_BEDROOM);
	  JRadioButton bathroomBtn1 = new JRadioButton(LESS_BATHROOM);
	  JRadioButton bathroomBtn2 = new JRadioButton(MORE_BATHROOM);
	  JRadioButton grageBtn1 = new JRadioButton(SMALL_GARAGE);
	  JRadioButton grageBtn2 = new JRadioButton(BIG_GARAGE);

	  JRadioButton gardenBtn1 = new JRadioButton(SMALL_GARDEN);
	  JRadioButton gardenBtn2 = new JRadioButton(BIG_GARDEN);

	  ButtonGroup areaGroup = new ButtonGroup();
	  ButtonGroup bedroomGroup = new ButtonGroup();
	  ButtonGroup bathroomGroup = new ButtonGroup();
	  ButtonGroup garageGroup = new ButtonGroup();
	  ButtonGroup gardenGroup = new ButtonGroup();

	  areaGroup.add(areaBtn1);
	  areaGroup.add(areaBtn2);
	  bedroomGroup.add(bedroomBtn1);
	  bedroomGroup.add(bedroomBtn2);
	  bathroomGroup.add(bathroomBtn1);
	  bathroomGroup.add(bathroomBtn2);
	  garageGroup.add(grageBtn1);
	  garageGroup.add(grageBtn2);

	  gardenGroup.add(gardenBtn1);
	  gardenGroup.add(gardenBtn2);

	  houseGUI.add(label1);
      houseGUI.add(areaBtn1);
	  houseGUI.add(areaBtn2);
	  houseGUI.add(label2);
	  houseGUI.add(bedroomBtn1);
	  houseGUI.add(bedroomBtn2);
	  houseGUI.add(label3);
	  houseGUI.add(bathroomBtn1);
	  houseGUI.add(bathroomBtn2);
	  houseGUI.add(label4);
	  houseGUI.add(grageBtn1);
	  houseGUI.add(grageBtn2);
      houseGUI.add(label5);
	  houseGUI.add(gardenBtn1);
	  houseGUI.add(gardenBtn2);

	  areaBtn1.addActionListener(new AreaListener());
	  areaBtn2.addActionListener(new AreaListener());
      bedroomBtn1.addActionListener(new BedroomListener());
      bedroomBtn2.addActionListener(new BedroomListener());
	  bathroomBtn1.addActionListener(new BathroomListener());
	  bathroomBtn2.addActionListener(new BathroomListener());
	  grageBtn1.addActionListener(new GarageListener());
      grageBtn2.addActionListener(new GarageListener());

      gardenBtn1.addActionListener(new GardenListener());
      gardenBtn2.addActionListener(new GardenListener());
  }

  /* Build up a whole object incrementally */
  public void buildType(){
    
    
  	 house.setType(NORMAL_TYPE);
  }
  public void buildArea(){
    
    
  	 house.setArea(area);
  }
  public void buildBedroom(){
    
    
     house.setBedroom(bedroom);
  }
  public void buildBathroom(){
    
    
  	 house.setBathroom(bathroom);
  }
  public void buildGarage(){
    
    
  	 house.setGarage(garage);
  }
  public void buildGarden(){
    
    
  	 house.setGarden(garden);
  }
  public void buildSwimmingpool(){
    
    
  	 house.setSwimmingPool(swimmingPool);
  }

  //This method returns user chosen requests
  //as a string to be displayed on screen
  public String getUserRequest(){
    
    
     String usrRequest = null;
     if((area==null)||(bedroom==null)||(bathroom==null)
                    ||(garage==null) ||(garden==null)){
    
    
        usrRequest = "Incomplete items";
     }
     else{
    
    
        usrRequest = NORMAL_TYPE
                     +"\nArea =" + area
                     + "\nBedroom number= " + bedroom
                     + "\nBathroom number = " + bathroom
                     + "\nGarage type = " + garage
                     + "\nGarden=" + garden;
     }
     return usrRequest;
  }
}// end class


6.运行截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7.实验小结

(1)生成器模式

是一种对象的创建模式,可以将一个复杂产品的内部表象和产品的生产过程分隔开来,(构建与表示分离)。使得同样的构建过程可以创建不同的表示。
简单的来说,就是讲一个复杂的对象拆分成一个一个小的对象,然后通过排列组合的方式生成不同的复杂对象。举个栗子:例如快餐店的套餐,会有个定价,比如说总价是20元套餐。可以有两素菜一个荤菜。素菜包括清炒白菜、土豆丝、麻婆豆腐等等,荤菜包括口水鸡,啤酒鸭,小炒肉等等。这样只要是两素菜一荤菜组合,就能形成一个个套餐。这其实就类似于生成器模式。这里的复杂产品是套餐,而生产过程则是一个一个的小菜。

(2)生成器模式示意图

在这里插入图片描述

(3)生成器模式所含角色

产品角色: 产品是要构建的复杂对象。
抽象建造者角色: 为创建产品角色对象的各个部分指定抽象接口,一般至少包含有两个方法,一个是建造产品,一个是返回产品。
具体建造者角色: 实现抽象建造者角色。并且根据不同的情形对于产品创建给出具体的方法。并且能够在创建完成后返回产品实例。
导演角色: 构造一个能够使用抽象建造者的对象。该角色不涉及具体的产品信息。主要负责和客户端打交道。
4.什么是生成器模式
生成器模式是创建型模式的一种,指使用多个实例通过一定的步骤来生成所需的类的实例,这里的步骤是相同的,但是通过传递不同的参数来达到生成不同实例的目的。
(1)意图
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
(2)使用的场合
在以下情况使用Builder模式:
1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时;
2.当构造过程必须允许被构造的对象有不同的表示时。
(3)效果
它使你可以改变一个产品的内部表示。Builder对象提供给导向器一个构造产品的抽象接口。该接口使得生成器可以隐藏这个产品的表示和内部结构。它同时也隐藏了该产品是如何装配的。因为产品是通过抽象接口构造的,你在改变该产品的内部表示是所要做的只是定义一个新的生成器。
它将构造代码和表示代码分开。Builder模式通过封装一个复杂对象的创建和表示方式提高了对象的模块性。客户不需要知道定义产品内部结构的类的所有信息,这些类是不出现在Builder接口中的。
它使你可对构造过程进行更精细的控制。Builder模式与一下子就生成产品的创建型模式不同,它是在导向器的控制下一步一步构造产品的。仅当该产品完成时导向器才从生成器中取回它。因此Builder接口相比其他创建型模式能更好地反映产品的构造过程。这使你可以更精细地控制构建过程,从而能更精细地控制所得产品的内部结构。
(4)优点
建造者独立,易扩展。
便于控制细节风险。
(5)缺点
产品必须有共同点,范围有限制。
如内部变化复杂,会有很多的建造类。

猜你喜欢

转载自blog.csdn.net/m0_52435951/article/details/124664064
今日推荐