




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
云南大学软件学院 实验报告课程: 设计模式实验 学期: 2010-2011学年 第一学期 任课教师: 专业: 学号: 姓名: 成绩: 实验3 装饰者模式1.题目:使用装饰着模式实现一个小吃店供应食物,然后计算费用并输出所点的食物和消费金额的程序,其中的被装饰者是粗米线、细米线、面条,装饰者是炸酱、焖肉、番茄鸡蛋以及酱油、醋、辣椒和葱花。2. 模式设计的UML类图:3.程序源代码:(1)食品抽象类FoodStuff.java:public abstract class FoodStuff String description = Unknown FoodStuff;public String getDescription()return description;public abstract double cost();(2)被装饰者的各个子类:public class ThickRicenoodles extends FoodStuff public ThickRicenoodles()description = 粗米线;public double cost()return 4.0;public class ThinRicenoodles extends FoodStuffpublic ThinRicenoodles()description = 细米线;public double cost()return 3.0;public class Noodles extends FoodStuffpublic Noodles()description = 面条;public double cost()return 2.0;(3)调味品抽象类CondimentDecorator.java:public abstract class CondimentDecorator extends FoodStuffpublic abstract String getDescription();(4)调料装饰者的各个子类:public class Firedbeansauce extends CondimentDecoratorFoodStuff foodstuff;public Firedbeansauce(FoodStuff foodstuff)this.foodstuff = foodstuff;public String getDescription()return foodstuff.getDescription() + , 炸酱;public double cost()return 2.0 + foodstuff.cost();public class Daube extends CondimentDecorator FoodStuff foodstuff;public Daube(FoodStuff foodstuff)this.foodstuff = foodstuff;public String getDescription()return foodstuff.getDescription() + , 焖肉;public double cost()return 2.0 + foodstuff.cost();public class TomatoAndEgg extends CondimentDecorator FoodStuff foodstuff;public TomatoAndEgg(FoodStuff foodstuff)this.foodstuff = foodstuff;public String getDescription()return foodstuff.getDescription() + , 番茄鸡蛋;public double cost()return 2.0 + foodstuff.cost();(5)佐料装饰者的各个子类:public class Soy extends CondimentDecorator FoodStuff foodstuff;public Soy(FoodStuff foodstuff)this.foodstuff = foodstuff;public String getDescription()return foodstuff.getDescription() + , 酱油;public double cost()return 0.0 + foodstuff.cost();public class Vinegar extends CondimentDecorator FoodStuff foodstuff;public Vinegar(FoodStuff foodstuff)this.foodstuff = foodstuff;public String getDescription()return foodstuff.getDescription() + , 醋;public double cost()return 0.0 + foodstuff.cost();public class Pepper extends CondimentDecorator FoodStuff foodstuff;public Pepper(FoodStuff foodstuff)this.foodstuff = foodstuff;public String getDescription()return foodstuff.getDescription() + , 辣椒;public double cost()return 0.0 + foodstuff.cost();public class Chives extends CondimentDecorator FoodStuff foodstuff;public Chives(FoodStuff foodstuff)this.foodstuff = foodstuff;public String getDescription()return foodstuff.getDescription() + , 葱花;public double cost()return 0.0 + foodstuff.cost();(6)实现用户界面的主类SnackBar.java:public class SnackBar extends javax.swing.JFrame public SnackBar() super(XXX); initComponents(); private void initComponents() buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); buttonGroup3 = new javax.swing.ButtonGroup(); buttonGroup4 = new javax.swing.ButtonGroup(); buttonGroup5 = new javax.swing.ButtonGroup(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jRadioButton3 = new javax.swing.JRadioButton(); jPanel2 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jPanel3 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jRadioButton4 = new javax.swing.JRadioButton(); jRadioButton5 = new javax.swing.JRadioButton(); jRadioButton6 = new javax.swing.JRadioButton(); jRadioButton7 = new javax.swing.JRadioButton(); jPanel4 = new javax.swing.JPanel(); jLabel6 = new javax.swing.JLabel(); jTextField4 = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jTextField5 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, 种类, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(宋体, 1, 12); / NOI18N jLabel1.setText(选择你需要的种类:); buttonGroup1.add(jRadioButton1); jRadioButton1.setText(粗米线(4元)); buttonGroup1.add(jRadioButton2); jRadioButton2.setText(细米线(3元)); buttonGroup1.add(jRadioButton3); jRadioButton3.setText(面条(2元)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(29, 29, 29) .addComponent(jRadioButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton3) .addContainerGap(77, Short.MAX_VALUE) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component jRadioButton1, jRadioButton2, jRadioButton3); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton1) .addComponent(jRadioButton2) .addComponent(jRadioButton3) .addContainerGap() ); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, 调料, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(宋体, 1, 12); / NOI18N jLabel2.setText(焖肉(每份2元):); jLabel3.setText(炸酱(每份2元):); jLabel4.setText(番茄鸡蛋(每份2元):); jTextField1.setText(0); jTextField1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jTextField1ActionPerformed(evt); ); jTextField2.setText(0); jTextField3.setText(0); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField2) .addComponent(jTextField3) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(91, Short.MAX_VALUE) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel4) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, 佐料, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(宋体, 1, 12); / NOI18N jLabel5.setText(选择你需要的佐料(免费):); buttonGroup2.add(jRadioButton4); jRadioButton4.setText(酱油); jRadioButton4.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jRadioButton4ActionPerformed(evt); ); buttonGroup3.add(jRadioButton5); jRadioButton5.setText(醋); jRadioButton5.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jRadioButton5ActionPerformed(evt); ); buttonGroup4.add(jRadioButton6); jRadioButton6.setText(辣椒); buttonGroup5.add(jRadioButton7); jRadioButton7.setText(葱花); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(jRadioButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton7) .addComponent(jLabel5) .addContainerGap(85, Short.MAX_VALUE) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton4) .addComponent(jRadioButton5) .addComponent(jRadioButton6) .addComponent(jRadioButton7) .addContainerGap() ); jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, 总额, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(宋体, 1, 12); / NOI18N jLabel6.setText(你所点的东西是:); jTextField4.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jTextField4ActionPerformed(evt); ); jLabel7.setText(所需要支付的金额是:); jTextField5.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jTextField5ActionPerformed(evt); ); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6) .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE) .addContainerGap() ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); jButton1.setText(退出); jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton1ActionPerformed(evt); ); jButton2.setText(清空); jButton2.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton2ActionPerformed(evt); ); jButton3.setText(确定); jButton3.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jButton3ActionPerformed(evt); ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane(); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParal
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论