1云南大学软件学院实验报告课程:设计模式学期:2012-2013学年第一学期任课教师:陆歌浩专业:软件工程学号:20101120007姓名:孙金文成绩:实验3装饰者模式一、实验目的1.熟悉装饰者模式2.运用装饰者模式编制简单的java程序二、实验内容类图:实验代码:packageheadfirst.decorator;importjava.awt.Container;importjava.awt.Dimension;importjava.awt.GridLayout;importjava.awt.Panel;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;2publicclassEatFood{publicstaticvoidmain(String[]args){FoodFramefoodFrame=newFoodFrame();foodFrame.setSize(400,450);foodFrame.setLocation(FoodFrame.src.width/2-foodFrame.getWidth()/2,FoodFrame.src.height/2-foodFrame.getHeight()/2);foodFrame.setVisible(true);}}classFoodFrameextendsJFrameimplementsActionListener{staticDimensionsrc=Toolkit.getDefaultToolkit().getScreenSize();Restaurantfood;JComboBoxcomboBox;JButton[]seasoning;JLabelfoodName,flavouring,price;String[]tiaoliao={鸡肉(2元),牛肉(2.5元),杂酱(1.5元),酱油(免费),醋(免费),辣椒(免费)};String[]str={粗米线(6元),细米线(7元),面条(6元)};publicFoodFrame(){super(米线店结账系统(孙金文));this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setFoodWindow();}privatevoidsetFoodWindow(){Containerct=this.getContentPane();ct.setLayout(newGridLayout(6,1));Panelp1=newPanel();p1.add(newJLabel(主食:));comboBox=newJComboBox(str);p1.add(comboBox);comboBox.addActionListener(this);ct.add(p1);ct.add(newJLabel(调料选择:));food=newThinRiceNoodle();seasoning=newJButton[tiaoliao.length];JPaneljp2=newJPanel();for(inti=0;itiaoliao.length;i++){3seasoning[i]=newJButton(tiaoliao[i]);seasoning[i].addActionListener(this);jp2.add(seasoning[i]);}ct.add(jp2);ct.add(flavouring=newJLabel(顾客的配套:nothing));ct.add(price=newJLabel(总价格为:+food.cost()));}publicvoidactionPerformed(ActionEvente){if(e.getSource().equals(comboBox)){if(comboBox.getSelectedItem().equals(细米线(7元))){food=newThinRiceNoodle();}elseif(comboBox.getSelectedItem().equals(粗米线(6元))){food=newThickRiceNoodle();}elseif(comboBox.getSelectedItem().equals(面条(6元))){food=newNoodle();}for(inti=0;itiaoliao.length;i++){seasoning[i].setSelected(false);}flavouring.setText(顾客的配套:Nothing);}elseif(e.getActionCommand().equals(鸡肉(2元))){food=newChicken(food);}elseif(e.getActionCommand().equals(牛肉(2.5元))){food=newBeef(food);}elseif(e.getActionCommand().equals(杂酱(1.5元))){food=newSauce(food);}elseif(e.getActionCommand().equals(酱油(免费))){food=newSoy(food);}elseif(e.getActionCommand().equals(醋(免费))){food=newVinegar(food);}elseif(e.getActionCommand().equals(辣椒(免费))){food=newChilli(food);4}flavouring.setText(顾客的配套:+food.getdescription().substring(food.getdescription().indexOf(,)+1));price.setText(总价格为:+food.cost());}}abstractclassRestaurant{Stringdescription=;publicStringgetdescription(){returndescription;}publicabstractdoublecost();}abstractclassSeasoningextendsRestaurant{publicabstractStringgetdescription();}abstractclassCondimentextendsRestaurant{publicabstractStringgetdescription();}classThinRiceNoodleextendsRestaurant{publicThinRiceNoodle(){description=细米线;}publicdoublecost(){return7;}}classThickRiceNoodleextendsRestaurant{publicThickRiceNoodle(){description=粗米线;}publicdoublecost(){return6;}}classNoodleextendsRestaurant{publicNoodle(){description=面条;}publicdoublecost(){5return6;}}classSauceextendsCondiment{RestaurantRestaurant;publicSauce(RestaurantRestaurant){this.Restaurant=Restaurant;}publicStringgetdescription(){returnRestaurant.getdescription()++杂酱;}publicdoublecost(){return1.5+Restaurant.cost();}}classBeefextendsCondiment{RestaurantRestaurant;publicBeef(RestaurantRestaurant){this.Restaurant=Restaurant;}publicStringgetdescription(){returnRestaurant.getdescription()++牛肉;}publicdoublecost(){return2.5+Restaurant.cost();}}classChickenextendsCondiment{RestaurantRestaurant;publicChicken(RestaurantRestaurant){this.Restaurant=Restaurant;}publicStringgetdescription(){6returnRestaurant.getdescription()++鸡肉;}publicdoublecost(){return2.0+Restaurant.cost();}}classChilliextendsSeasoning{RestaurantRestaurant;publicChilli(RestaurantRestaurant){this.Restaurant=Restaurant;}publicStringgetdescription(){returnRestaurant.getdescription()++辣椒;}publicdoublecost(){returnRestaurant.cost();}}classSoyextendsSeasoning{RestaurantRestaurant;publicSoy(RestaurantRestaurant){this.Restaurant=Restaurant;}publicStringgetdescription(){returnRestaurant.getdescription()++酱油;}publicdoublecost(){returnRestaurant.cost();}}7classVinegarextendsSeasoning{RestaurantRestaurant;publicVinegar(RestaurantRestaurant){this.Restaurant=Restaurant;}publicStringgetdescription(){returnRestaurant.getdescription()++醋;}publicdoublecost(){returnRestaurant.cost();}}运行结果截图: