设计模式大作业实验报告.docx

上传人:聆听****声音 文档编号:564232 上传时间:2023-04-29 格式:DOCX 页数:23 大小:684.78KB
下载 相关 举报
设计模式大作业实验报告.docx_第1页
第1页 / 共23页
设计模式大作业实验报告.docx_第2页
第2页 / 共23页
设计模式大作业实验报告.docx_第3页
第3页 / 共23页
设计模式大作业实验报告.docx_第4页
第4页 / 共23页
设计模式大作业实验报告.docx_第5页
第5页 / 共23页
设计模式大作业实验报告.docx_第6页
第6页 / 共23页
设计模式大作业实验报告.docx_第7页
第7页 / 共23页
设计模式大作业实验报告.docx_第8页
第8页 / 共23页
设计模式大作业实验报告.docx_第9页
第9页 / 共23页
设计模式大作业实验报告.docx_第10页
第10页 / 共23页
设计模式大作业实验报告.docx_第11页
第11页 / 共23页
设计模式大作业实验报告.docx_第12页
第12页 / 共23页
设计模式大作业实验报告.docx_第13页
第13页 / 共23页
设计模式大作业实验报告.docx_第14页
第14页 / 共23页
设计模式大作业实验报告.docx_第15页
第15页 / 共23页
设计模式大作业实验报告.docx_第16页
第16页 / 共23页
设计模式大作业实验报告.docx_第17页
第17页 / 共23页
设计模式大作业实验报告.docx_第18页
第18页 / 共23页
设计模式大作业实验报告.docx_第19页
第19页 / 共23页
设计模式大作业实验报告.docx_第20页
第20页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

设计模式大作业实验报告.docx

《设计模式大作业实验报告.docx》由会员分享,可在线阅读,更多相关《设计模式大作业实验报告.docx(23页珍藏版)》请在冰点文库上搜索。

设计模式大作业实验报告.docx

蛋糕订购系统

糕订购系统

23

一、 实验介绍

………………………………………………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;ArrayListpersonList;

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=new

MakeChocolateCake(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=new

MakeChocolateCake(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.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.getConnection("jdbc:

mysql:

//localhost:

3306/cakeshop","root","admin");

}catch(Exceptione){

System.out.println("数据库连接失败"+e.getMessage());

}

returnconn;

}

}

(五) 构造蛋糕(工厂方法模式):

a)构造者MakeCake.java:

packagenet.xsl.ordercake.creator;

publicinterfaceMakeCake{

}

b)具体构造者一MakeChocolateCake.java:

packagenet.xsl.ordercake.creator;

importnet.xsl.ordercake.product.ChocolateFactory;

publicclassMakeChocolateCakeimplementsMakeCake{

privateStringcakeType;

publicMakeChocolateCake(S

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 交通运输

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2