设计模式实验报告优选.

上传人:hs****ma 文档编号:563288171 上传时间:2023-11-17 格式:DOC 页数:55 大小:1.21MB
返回 下载 相关 举报
设计模式实验报告优选._第1页
第1页 / 共55页
设计模式实验报告优选._第2页
第2页 / 共55页
设计模式实验报告优选._第3页
第3页 / 共55页
设计模式实验报告优选._第4页
第4页 / 共55页
设计模式实验报告优选._第5页
第5页 / 共55页
点击查看更多>>
资源描述

《设计模式实验报告优选.》由会员分享,可在线阅读,更多相关《设计模式实验报告优选.(55页珍藏版)》请在金锄头文库上搜索。

1、楕品Word.最新文件仅供参考已换word文本方便更改赠人玫瑰,手留余香。实验一单例模式的应用1实验目的1) 掌握单例模式(Singleton )的特点2) 分析具体问题,使用单例模式进行设计。2实验内容和要求很多应用项目都有配置文件,这些配置文件里面定义一些应用需要的参数数据。AppConfig -ParameterA : string +GetParameterA() +SetParameterA()通常客户端使用这个类是通过new 个AppConfig的实例来得到一个操作 配置文件内容的对象。如果在系统运行中,有很多地方都需要使用配置文件的 内容,系统中会同时存在多份配置文件的内容,这会

2、严重浪费内存资源。事实上,对于AppConfig类,在运行期间,只需要一个对象实例就够了。 那么应该怎么实现呢?用C#控制台应用程序实现该单例模式。绘制该模式的UML 图。#/473模式结构图4 UML类图5代码Singleton-instance : Singleton-Si ngletonO十Getlnslance() : SingletonAppconfig-parameterAistringconfig: Appconfig Appco nfig() +-Getlnstance() GetParameterAQ +SetParameterA()class AppConfigprivat

3、e st at ic AppConfig config: private st ring paranie-terA;private AppConfig()public string ParapeterAget return parameterA; set parameterA. = value : public st at i c AppConf ig Get Instance() if (null = config)config = new AppConfig():return config;class Prograjnstatic void Main (string args)AppCon

4、fig appconfigl = AppConf igGetInsi:ariceC); appconf igl. Parente ter A 二ParaA.;Console. Writ eLineappconfigl:):Console. Writ eLine(appconfigl ParametarA):AppConfig appconfigZ = AppConf ig.Ge:Console. Writ eLine Cappconf ig2:):Console. Writ eLine (appconf ig2 Paraniet&rA):Console. Read.():6运行结果材匍包侶网各

5、种课程/设计.Le?I回IMappconfigl:-I_nParaAuappconfig2:ParaA.实验二工厂模式的应用1实验目的1) 掌握工厂模式(Factory )的特点2) 分析具体问题,使用工厂模式进行设计。2实验内容和要求有一个OEM制造商代理做HP笔记本电脑(Laptop),后来该制造商得到了 更多的品牌笔记本电脑的订单Acer , Lenovo , Dell ,该OEM商发现,如果一 次同时做很多个牌子的本本,有些不利于管理。利用工厂模式改善设计,用C# 控制台应用程序实现该OEM制造商的工厂模式。绘制该模式的UML图。3模式结构图ConcreteCreator Facior

6、yMelhodO : Product4 UML类图5代码class Laptopprotacted string bHand:public string Brandget return biand: set brand. = value: class HPLaptop : Lap!oppublic HPLaptop()this.btand 二HP:class AcerLap-top : Laptoppublic AcerLapt op()this .brand = Acer:class LenovoLaptop : Laptop public LenovoLaptop()this, brand

7、 = ,vLenovo ; class DellLaptop : Laptoppublic D&LILaptop()this .brand = Dell:interface LaptopFactoiryL apt op Great eLapt op():class HPLaptopFactory : LaptopFactorypublic Laptop CreateLaptop()return new HPLapt op ():class AcerLapt opFactofy : LaptopFact orypublic Lapt op Cr eat eL apt op ()return ne

8、w AcerLaptop ():class LenovoLaptopFactory : LaptopFactorypublic Laptop CresteLapt op ()return n&w LenovoLapt op():class DellLaptopFactory : LaptopFactorypublic Laptop Creat eLapt op ()return n&vr DellLapt op ():class Progranist atic void Main (st ring args)LaptopFactory HPfactory = new HPLaptopFacto

9、ry(): LaptopFactoiry Acerfact ory = new AcerLapt opFact ory (): I.-ap-t opFact ory Lenovof act ory = new LenovoLapt opFact ory (): LaptopFactoiry Dellfact ory = new DellLapt opFact ory ():Console叩rit&Lirbe (HPf act ory CreateLapt op (). Brand): Console WriteLine(Acerfactory CreateLapt op () Brand):

10、Console AVritgLirve (Lenovof actory GreateLaptod () Brand): Console WriteLine(Dellfactory CreateLapt op () Brand):ConsoleRead():6运行结果实验三抽象工厂模式的应用1实验目的1)拿握抽象工厂模式(Abstract Factory )的特点2)分析具体问题,使用抽象工厂模式进行设计。2实验内容和要求麦当劳(McDonalds )和肯德基(KFC )快餐店都经营汉堡(Hamburg )和 可乐(Cola ),用C#控制台应用程序实现这两个快餐店经营产品的抽象工厂模 式。绘制

11、该模式的UML图。3模式结构图齐壬WORD.4 UML滋圈MCDOnaEs+GreareHafnbcrg9 lHamburg +cre%ecocr?now八 Anterfacevv Noshery wKM+CreareHarrbLrgO 二 Hamburg+Creacecol30noEF楕品Word.class Hamburgprivate irrt id;private double price;public int Idget return id: set id = value: public double Priceget return price: set price = value:

12、 interface IHajaburgHannburg Get Hamburg (int id): void UpdateHamburgPrice(int id);class McDonaldsHamburg : IHam.b-urg public Hamburg GetHamburg(int id)Conso 1 e. Writ eLine C MMcDonalds买了 一个Hamburg): return null;public void UpdateHniburgPrice(int id)Console. Writ eLine (ffMcDonalds快翳店的HajriLurgP 价大

13、甩卖 了); #/47class KFCHamburg : IHamburgpublic Hamburg GetHajriburg (int id)Console. WriteLin& ( MKFC买 了 一个Haniburg):return null:public void UpdateHajriburgPrics(irrt id)Console. Wr it eLine (KFC 快霧止的 H amburgP 价大用卖了 ):class Colaprivate int id;private double price;public int Idget return id; set id = value; 1public double Priceget wturn price; set price = value: interface IColaCola GetCola(int id):void UpdateColaPr ice (int id):class NcDonaldsCola : IColapublic Cola GetCola(int id)Console. Writ eLine

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号