工厂模式单例模式案例实验报告

上传人:汽*** 文档编号:563467866 上传时间:2022-10-16 格式:DOC 页数:18 大小:720KB
返回 下载 相关 举报
工厂模式单例模式案例实验报告_第1页
第1页 / 共18页
工厂模式单例模式案例实验报告_第2页
第2页 / 共18页
工厂模式单例模式案例实验报告_第3页
第3页 / 共18页
工厂模式单例模式案例实验报告_第4页
第4页 / 共18页
工厂模式单例模式案例实验报告_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《工厂模式单例模式案例实验报告》由会员分享,可在线阅读,更多相关《工厂模式单例模式案例实验报告(18页珍藏版)》请在金锄头文库上搜索。

1、软件设计与体系结构实验报告软件设计与体系0920116课程名称课程编号结构实验项目名称工厂模式,单例模式案例学号班级姓名专业学生所在学院指导教师实验室名称地点实验时间实验名称:工厂模式,单例模式模式案例实验目的: 工厂单例模式 (Observer Pattern) 是设计模式中行为模式的一种,它解决了上述具有一对多依赖关系的对象的重用问题。此模式的参与者分为两大类,一类是被观察的目标,另一类是观察该目标的观察者们。正因为该模式是基于“一对多”的关系,所以该模式一般是应用于由一个目标对象和 N 个观察者对象组成(当然也可以扩展为有多个目标对象,但我们现在只讨论前者)的场合。当目标对象的状态发生改

2、变或做出某种行为时,正在观察该目标对象的观察者们将自动地、连锁地作出相应的响应行为。通过本次实验了解观察者模式的原理。并能够运用观察者模式来进行编程。实验内容1 UML类图工厂模式手机工厂MobileMobileFactoryNokiaMotorolaMotorolaFatoryNokiaFactory工厂模式电脑产品单例模式:2 程序的源代码工厂模式手机工厂public interface Mobilepublic void call();public interface MobileFactorypublic Mobile produceMobile();public class Moto

3、rola implements Mobilepublic void call() 摩托罗拉手机 );public class Nokia implements Mobilepublic void call() 诺基亚手机 );public class MotorolaFactory implements MobileFactorypublic Mobile produceMobile() 摩托罗拉工厂制造了 );return new Motorola();public class NokiaFactory implements MobileFactorypublic Mobile produc

4、eMobile() 诺基亚工厂制造了 ); return new Nokia();public class Clientpublic static void main(String argv)MobileFactory mf;Mobile m;mf=new MotorolaFactory();m=();();mf=new NokiaFactory();m=();();工厂模式电脑产品public interface CPUpublic String getCPU();public class AMD implements CPUpublic String getCPU()return Athl

5、on XP 2008+;public class Intel implements CPUpublic String getCPU()return 奔腾 4 3.2C;public interface HardDiskpublic String getSize();public class Maxtor implements HardDiskpublic String getSize()return MaXLine Plus II 200G;public class WestDigit implements HardDiskpublic String getSize()return WD250

6、0JD 250G;public interface MainBoardpublic void Attach(CPU cpu) throws Exception;public class MSI865PE implements MainBoardpublic void Attach(CPU cpu) throws Exceptionif ().toString ().endsWith(Intel)MSI865PE);elsethrow new Exception(主板 MSI865PE只能配 Intel的CPU);public class MSIK7N2G implements MainBoar

7、dpublic void Attach(CPU cpu) throws Exceptionif ().toString ().endsWith(AMD)MSIK7N2G);elsethrow new Exception(主板 MSIK7N2G只能配 AMD的CPU);public abstract class ComputerFactoryCPU cpu;HardDisk hd;MainBoard mb;public void show()try.toString () + (生产的电脑配置 );(CPU: + ();(HardDisk: + ();(MainBoard:);(cpu);cat

8、ch(Exception e)public class IBM extends ComputerFactoryIBM()cpu = new Intel();hd = new WestDigit();mb = new MSI865PE();public class Dell extends ComputerFactoryDell()cpu = new AMD();hd = new Maxtor();mb = new MSIK7N2G();public class Clientpublic static void main(String argv)IBM ibm = new IBM();();De

9、ll dell = new Dell();();单例模式:package Singleton;public class AgentMazeFactory extends MazeFactorypublic AgentMazeFactory()a AgentMazeFactory has been created);package Singleton;public class EnchantedMazeFactory extends MazeFactorypublic EnchantedMazeFactory ()a EnchantedMazeFactory has been created);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

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