设计模式大作业蛋糕预定系统实验报告.docx
- 文档编号:10504314
- 上传时间:2023-02-17
- 格式:DOCX
- 页数:28
- 大小:861.72KB
设计模式大作业蛋糕预定系统实验报告.docx
《设计模式大作业蛋糕预定系统实验报告.docx》由会员分享,可在线阅读,更多相关《设计模式大作业蛋糕预定系统实验报告.docx(28页珍藏版)》请在冰豆网上搜索。
设计模式大作业蛋糕预定系统实验报告
蛋
糕
订
购
系
统
一、实验介绍
………………………………………………3
二、实验环境
………………………………………………3
三、系统流程
………………………………………………3
四、系统总类图
………………………………………………4
五、程序源代码及各个模块子类图
………………………………………………5
六、程序运行截图
…………………………………………….22
七、实验总结
…………………………………………….22
一、实验介绍
本系统使用了简单工厂模式、工厂方法模式、单例模式、门面模式、策略模式、观察者模式一共6种模式实现了从蛋糕店订购订购蛋糕的这样一个系统。
二、实验环境
Windows操作系统、MyEclipse10.0、MySQL5.5、jdk1.6
三、系统流程
四、系统总类图
五、程序源代码及各个模块子类图
(一)测试类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("本次订购成功完成,需要支付的金额为"+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;
}
}
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){
this.style=style;
}
publicDiscountStylegetStyle(){
returnstyle;
}
publicvoidsetM(doublem){
this.m=m;
}
publicdoublegetM(){
returnm;
}
}
(三)顾客向蛋糕订购蛋糕(观察者模式),并在具体主题中使用了单例模式
a)主题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;
ArrayList
privateinti=0;
privatestaticCakeShopuniqueCakeShop;
privateCakeShop(){
uniqueCakeShop=this;
personList=newArrayList
name="";
changed=false;
}
publicstaticsynchronizedCakeShopgetCakeShop(){
if(uniqueCakeShop==null){
uniqueCakeShop=newCakeShop();
}
returnuniqueCakeShop;
}
publicvoidaddCustomer(Customero){
if(!
(personList.contains(o)))
personList.add(o);
}
publicvoiddeleteCustomer(Customero){
if((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;
publicclassEastVillaimplementsCustomer{
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("甜奶")){
MakeMilkCakeeastVilla1=newMakeMilkCake(cakeType);
MilkFactorymilkCake=eastVilla1.makeMilkCake();
milkCake.createCake();
}
}
}
e)具体观察者二WestVilla.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;
publicclassWestVillaimplementsCustomer{
Shopshop;
privateStringcakeType;
publicWestVilla(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("葡萄水果")){
MakeFruityCakewestVilla=newMakeFruityCake(cakeType);
FruitFactoryfruitCake=westVilla.makeFruityCake();
fruitCake.createCake();
}
elseif(cakeType.equals("牛奶巧克力")||cakeType.equals("黑巧克力")){
MakeChocolateCakewestVilla1=newMakeChocolateCake(cakeType);
ChocolateFactorychocolateCake=westVilla1.makeChocolateCake();
chocolateCake.createCake();
}
elseif(cakeType.equals("双层鲜奶")||cakeType.equals("甜奶")){
MakeMilkCakewestVilla1=newMakeMilkCake(cakeType);
MilkFactorymilkCake=westVilla1.makeMilkCake();
milkCake.createCake();
}
}
}
(四)JDBC(门面模式)JDBCFacade.java:
packagenet.xsl.ordercake.jdbc;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.ResultSetMetaData;
importjava.sql.SQLException;
importjava.sql.Statement;
publicclassJDBCFacade{
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
StringsqlStr="";
publicvoidcreateStatement(){
try{
stmt=conn.createStatement();
}catch(SQLExceptione){
e.printStackTrace();
}
}
publicResultSetjdbcSelect(StringsqlStr){
this.sqlStr=sqlStr;
getConnection();
createStatement();
try{
rs=stmt.executeQuery(sqlStr);
ResultSetMetaDatarsmd=rs.getMetaData();
intj=0;
j=rsmd.getColumnCount();
while(rs.next()){
for(inti=0;i System.out.print(rs.getString(i+1)); System.out.print("\t"); } System.out.println(); } }catch(SQLExceptione){ e.printStackTrace(); } returnrs; } publicvoidjdbcUpdate(StringsqlStr){ this.sqlStr=sqlStr; getConnection(); createStatement(); try{ intcount=stmt.executeUpdate(sqlStr); System.out.println("表更新"+count+"条数据"); }catch(SQLExceptione){ System.out.println("更新数据失败"); } } publicvoidjdbcInsert(StringsqlStr){ this.sqlStr=sqlStr; getConnection(); createStatement(); try{ intcount=stmt.executeUpdate(sqlStr); System.out.println("向表插入"+count+"条数据"); }catch(SQLExceptione){ System.out.println("插入数据失败"+e.getMessage()); } } publicvoidjdbcDelete(StringsqlStr){ this.sqlStr=sqlStr; getConnection(); createStatement(); try{ intcount=stmt.executeUpdate(sqlStr); System.out.println("向表删除"+count+"条数据"); }catch(SQLExceptione){ System.out.println("删除数据失败"); } } publicvoidjdbcClose(){ try{ rs.close(); stmt.close(); conn.close(); }catch(SQLExceptione){ e.printStackTrace(); } } publicConnectiongetConnection(){ try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnectio
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 设计 模式 作业 蛋糕 预定 系统 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)