设计模式大作业-蛋糕预定系统实验报告(共23页).docx
《设计模式大作业-蛋糕预定系统实验报告(共23页).docx》由会员分享,可在线阅读,更多相关《设计模式大作业-蛋糕预定系统实验报告(共23页).docx(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上蛋糕订购系统一、 实验介绍3二、 实验环境3三、 系统流程3四、 系统总类图4五、 程序源代码及各个模块子类图5六、 程序运行截图.22七、 实验总结.22一、 实验介绍本系统使用了简单工厂模式、工厂方法模式、单例模式、门面模式、策略模式、观察者模式一共6种模式实现了从蛋糕店订购订购蛋糕的这样一个系统。二、 实验环境 Windows操作系统、MyEclipse10.0、MySQL 5.5、jdk1.6三、 系统流程四、 系统总类图五、 程序源代码及各个模块子类图(一) 测试类Client.java:package net.xsl.ordercake.test;impo
2、rt net.xsl.ordercake.discount.DiscountStyleThree;import net.xsl.ordercake.discount.OrderCake;import net.xsl.ordercake.order.CakeShop;import net.xsl.ordercake.order.EastVilla;import net.xsl.ordercake.order.WestVilla;public class Client public static void main (String arg)CakeShop starCakeShop = CakeS
3、hop.getCakeShop();new WestVilla(starCakeShop, 草莓水果);starCakeShop.getNewName(西13);starCakeShop.notifyCustomer();OrderCake cake = new OrderCake();System.out.println(今日促销一律8折优惠);cake.setM(8);cake.setStyle(new DiscountStyleThree();cake.setPrice(80) ;System.out.println(本次订购成功完成,需要支付的金额为+cake.getMoney(cak
4、e.getPrice(),cake.getM()+,原价80);System.out.println(=);new EastVilla(starCakeShop, 甜奶);starCakeShop.getNewName(东18);starCakeShop.notifyCustomer();System.out.println(=);starCakeShop.getNewName(东12);starCakeShop.notifyCustomer();(二) 折扣的接口和方法(策略模式):a) 接口DiscountStyle.java:package net.xsl.ordercake.disco
5、unt;public interface DiscountStyle public double disCount(double price,double m);b) 折方式一DiscountStyleOne.java:package net.xsl.ordercake.discount;public class DiscountStyleOne implements DiscountStylepublic double disCount(double price,double m) return price;c) 打折方式二DiscountStyleTwo.java:package net.
6、xsl.ordercake.discount;public class DiscountStyleTwo implements DiscountStylepublic double disCount(double price,double m) price = price - m;return price;d) 打着方式三DiscountStyleThree.java:package net.xsl.ordercake.discount;public class DiscountStyleThree implements DiscountStylepublic double disCount(
7、double price,double m) price = price * m/10;return price;e) 订蛋糕对象类,使用了get/set方法OrderCake.java:package net.xsl.ordercake.discount;public class OrderCake double money,price;/money是打折后的最终价格,price是原价DiscountStyle style;double m;public double getMoney(double price,double m)return style.disCount(price,m);
8、 public void setPrice(double price)this.price = price ;public double getPrice()return price;public void setStyle(DiscountStyle style)this.style = style ;public DiscountStyle getStyle()return style;public void setM(double m)this.m = m ;public double getM()return m;(三) 顾客向蛋糕订购蛋糕(观察者模式),并在具体主题中使用了单例模式a
9、) 主题Shop.java:package net.xsl.ordercake.order;public interface Shop public void addCustomer(Customer o);public void deleteCustomer(Customer o);public void notifyCustomer();b) 具体主题(使用了单例模式,只能创建一个蛋糕商店的对象)CakeShop.java:package net.xsl.ordercake.order;import java.util.ArrayList;public class CakeShop imp
10、lements ShopString name;boolean changed;ArrayListpersonList;private int i = 0;private static CakeShop uniqueCakeShop;private CakeShop() uniqueCakeShop = this;personList = new ArrayList();name = ;changed = false;public static synchronized CakeShop getCakeShop()if(uniqueCakeShop = null)uniqueCakeShop
11、= new CakeShop();return uniqueCakeShop;public void addCustomer(Customer o) if(!(personList.contains(o)personList.add(o);public void deleteCustomer(Customer o) if(personList.contains(o)personList.remove(o);public void notifyCustomer() if(changed)tryCustomer customer = personList.get(i);customer.order
12、Cake(name);i+;changed = false;catch(Exception e)System.out.println(没有新的订购信息);public void getNewName(String str)if(str.equals(name)changed = false;elsename = str;changed = true;c) 观察者Customer.java:package net.xsl.ordercake.order;public interface Customer public void orderCake(String name);d) 体观察者一Eas
13、tVilla.java:package net.xsl.ordercake.order;import net.xsl.ordercake.creator.MakeChocolateCake;import net.xsl.ordercake.creator.MakeFruityCake;import net.xsl.ordercake.creator.MakeMilkCake;import net.xsl.ordercake.jdbc.JDBCFacade;import net.xsl.ordercake.product.ChocolateFactory;import net.xsl.order
14、cake.product.FruitFactory;import net.xsl.ordercake.product.MilkFactory;public class EastVilla implements CustomerShop shop;private String cakeType;public EastVilla(Shop shop,String cakeType)this.shop = shop;this.cakeType = cakeType;shop.addCustomer(this);public void orderCake(String name)System.out.
15、println(name+订了一个+cakeType+蛋糕);JDBCFacade dbFacade=new JDBCFacade(); String sqlStr = select * from ordercake; String sqlStr1 = insert into ordercake(cakeType, name, quantity)+ VALUES (+cakeType+,+name+,1); dbFacade.jdbcInsert(sqlStr1);System.out.println(查询插入之后新的数据库); dbFacade.jdbcSelect(sqlStr);if(c
16、akeType.equals(草莓水果) | cakeType.equals(葡萄水果)MakeFruityCake eastVilla = new MakeFruityCake(cakeType);FruitFactory fruitCake = eastVilla.makeFruityCake();fruitCake.createCake();else if(cakeType.equals(牛奶巧克力) | cakeType.equals(黑巧克力)MakeChocolateCake eastVilla1 = new MakeChocolateCake(cakeType);Chocolat
17、eFactory chocolateCake = eastVilla1.makeChocolateCake();chocolateCake.createCake();else if(cakeType.equals(双层鲜奶) | cakeType.equals(甜奶)MakeMilkCake eastVilla1 = new MakeMilkCake(cakeType);MilkFactory milkCake = eastVilla1.makeMilkCake();milkCake.createCake(); e) 具体观察者二WestVilla.java:package net.xsl.o
18、rdercake.order;import net.xsl.ordercake.creator.MakeChocolateCake;import net.xsl.ordercake.creator.MakeFruityCake;import net.xsl.ordercake.creator.MakeMilkCake;import net.xsl.ordercake.jdbc.JDBCFacade;import net.xsl.ordercake.product.ChocolateFactory;import net.xsl.ordercake.product.FruitFactory;imp
19、ort net.xsl.ordercake.product.MilkFactory;public class WestVilla implements CustomerShop shop;private String cakeType;public WestVilla(Shop shop,String cakeType)this.shop = shop;this.cakeType = cakeType;shop.addCustomer(this);public void orderCake(String name)System.out.println(name+订了一个+cakeType+蛋糕
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 设计 模式 作业 蛋糕 预定 系统 实验 报告 23
限制150内