📄 desingpattern_builderfactory.java
字号:
/*****************************************************************
*
* Design Pattern: Builder with Factory
*
*****************************************************************
*
* 程序名称:食品价格计算
* 程序说明:
* 1. 将所购食品分为主食,水果和饮料三组,在左面板目显示。
* 2. 点击任何一个组名,右面板目显示其组内所包括的具体食物。
* 3. 通过使用Builder方法,来控制对不同组食物,右面板的不同显示方式。
* 4. 在底部面板显示各组内食品的总价格。
* 5. 若选择中右面板中的特定食品,只显示所选中条目所代表的价格。
* 6. 若选择中右面板中的全部食品,则其个底部面板上的总价格相等。
*
*****************************************************************
* Author: LU Hongyu (68721123, 62755625)
*
* Class: Advanced Program Design, Peking Univ.
* Teacher: GUO Yingshou
*
* Purpose:
* 1. Demonstrating the Power of Design Pattern and Java.
* 2. Display different panel of different food group.
* 3. Caculating the total cost in one food group.
* 4. Caculating the cost of the selected items in a group.
*
* Run: javac LuhyBuilder.java, java LuhyBuilder
*
* TabSize: 4 Characters
* Complier: JDK 1.2 on Windows98
* Date: Jan, 23, 2002
*
*****************************************************************/
//----------------------------------------------------------------
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.util.*;
import java.util.Vector;;
//----------------------------------------------------------------
public class lbd.java
{
public static void main(String args[])
{
new CostBuilder();
}
}
//----------------------------------------------------------------
// class for data communication using Vector
class MyFood extends Vector
{
String name;
float price;
MyFood()
{
}
MyFood(String nameTmp, float priceTmp)
{
name = nameTmp;
price = priceTmp;
}
}
//----------------------------------------------------------------
// abstract class for listBox, btnBox, checkBox-Choice class
abstract class MultiChoice
{
MyFood choices;
public MultiChoice(MyFood choiceList)
{
choices = choiceList;
}
abstract public Panel getUI(); //return a Panel of components
}
//----------------------------------------------------------------
// class for creating ListBoxChoice
class ListBoxChoice extends MultiChoice implements ItemListener
{
List aList;
Label aLabel;
public ListBoxChoice(MyFood choices)
{
super(choices);
}
// create a panel containing a ListBox
public Panel getUI()
{
Panel p = new Panel();
p.setLayout(new BorderLayout());
aLabel = new Label();
aLabel.setBackground(Color.green);
p.add("South", aLabel);
aList = new List(choices.size()+2);
aList.setMultipleMode(true);
aList.addItemListener(this);
p.add(aList);
for (int i=0; i< choices.size(); i++)
{
MyFood food = (MyFood) choices.get(i);
aList.add(food.name);
}
return p;
}
// event hanlder for selecting the list items
public void itemStateChanged(ItemEvent ievt)
{
List lsTmp;
int lsSelIndex[];
if(ievt.getItemSelectable() instanceof List)
{
lsTmp = (List) (ievt.getItemSelectable());
lsSelIndex = lsTmp.getSelectedIndexes();
// add money together for the selected items
float sum = (float) 0.0;
for (int i=0; i<lsSelIndex.length; i++)
{
int j = lsSelIndex[i];
MyFood food = (MyFood) choices.get(j);
sum = sum + food.price;
}
aLabel.setText((" " + new Float(sum)).toString());
}
}
}
//----------------------------------------------------------------
// class for creating ButtonBox
class BtnBoxChoice extends MultiChoice implements ActionListener
{
int count;
Panel p;
Vector vb;
Label lb;
static int onFlag[];
public BtnBoxChoice(MyFood choices)
{
super(choices);
count = 0;
}
// create a ButtonBox containing several buttons
public Panel getUI()
{
p = new Panel();
vb = new Vector(100, 10);
onFlag = new int[10];
p.setLayout(new GridLayout(choices.size()+1, 1));
// add labeled button boxes to it
for (int i=0; i< choices.size(); i++)
{
MyFood food = (MyFood) choices.get(i);
String s = food.name;
Button cb = new Button(s);
cb.setBackground(Color.pink);
cb.addActionListener(this);
p.add(cb);
vb.addElement(cb);
count++;
}
lb = new Label();
lb.setBackground(Color.green);
p.add("South", lb);
return p;
}
// event handler for clicking the button
public void actionPerformed(ActionEvent evt)
{
Button bTmp;
String sbTmp;
bTmp = (Button) evt.getSource();
sbTmp = bTmp.getLabel();
// set the color of the button according to the selected situation
for(int i=0; i<choices.size(); i++)
{
MyFood food = (MyFood) choices.get(i);
if(food.name==sbTmp)
{
if(onFlag[i]==0)
{
onFlag[i] = 1;
bTmp.setBackground(Color.red);
bTmp.setForeground(Color.red);
}
else if(onFlag[i]==1)
{
onFlag[i] = 0;
bTmp.setBackground(Color.white);
}
}
}
// add money together for the selected items
float sum = (float) 0.0;
for(int i=0; i<choices.size(); i++)
{
if(onFlag[i]==1)
{
MyFood food = (MyFood) choices.get(i);
sum = sum + food.price;
}
}
lb.setText((" " + new Float(sum)).toString());
}
}
//----------------------------------------------------------------
// class for create the CheckBox
class CheckBoxChoice extends MultiChoice implements ItemListener
{
int count;
Panel p;
Vector vcb;
Label lbc;
public CheckBoxChoice(MyFood choices)
{
super(choices);
count = 0;
p = new Panel();
}
// create a CheckBox containing several CheckButtons with Label
public Panel getUI()
{
vcb = new Vector(100, 10);
String s;
p.setLayout(new GridLayout(choices.size()+1, 1));
// add labeled check boxes
for (int i=0; i< choices.size(); i++)
{
MyFood food = (MyFood) choices.get(i);
s = food.name;
Checkbox cb = new Checkbox(s);
cb.addItemListener(this);
p.add(cb);
vcb.addElement(cb);
count++;
}
lbc = new Label();
lbc.setBackground(Color.green);
p.add("South", lbc);
return p;
}
// event handler for selecting the check box
public void itemStateChanged(ItemEvent ievt)
{
Checkbox cbTmp;
if(ievt.getItemSelectable() instanceof Checkbox)
{
// add money togehter of slected items
float sum = (float) 0.0;
for (int i=0; i< choices.size(); i++)
{
cbTmp = (Checkbox) vcb.elementAt(i);
if(cbTmp.getState())
{
MyFood food = (MyFood) choices.get(i);
sum = sum + food.price;
}
}
lbc.setText((" " + new Float(sum)).toString());
}
}
}
//----------------------------------------------------------------
// class using Factory method to assign functions
class choiceFactory
{
MultiChoice ui;
// returns a Panel containing a set of choices
// displayed by one of several UI methods.
public MultiChoice getChoiceUI(MyFood choices)
{
if(choices.size() <=3)
ui = new CheckBoxChoice(choices); // return a panel of checkboxes
else if(choices.size()>=5)
ui = new ListBoxChoice(choices); // return a multi-select list box panel
else
ui = new BtnBoxChoice(choices);
return ui;
}
}
//----------------------------------------------------------------
// Main class for using GUI to calculate food cost
class CostBuilder extends Frame implements ActionListener, ItemListener
{
MyFood MainFood, Fruit, Drink;
List FoodList;
Panel choicePanel;
choiceFactory cfact;
MyFood Bon, Bread, Noodle, Beef, Chicken, Fish;
MyFood Apple, Grape, Peach, Pear;
MyFood Wine, Juice, Milk;
Panel p, pCalc;
Button btnClose;
Label lResult;
public CostBuilder()
{
super("Food Cost Calculator");
setGUI();
buildFoodItems();
cfact = new choiceFactory();
pack();
}
private void setGUI()
{
setLayout(new BorderLayout());
p = new Panel();
add("North", p);
// frmae contains left and right panels
p.setLayout(new GridLayout(1,2));
//left is list of stocks
FoodList= new List(10);
p.add(FoodList);
//right is empty at first
choicePanel = new Panel();
choicePanel.setBackground(Color.red);
p.add(choicePanel);
// bottom panel for dispalying the total money
pCalc = new Panel();
pCalc.setLayout(new BorderLayout());
pCalc.setBackground(Color.lightGray);
add(pCalc);
btnClose = new Button("Close");
btnClose.addActionListener(this);
pCalc.add("East", btnClose);
lResult = new Label();
lResult.setBackground(Color.green);
pCalc.add(lResult);
addWindowListener(new closeWin());
setBounds(100, 100, 300, 200);
setVisible(true);
}
public void buildFoodItems()
{
FoodList.add("MainFood");
FoodList.add("Fruit");
FoodList.add("Drink");
FoodList.addItemListener(this);
MainFood = new MyFood();
Bon = new MyFood("Bon", (float) 4.0);
Bread = new MyFood("Bread", (float) 6.2);
Noodle = new MyFood("Noodle", (float) 3.5);
Beef = new MyFood ("Beef", (float) 15.8);
Chicken = new MyFood("Chicken", (float) 12.6);
Fish = new MyFood("Fish", (float) 8.2);
MainFood.addElement(Bon);
MainFood.addElement(Bread);
MainFood.addElement(Noodle);
MainFood.addElement(Beef);
MainFood.addElement(Chicken);
MainFood.addElement(Fish);
Fruit = new MyFood();
Apple = new MyFood("Apple", (float) 5.8);
Grape = new MyFood("Grape", (float) 3.2);
Peach = new MyFood("Peach", (float) 2.8);
Pear = new MyFood("Pear", (float) 4.6);
Fruit.addElement(Apple);
Fruit.addElement(Grape);
Fruit.addElement(Peach);
Fruit.addElement(Pear);
Drink = new MyFood();
Wine = new MyFood("Wine", (float) 12.5);
Juice = new MyFood("Juice", (float) 6.8);
Milk = new MyFood("Milk", (float) 5.0);
Drink.addElement(Wine);
Drink.addElement(Juice);
Drink.addElement(Milk);
}
// event handler to close by clicking the close button
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==btnClose)
{
dispose();
System.exit(0);
}
}
// event handler to dispaly the sub-items on the right panel
// by clicking the food group name on the left panel
public void itemStateChanged(ItemEvent ievt)
{
if(ievt.getSource()==FoodList)
{
MyFood v = null;
int index = FoodList.getSelectedIndex();
//this just switches among 3 different MyFoods
//and passes the one you select to the Builder pattern
switch(index)
{
case 0:
v = MainFood;
break;
case 1:
v = Fruit;
break;
case 2:
v = Drink;
break;
}
MultiChoice mchoice = cfact.getChoiceUI(v); // get one of the UIs
choicePanel.removeAll(); // remove previous ui panel
choicePanel.add(mchoice.getUI()); // insert in right panel
choicePanel.validate(); // re-layout and display
// calculate the total money of a food goup
float sum = (float) 0.0;
for (int i=0; i<v.size(); i++)
{
MyFood food = (MyFood) v.get(i);
sum = sum + food.price;
}
lResult.setText((" " + new Float(sum)).toString());
}
}
}
//----------------------------------------------------------------
// class for closing windows by clicking 'x' on the frame
class closeWin extends WindowAdapter
{
public void winodowClose(WindowEvent evt)
{
Frame frm = (Frame) (evt.getSource());
frm.dispose();
System.exit(0);
}
}
//----------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -