代理模式,适配器模式,远程方法调用案例实验报告_第1页
代理模式,适配器模式,远程方法调用案例实验报告_第2页
代理模式,适配器模式,远程方法调用案例实验报告_第3页
代理模式,适配器模式,远程方法调用案例实验报告_第4页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

1、代理模式,适配器模式,远程方法调用案例实验报告 软件设计与体系结构 实 实 验 报 告 课程名称 软件设计与体系结构 课程编号 0920216 实验项目名称 代理模式,适配器模式,远程方法调用案例 学号 班级 姓名 专业 学生所在学院 指导教师 实验室名称地点 实验时间 实验名称: 代理模式,适配器模式,远程方法调用案例 实验目的:代理模式,适配器模式,远程方法调用案例(observer pattern)是设计模式中行为模式的一种,它解决了上述具有一对多依赖关系的对象的重用问题。此模式的参与者分为两大类,一类是被观察的目标,另一类是观察该目标的观察者们。正因为该模式是基于"一对多&#

2、39;的关系,所以该模式一般是应用于由一个目标对象和 n 个观察者对象组成(当然也可以扩展为有多个目标对象,但我们现在只讨论前者)的场合。当目标对象的状态发生改变或做出某种行为时,正在观察该目标对象的观察者们将自动地、连锁地作出相应的响应行为。 通过本次实验了解观察者模式的原理。并能够运用观察者模式来进行编程。 实验内容 1 uml类图 代理模式-追女友实例: 适配器模式-清洁系统: 远程方法调用: 2 程序的源代码 代理模式-追女友实例: package gift; public interface givegift public void givedolls(); public void

3、giveflowers(); public void givechocolate(); package gift; public class proxy implements givegift pursuit gg; public proxy(pursuit g,schoolgirl mm) super(); g.setmm(mm); this.gg = g; override public void givedolls() / todo auto-generated method stub gg.givedolls(); override public void giveflowers()

4、/ todo auto-generated method stub gg.giveflowers(); override public void givechocolate() / todo auto-generated method stub gg.givechocolate(); package gift; public class pursuit implements givegift private schoolgirl mm; private string name; public pursuit(schoolgirl mm, string name) super(); this.m

5、m = mm; = name; public schoolgirl getmm() return mm; public void setmm(schoolgirl mm) this.mm = mm; override public void givedolls() / todo auto-generated method stub system.out.println(mm.getname() + + 这个洋娃娃是给你的,你亲爱的 + + name); override public void giveflowers() / todo auto-generated meth

6、od stub system.out.println(mm.getname() + + 这束鲜花是给你的,你亲爱的 + + name); override public void givechocolate() / todo auto-generated method stub system.out.println(mm.getname() + + 这块巧克力是给你的,你亲爱的 + + name); package gift; public class schoolgirl private string name; public schoolgirl(string name) super();

7、 = name; public string getname() return name; public void setname(string name) = name; package gift; public class test /* * param args */ public static void main(string args) / todo auto-generated method stub schoolgirl ss = new schoolgirl(小笼包); pursuit p = new pursuit(ss, 过儿); p

8、roxy pxy = new proxy(p, ss); pxy.givechocolate(); pxy.givedolls(); pxy.giveflowers(); 适配器模式-清洁系统: public interface clean public void makeclean(); public class office implements clean public void makeclean() system.out.println(清洁办公室); public class workshop implements clean public void makeclean() sys

9、tem.out.println(清洁工作室); public interface extra extends clean public void takecare(); class facility implements extra public void takecare() system.out.println(养护照料); public void makeclean () system.out.println(清洁设备); public class client static void jobs(clean job) if(job instanceof clean) (clean)job

10、).makeclean(); if(job instanceof extra) (extra)job).takecare(); public static void main(string args) extra e = new facility(); jobs(e); clean c1 = new office(); clean c2 = new workshop(); jobs(c1); jobs(c2); 远程方法调用: package client; import java.rmi.*; import java.math.*; import compute.*; /* * client

11、 that asks the generic compute server to compute pi. the first * command-line argument is the server hostname. the second command-line * argument is the number of required digits after the decimal point for the * computation. */ public class computepi public static void main(string args) / install a

12、 security manager! try /string name = / + args0 + /compute; string name = rmi:/localhost:3332/compute; / get a reference to the remote object from the registry. compute comp = (compute) naming.lookup(name); / create a task object. pi task = new pi(0); / ask the server to perform the computation. dou

13、ble pi = (double) (comp.executetask(task); system.out.println(pi); catch (exception e) system.err.println(computepi exception: + e.getmessage(); e.printstacktrace(); package client; import compute.*; import java.math.*; public class pi implements task private int digits; public pi(int digits) this.d

14、igits = digits; package compute; import java.rmi.*; /* * generic compute interface. */ public interface compute extends remote double executetask(task t) throws remoteexception; package compute; import java.io.serializable; /* * task interface. */ public interface task extends serializable package e

15、ngine; import java.math.bigdecimal; import java.rmi.*; import java.rmi.registry.locateregistry; import java.rmi.server.*; import compute.*; /* * server that executes a task specified in a task object. */ public class computeengine extends unicastremoteobject implements compute public computeengine()

16、 throws remoteexception super(); double pi; public double executetask(task t) pi=3.1415926; return pi; public static void main(string args) / install a security manager! / create the remote object. / register the remote object as compute. string name = rmi:/localhost:3332/compute; try compute engine

17、 = new computeengine(); locateregistry.createregistry(3332); naming.rebind(name, engine); system.out.println(computeengine bound); catch (exception e) system.err.println(computeengine exception: + e.getmessage(); e.printstacktrace(); 3实验截图 代理模式-追女友实例: 适配器模式-清洁系统: 远程方法调用: 对该模式的认识 经过本次代理远程方法调用和适配器模式的实验,通过自己动手编代码,是自己理解代理远程方法调用和适配器模式机制,并且知道代理远程方法调用和适配器模式有以下的优点:代理模式适用性:在需要更高级的对象引

温馨提示

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

评论

0/150

提交评论