版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验10:抽象工厂模式一、实验目的本实验要求学生掌握抽象工厂模式,要求学生理解抽象工厂模式中的四种角色并完成类的定义及测试。二、实验任务1.服装工厂能够生成不同类型的配套服装,但用户不知道这些配套的服装是如何被创建出来的。北京服装厂用来生成西服套装,上海服装厂用来生产牛仔套装,天津服装厂用来生产运动套装,在不修改原代码的基础上可以灵活地添加生产新的配套服装类型。使用抽象工厂模式分析有哪些类以及这些类的角色,完成这些类的定义并进行测试。答:抽象产品角色:类UpperClothes,类Trousers具体产品角色:类WesternUpperClothes,类CowboyUpperClothes,类SportUpperClothes,类CowboyTrousers,类WesternTrousers,类SportTrousers抽象工厂角色:类ClothesFactory,具体工厂角色:类BeiJingClothesFactory,类ShangHaiClothesFactory,类TianJinClothesFactory抽象产品(AbstractProduct):UpperClothes.javapublicabstractclassUpperClothes{publicabstractintgetChestSize();publicabstractintgetHeight();publicabstractStringgetName();}Trousers.javapublicabstractclassTrousers{publicabstractintgetWaistSize();publicabstractintgetHeight();publicabstractStringgetName();}具体产品(ConcreteProduct):WesternUpperClothes.javapublicclassWesternUpperClothesextendsUpperClothes{privateintchestSize;privateintheight;privateStringname;WesternUpperClothes(Stringname,intchestSize,intheight){this.chestSize=chestSize;this.height=height;=name;}publicintgetChestSize(){returnchestSize;}publicintgetHeight(){returnheight;}publicStringgetName(){returnname;}}CowboyUpperClothes.javapublicclassCowboyUpperClothesextendsUpperClothes{privateintchestSize;privateintheight;privateStringname;CowboyUpperClothes(Stringname,intchestSize,intheight){this.chestSize=chestSize;this.height=height;=name;}publicStringgetName(){return;}publicintgetHeight(){returnthis.height;}publicintgetChestSize(){returnthis.chestSize;}}SportUpperClothes.javapublicclassSportUpperClothesextendsUpperClothes{privateStringname;privateintheight;privateintchestSize;SportUpperClothes(Stringname,intheight,intchestSize){=name;this.height=height;this.chestSize=chestSize;}publicStringgetName(){return;}publicintgetHeight(){returnthis.height;}publicintgetChestSize(){returnthis.chestSize;WesternTrousers.javapublicclassWesternTrousersextendsTrousers{privateintwaistSize;privateintheight;privateStringname;WesternTrousers(Stringname,intwaistSize,intheight){=name;this.waistSize=waistSize;this.height=height;}publicStringgetName(){return;}publicintgetHeight(){returnthis.height;}publicintgetWaistSize(){returnthis.waistSize;}}CowboyTrousers.javapublicclassCowboyTrousersextendsTrousers{privateintwaistSize;privateintheight;privateStringname;CowboyTrousers(Stringname,intwaistSize,intheight){=name;this.waistSize=waistSize;this.height=height;}publicintgetHeight(){returnheight;}publicintgetWaistSize(){returnwaistSize;}publicStringgetName(){returnname;}}SportTrousers.javapublicclassSportTrousersextendsTrousers{privateintwaistSize;privateintheight;privateStringname;SportTrousers(Stringname,intwaistSize,intheight){=name;this.waistSize=waistSize;this.height=height;}publicintgetHeight(){returnheight;}publicintgetWaistSize(){returnwaistSize;}publicStringgetName(){returnname;}}抽象工厂(AbstractFactory):ClothesFactory.javapublicabstractclassClothesFactory{publicabstractUpperClothescreateUpperClothes(intchestSize,intheight);publicabstractTrouserscreateTrousers(intwaistSize,intheight);具体工厂(ConcreteFactory):BeiJingClothesFactory.javapublicclassBeiJingClothesFactoryextendsClothesFactory{publicUpperClothescreateUpperClothes(intchestSize,intheight){returnnewWesternUpperClothes("北京牌西服上衣",chestSize,height);}publicTrouserscreateTrousers(intwaistSize,intheight){returnnewWesternTrousers("北京牌西服下衣",waistSize,height);}}ShangHaiClothesFactory.javapublicclassShangHaiClothesFactoryextendsClothesFactory{publicUpperClothescreateUpperClothes(intchestSize,intheight){returnnewCowboyUpperClothes("上海牌牛仔上衣",chestSize,height);}publicTrouserscreateTrousers(intwaistSize,intheight){returnnewCowboyTrousers("上海牌牛仔下衣",waistSize,height);}}TianJinClothesFactory.javapublicclassTianJinClothesFactoryextendsClothesFactory{publicUpperClothescreateUpperClothes(intchestSize,intheight){returnnewSportUpperClothes("天津牌运行上衣",chestSize,height);}publicTrouserscreateTrousers(intwaistSize,intheight){returnnewSportTrousers("天津牌运行下衣",waistSize,height);}测试类:Shop.javapublicclassShop{UpperClothescloth;Trouserstrouser;publicvoidgiveSuit(ClothesFactoryfactory,intchestSize,intwaistSize,intheight){cloth=factory.createUpperClothes(chestSize,height);trouser=factory.createTrousers(waistSize,height);showMess();}privatevoidshowMess(){System.out.println("<套装信息>");System.out.println(cloth.getName()+":");System.out.print("胸围:"+cloth.getChestSize());System.out.println("身高:"+cloth.getHeight());System.out.println(trouser.getName()+":");System.out.print("腰围:"+trouser.getWaistSize());System.out.println("身高:"+trouser.getHeight());}}Test.javapublicclassTest{publicstaticvoidmain(String[]args){Shopshop=newShop();ClothesFactoryfactory=newBeiJingClothesFactory();shop.giveSuit(factory,110,82,170);factory=newShangHaiClothesFactory();shop.giveSuit(factory,110,82,170);factory=newTianJinClothesFactory();shop.giveSuit(factory,110,82,170);运行截图如下:€套装信息〉北京牌西胆上衣:胸副11。身r^i:170北京牌西胆下衣:腰国:號身■髙:170V套装信息A上海牌牛仔上衣:胸[U:110身髙:170上海牌牛仔下衣:腰凰:酣身■髙:170€套装信息》天津牌运行上衣:胸1:170身髙:110天津牌运行下衣:腰囤:號身■髙:丄刖2.用户在银行存款后,用户将得到银行给予的存款凭证,该存款凭证就是加盖了业务公章的存款明细。不同银行的业务公章不仅名称互不相同,而且形状也互不相同,例如中国银行的业务公章是圆形,中国建设银行的业务公章是正方形,交通银行的业务公章是三角形。使用抽象工厂模式分析有哪些类以及这些类的角色,完成这些类的定义并进行测试。答:抽象产品角色:类DepositSlip,类Seal具体产品角色:类DepositSlip1,类DepositSlip2,类DepositSlip3,类SealOne,类SealTwo,类SealThree抽象工厂角色:类Bank,具体工厂角色:类ChinaBank,类ChinaConstructionBank,类BankOfCommunications抽象产品(AbstractProduct):DepositSlipjavapublicinterfaceDepositSlip{publicpublicpublicpublicabstractabstractabstractabstractStringStringStringpublicpublicpublicpublicabstractabstractabstractabstractStringStringStringgetBankName();getClientName();getClientNumber();intgetAmountOfMoney();Seal.javapublicinterfaceSeal{publicabstractImagegetImage();}具体产品(ConcreteProduct):DepositSlip1.javapublicclassDepositSlip1implementsDepositSlip{StringClientNumber;StringClientName;intmoney;DepositSlip1(StringClientNumber,StringClientName,intmoney){this.ClientName=ClientName;this.ClientNumber=ClientNumber;this.money=money;}publicStringgetBankName(){return"中国银行";}publicStringgetClientName(){returnthis.ClientName;}publicStringgetClientNumber(){returnthis.ClientNumber;}publicintgetAmountOfMoney(){returnthis.money;}}DepositSlip2.javapublicclassDepositSlip2implementsDepositSlip{StringClientNumber;StringClientName;intmoney;DepositSlip2(StringClientNumber,StringClientName,intmoney){this.ClientName=ClientName;this.ClientNumber=ClientNumber;this.money=money;}publicStringgetBankName(){return"中国建设银行";}publicStringgetClientName(){returnthis.ClientName;}publicStringgetClientNumber(){returnthis.ClientNumber;}publicintgetAmountOfMoney(){returnthis.money;}}DepositSlip3.javapublicclassDepositSlip3implementsDepositSlip{StringClientNumber;StringClientName;intmoney;DepositSlip3(StringClientNumber,StringClientName,intmoney){this.ClientName=ClientName;this.ClientNumber=ClientNumber;this.money=money;}publicStringgetBankName(){return"交通银行";}publicStringgetClientName(){returnthis.ClientName;}publicStringgetClientNumber(){returnthis.ClientNumber;}publicintgetAmountOfMoney(){returnthis.money;}}SealOne.javaBufferedImageimage;Graphics2Dg;SealOne(){image=newBufferedImage(100,100,BufferedImage.TYPE_INT_RGB);g=image.createGraphics();g.setColor(Color.white);Rectangle2Drect=newRectangle2D.Double(0,0,100,100);g.fill(rect);g.setColor(Color.red);BasicStrokebs=newBasicStroke(3f,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND);Ellipse2Dellipse=newEllipse2D.Double(5,6,80,80);g.setStroke(bs);g.draw(ellipse);g.setFont(newFont("宋体",Font.B0LD,14));g.drawString("中国银行",16,50);}publicImagegetImage(){returnimage;}}SealTwo.javapublicclassSealTwoimplementsSeal{BufferedImageimage;Graphics2Dg;SealTwo(){image=newBufferedImage(100,100,BufferedImage.TYPE_INT_RGB);g=image.createGraphics();g.setColor(Color.white);Rectangle2Drect=newRectangle2D.Double(0,0,100,100);g.fill(rect);g.setColor(Color.red);BasicStrokebs=newBasicStroke(3f,BasicStroke.CAP_SQUARE,BasicStroke.J0IN_R0UND);rect=newRectangle2D.Double(5,6,80,80);g.setStroke(bs);g.draw(rect);g.setFont(newFont("宋体",Font.B0LD,14));g.drawString("建设银行",16,50);}returnimage;}SealThree.javapublicclassSealThreeimplementsSeal{BufferedImageimage;Graphics2Dg;SealThree(){image=newBufferedImage(110,110,BufferedImage.TYPE_INT_RGB);g=image.createGraphics();g.setColor(Color.white);Rectangle2Drect=newRectangle2D.Double(0,0,110,110);g.fill(rect);g.setColor(Color.red);BasicStrokebs=newBasicStroke(3f,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND);g.setStroke(bs);Line2Dline=newLine2D.Double(5,105,55,5);g.draw(line);line.setLine(55,5,105,105);g.draw(line);line.setLine(105,105,5,105);g.draw(line);g.setFont(newFont("宋体",Font.B0LD,14));g.drawString("交通银行",25,78);}publicImagegetImage(){returnimage;}}抽象工厂(AbstractFactory):Bank.javapublicabstractclassBank{publicabstractDepositSlipcreateDepositSlip(Stringnumber,Stringname,intmoney);publicabstractSealcreateSeal();具体工厂(ConcreteFactory):ChinaBank.javapublicclassChinaBankextendsBank{publicDepositSlipcreateDepositSlip(Stringnumber,Stringname,intmoney){returnnewDepositSlip1(number,name,money);}publicSealcreateSeal(){returnnewSealOne();}}ChinaConstructionBank.javapublicclassChinaConstructionBankextendsBank{publicDepositSlipcreateDepositSlip(Stringnumber,Stringname,intmoney){returnnewDepositSlip2(number,name,money);}publicSealcreateSeal(){returnnewSealTwo();}}BankOfCommunications.javapublicclassBankOfCommunicationsextendsBank{publicDepositSlipcreateDepositSlip(Stringnumber,Stringname,intmoney){returnnewDepositSlip3(number,name,money);}publicSealcreateSeal(){returnnewSealThree();}}测试类:ShowDepositSlipjavapublicclassShowDepositSlipextendsJPanel{DepositSlipdepositSlip;Sealseal;booleanboo;JLabellabel;Imageimage;JFrameframe;ShowDepositSlip(){setLayout(null);setSize(200,200);label=newJLabel();add(label);frame=newJFrame();frame.add(this);}publicvoidshowDepositSlip(Bankbank,Stringnumber,Stringname,intmoney){depositSlip=bank.createDepositSlip(number,name,money);seal=bank.createSeal();image=seal.getImage();label.setIcon(newImageIcon(image));boo=true;frame.setSize(200,280);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.DI
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年中国水烟炭市场调查研究报告
- 2024至2030年梯柱项目投资价值分析报告
- 2024年中国丙纶中强粗旦产业用丝市场调查研究报告
- 2024至2030年手提贮压式机械泡沫灭器项目投资价值分析报告
- 2025至2031年中国矽橡胶玻纤套管行业投资前景及策略咨询研究报告
- 2025至2031年中国新康霜行业投资前景及策略咨询研究报告
- 2025至2031年中国微机控制牵引治疗机行业投资前景及策略咨询研究报告
- 2025至2031年中国复合型防火卷帘行业投资前景及策略咨询研究报告
- 2025年度版权质押合同(具体描述质押权利、质押期限等)2篇
- 2025年度委托企业员工培训项目设计与执行合同3篇
- 血气分析结果判读及临床应用护理课件
- 智能船舶与海洋工程:物联网在船舶与海洋工程中的应用
- 高速服务区经营分析报告
- 浙江省湖州市2022-2023学年四年级上学期数学期末试卷(含答案)
- 现场工艺纪律检查表
- 建井施工方案
- YMO青少年数学思维28届五年级全国总决赛试卷
- 个人业绩相关信息采集表
- 过敏性紫癜课件PPT
- 大学生暑期社会实践证明模板(20篇)
- 自来水维修员年度工作总结
评论
0/150
提交评论