实验报告6_单件模式_第1页
实验报告6_单件模式_第2页
实验报告6_单件模式_第3页
实验报告6_单件模式_第4页
实验报告6_单件模式_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、云南大学软件学院实 验 报 告序号: 姓名: 程聪 学号: 20151120082 专业: 软件工程 日期: 2017/12/7 成绩: 实验六 单件模式的运用一、实验目的:单件模式是一种用于确保整个应用程序中只有一个类实例且这个实例所占资源在整个应用程序中是共享时的程序设计方法。在熟悉单件模式相关理论知识的基础上,使用单件模式实现一款应用程序框架。二、实验要求:使用单件模式实现一款应用程序框架,要求如下:1. 使用同步getinstance()方法实现应用程序框架的运行环境组件,其中包含登录用户名、密码、级别等信息。2. 应用程序框架采用模块化设计,模块控制组件使用急切实例化方法实现。该组件

2、负责管理在用模块并处理模块间的通信。3. 为了避免模块的多次重复创建和释放,应用程序框架使用双重检查加锁的方法实现模块缓存组件。该组件缓存被释放的模块实例,当再次请求使用的时候,如果缓存了该模块,则返回该实例,否则就创建模块。4. 以上三个组件使用不同的单例实现方法,在应用程序命名空间内都能够访问使用。1、 设计并绘制该程序的类图;2、 依照设计的类图使用java语言编写代码,并实现该程序;3、 除了核心的模式相关类实现外,提供测试环境,按照难度高低,分别是:a) 控制台程序,client硬编码初始化模式和测试环境,运行结果文本输出;b) 控制台程序,client初始化测试环境,并根据用户输入

3、运算,运行结果文本输出;c) 设计并实现用户ui,client初始化测试环境,并根据用户在ui控件上的输入运算,运行结果文本输出;3、 实验内容:1. 主要类图使用同步getinstance()方法的代码public class singleton public static singleton uniqueuser;static string info1;static string info2;static int count=0;string username;public static string getinfo1() return info1;public static void s

4、etinfo1(string info1) s1 = info1;public static string getinfo2() return info2;public static void setinfo2(string info2) s2 = info2;public string getusername() return username;public void setusername(string username) this.username = username;public string getpassword() retur

5、n password;public void setpassword(string password) this.password = password;public int getprior() return prior;public void setprior(int prior) this.prior = prior;string password ;int prior;public static synchronized singleton getinstance() if (uniqueuser = null) info1=没有该单例,现在创建;uniqueuser = new si

6、ngleton();elseinfo1=; info2=已有该单例,直接传值;return uniqueuser;private singleton() count+;username = chengcong; password = 20151120082; prior = 1;public static int getcount() return count;public static void setcount(int count) singleton.count = count;急切实例化方法代码public class singleton2static int count=0;stat

7、ic string info1;public static string getinfo1() return info1;public static void setinfo1(string info1) s1 = info1;public static int getcount() return count;public static void setcount(int count) singleton2.count = count;private static singleton2 uniqueinstance = new singleton2();privat

8、e singleton2() count+;/ 在jvm加载该类时已经创建此唯一的单件实例public static singleton2 getinstance() info1=单例已创建好,始终唯一;return uniqueinstance;双重检查加锁代码public class singleton3 static string info1;static string info2;static string info3;static int count=0; public static int getcount() return count;public static void set

9、count(int count) singleton3.count = count;private volatile static singleton3 uniqueinstance = null; private singleton3() count+; info3=20151120082; public static string getinfo1() return info1;public static void setinfo1(string info1) s1 = info1;public static string getinfo2() return i

10、nfo2;public static void setinfo2(string info2) s2 = info2;public static string getinfo3() return info3;public static void setinfo3(string info3) s3 = info3;public static singleton3 getinstance() if (uniqueinstance = null) synchronized(singleton3.class) if(uniqueinstance=null) info1=没有该单例,现在创建; uniqueinstance = new singl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论