您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 设计模式大作业-蛋糕预定系统实验报告
蛋糕订购系统1蛋糕订购系统蛋糕订购系统2一、实验介绍………………………………………………3二、实验环境………………………………………………3三、系统流程………………………………………………3四、系统总类图………………………………………………4五、程序源代码及各个模块子类图………………………………………………5六、程序运行截图…………………………………………….22七、实验总结…………………………………………….22蛋糕订购系统3一、实验介绍本系统使用了简单工厂模式、工厂方法模式、单例模式、门面模式、策略模式、观察者模式一共6种模式实现了从蛋糕店订购订购蛋糕的这样一个系统。二、实验环境Windows操作系统、MyEclipse10.0、MySQL5.5、jdk1.6三、系统流程四、系统总类图蛋糕订购系统4五、程序源代码及各个模块子类图(一)测试类Client.java:packagenet.xsl.ordercake.test;importnet.xsl.ordercake.discount.DiscountStyleThree;importnet.xsl.ordercake.discount.OrderCake;importnet.xsl.ordercake.order.CakeShop;importnet.xsl.ordercake.order.EastVilla;importnet.xsl.ordercake.order.WestVilla;publicclassClient{publicstaticvoidmain(Stringarg[]){CakeShopstarCakeShop=CakeShop.getCakeShop();newWestVilla(starCakeShop,草莓水果);starCakeShop.getNewName(西13);starCakeShop.notifyCustomer();OrderCakecake=newOrderCake();System.out.println(今日促销一律8折优惠);cake.setM(8);cake.setStyle(newDiscountStyleThree());cake.setPrice(80);System.out.println(本次订购成功完成,需要支付的金额为蛋糕订购系统5+cake.getMoney(cake.getPrice(),cake.getM())+,原价80);System.out.println(=================================);newEastVilla(starCakeShop,甜奶);starCakeShop.getNewName(东18);starCakeShop.notifyCustomer();System.out.println(=================================);starCakeShop.getNewName(东12);starCakeShop.notifyCustomer();}}(二)折扣的接口和方法(策略模式):a)接口DiscountStyle.java:packagenet.xsl.ordercake.discount;publicinterfaceDiscountStyle{publicdoubledisCount(doubleprice,doublem);}b)折方式一DiscountStyleOne.java:packagenet.xsl.ordercake.discount;publicclassDiscountStyleOneimplementsDiscountStyle{publicdoubledisCount(doubleprice,doublem){returnprice;蛋糕订购系统6}}c)打折方式二DiscountStyleTwo.java:packagenet.xsl.ordercake.discount;publicclassDiscountStyleTwoimplementsDiscountStyle{publicdoubledisCount(doubleprice,doublem){price=price-m;returnprice;}}d)打着方式三DiscountStyleThree.java:packagenet.xsl.ordercake.discount;publicclassDiscountStyleThreeimplementsDiscountStyle{publicdoubledisCount(doubleprice,doublem){price=price*m/10;returnprice;}}e)订蛋糕对象类,使用了get/set方法OrderCake.java:packagenet.xsl.ordercake.discount;publicclassOrderCake{doublemoney,price;//money是打折后的最终价格,price是原价DiscountStylestyle;doublem;publicdoublegetMoney(doubleprice,doublem){returnstyle.disCount(price,m);}publicvoidsetPrice(doubleprice){this.price=price;}publicdoublegetPrice(){returnprice;}publicvoidsetStyle(DiscountStylestyle){蛋糕订购系统7this.style=style;}publicDiscountStylegetStyle(){returnstyle;}publicvoidsetM(doublem){this.m=m;}publicdoublegetM(){returnm;}}(三)顾客向蛋糕订购蛋糕(观察者模式),并在具体主题中使用了单例模式蛋糕订购系统8a)主题Shop.java:packagenet.xsl.ordercake.order;publicinterfaceShop{publicvoidaddCustomer(Customero);publicvoiddeleteCustomer(Customero);publicvoidnotifyCustomer();}b)具体主题(使用了单例模式,只能创建一个蛋糕商店的对象)CakeShop.java:packagenet.xsl.ordercake.order;importjava.util.ArrayList;publicclassCakeShopimplementsShop{Stringname;booleanchanged;ArrayListCustomerpersonList;privateinti=0;privatestaticCakeShopuniqueCakeShop;privateCakeShop(){uniqueCakeShop=this;personList=newArrayListCustomer();name=;changed=false;}publicstaticsynchronizedCakeShopgetCakeShop(){if(uniqueCakeShop==null){uniqueCakeShop=newCakeShop();}returnuniqueCakeShop;}publicvoidaddCustomer(Customero){if(!(personList.contains(o)))personList.add(o);}publicvoiddeleteCustomer(Customero){蛋糕订购系统9if((personList.contains(o)))personList.remove(o);}publicvoidnotifyCustomer(){if(changed){try{Customercustomer=personList.get(i);customer.orderCake(name);i++;changed=false;}catch(Exceptione){System.out.println(没有新的订购信息);}}}publicvoidgetNewName(Stringstr){if(str.equals(name))changed=false;else{name=str;changed=true;}}}c)观察者Customer.java:packagenet.xsl.ordercake.order;publicinterfaceCustomer{publicvoidorderCake(Stringname);}d)体观察者一EastVilla.java:packagenet.xsl.ordercake.order;importnet.xsl.ordercake.creator.MakeChocolateCake;importnet.xsl.ordercake.creator.MakeFruityCake;importnet.xsl.ordercake.creator.MakeMilkCake;importnet.xsl.ordercake.jdbc.JDBCFacade;importnet.xsl.ordercake.product.ChocolateFactory;importnet.xsl.ordercake.product.FruitFactory;importnet.xsl.ordercake.product.MilkFactory;蛋糕订购系统10publicclassEastVillaimplementsCustomer{Shopshop;privateStringcakeType;publicEastVilla(Shopshop,StringcakeType){this.shop=shop;this.cakeType=cakeType;shop.addCustomer(this);}publicvoidorderCake(Stringname){System.out.println(name+订了一个+cakeType+蛋糕);JDBCFacadedbFacade=newJDBCFacade();StringsqlStr=select*fromordercake;StringsqlStr1=insertintoordercake(cakeType,name,quantity)+VALUES('+cakeType+','+name+','1');dbFacade.jdbcInsert(sqlStr1);System.out.println(查询插入之后新的数据库);dbFacade.jdbcSelect(sqlStr);if(cakeType.equals(草莓水果)||cakeType.equals(葡萄水果)){MakeFruityCakeeastVilla=newMakeFruityCake(cakeType);FruitFactoryfruitCake=eastVilla.makeFruityCake();fruitCake.createCake();}elseif(cakeType.equals(牛奶巧克力)||cakeType.equals(黑巧克力)){MakeChocolateCakeeastVilla1=newMakeChocolateCake(cakeType);ChocolateFactorychocolateCake=eastVilla1.makeChocolateCake();chocolateCake.createCake();}elseif(cakeType.equals(双层鲜奶)||cakeType.equals(
本文标题:设计模式大作业-蛋糕预定系统实验报告
链接地址:https://www.777doc.com/doc-5801101 .html