實(shí)驗(yàn)十:抽象工廠模式_第1頁
實(shí)驗(yàn)十:抽象工廠模式_第2頁
實(shí)驗(yàn)十:抽象工廠模式_第3頁
實(shí)驗(yàn)十:抽象工廠模式_第4頁
實(shí)驗(yàn)十:抽象工廠模式_第5頁
已閱讀5頁,還剩10頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

實(shí)驗(yàn)10:抽象工廠模式一、實(shí)驗(yàn)?zāi)康谋緦?shí)驗(yàn)要求學(xué)生掌握抽象工廠模式,要求學(xué)生理解抽象工廠模式中的四種角色并完成類的定義及測(cè)試。二、實(shí)驗(yàn)任務(wù)1.服裝工廠能夠生成不同類型的配套服裝,但用戶不知道這些配套的服裝是如何被創(chuàng)建出來的。北京服裝廠用來生成西服套裝,上海服裝廠用來生產(chǎn)牛仔套裝,天津服裝廠用來生產(chǎn)運(yùn)動(dòng)套裝,在不修改原代碼的基礎(chǔ)上可以靈活地添加生產(chǎn)新的配套服裝類型。使用抽象工廠模式分析有哪些類以及這些類的角色,完成這些類的定義并進(jìn)行測(cè)試。答:抽象產(chǎn)品角色:類UpperClothes,類Trousers具體產(chǎn)品角色:類WesternUpperClothes,類CowboyUpperClothes,類SportUpperClothes,類CowboyTrousers,類WesternTrousers,類SportTrousers抽象工廠角色:類ClothesFactory,具體工廠角色:類BeiJingClothesFactory,類ShangHaiClothesFactory,類TianJinClothesFactory抽象產(chǎn)品(AbstractProduct):UpperClothes.javapublicabstractclassUpperClothes{publicabstractintgetChestSize();publicabstractintgetHeight();publicabstractStringgetName();}Trousers.javapublicabstractclassTrousers{publicabstractintgetWaistSize();publicabstractintgetHeight();publicabstractStringgetName();}具體產(chǎn)品(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("天津牌運(yùn)行上衣",chestSize,height);}publicTrouserscreateTrousers(intwaistSize,intheight){returnnewSportTrousers("天津牌運(yùn)行下衣",waistSize,height);}測(cè)試類: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);運(yùn)行截圖如下:€套裝信息〉北京牌西膽上衣:胸副11。身r^i:170北京牌西膽下衣:腰國(guó):號(hào)身■髙:170V套裝信息A上海牌牛仔上衣:胸[U:110身髙:170上海牌牛仔下衣:腰凰:酣身■髙:170€套裝信息》天津牌運(yùn)行上衣:胸1:170身髙:110天津牌運(yùn)行下衣:腰囤:號(hào)身■髙:丄刖2.用戶在銀行存款后,用戶將得到銀行給予的存款憑證,該存款憑證就是加蓋了業(yè)務(wù)公章的存款明細(xì)。不同銀行的業(yè)務(wù)公章不僅名稱互不相同,而且形狀也互不相同,例如中國(guó)銀行的業(yè)務(wù)公章是圓形,中國(guó)建設(shè)銀行的業(yè)務(wù)公章是正方形,交通銀行的業(yè)務(wù)公章是三角形。使用抽象工廠模式分析有哪些類以及這些類的角色,完成這些類的定義并進(jìn)行測(cè)試。答:抽象產(chǎn)品角色:類DepositSlip,類Seal具體產(chǎn)品角色:類DepositSlip1,類DepositSlip2,類DepositSlip3,類SealOne,類SealTwo,類SealThree抽象工廠角色:類Bank,具體工廠角色:類ChinaBank,類ChinaConstructionBank,類BankOfCommunications抽象產(chǎn)品(AbstractProduct):DepositSlipjavapublicinterfaceDepositSlip{publicpublicpublicpublicabstractabstractabstractabstractStringStringStringpublicpublicpublicpublicabstractabstractabstractabstractStringStringStringgetBankName();getClientName();getClientNumber();intgetAmountOfMoney();Seal.javapublicinterfaceSeal{publicabstractImagegetImage();}具體產(chǎn)品(ConcreteProduct):DepositSlip1.javapublicclassDepositSlip1implementsDepositSlip{StringClientNumber;StringClientName;intmoney;DepositSlip1(StringClientNumber,StringClientName,intmoney){this.ClientName=ClientName;this.ClientNumber=ClientNumber;this.money=money;}publicStringgetBankName(){return"中國(guó)銀行";}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"中國(guó)建設(shè)銀行";}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("中國(guó)銀行",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("建設(shè)銀行",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();}}測(cè)試類: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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論