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

下载本文档

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

文档简介

软件设计与体系结构实 验 报 告课程名称软件设计与体系结构课程编号0920116实验项目名称 代理模式,适配器模式,远程方法调用案例学号班级姓名专业学生所在学院指导教师实验室名称地点实验时间 实验名称: 代理模式,适配器模式,远程方法调用案例实验目的:代理模式,适配器模式,远程方法调用案例(Observer Pattern)是设计模式中行为模式的一种,它解决了上述具有一对多依赖关系的对象的重用问题。此模式的参与者分为两大类,一类是被观察的目标,另一类是观察该目标的观察者们。正因为该模式是基于“一对多”的关系,所以该模式一般是应用于由一个目标对象和N个观察者对象组成(当然也可以扩展为有多个目标对象,但我们现在只讨论前者)的场合。当目标对象的状态发生改变或做出某种行为时,正在观察该目标对象的观察者们将自动地、连锁地作出相应的响应行为。 通过本次实验了解观察者模式的原理。并能够运用观察者模式来进行编程。实验内容1 UML类图代理模式-追女友实例:适配器模式-清洁系统:远程方法调用:2 程序的源代码代理模式-追女友实例:package Gift;public interface GiveGift public void GiveDolls();public void 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;Overridepublic void GiveDolls() / TODO Auto-generated method stubgg.GiveDolls();Overridepublic void GiveFlowers() / TODO Auto-generated method stubgg.GiveFlowers();Overridepublic void GiveChocolate() / TODO Auto-generated method stubgg.GiveChocolate();package Gift;public class Pursuit implements GiveGift private SchoolGirl mm;private String name;public Pursuit(SchoolGirl mm, String name) super();this.mm = mm; = name;public SchoolGirl getMm() return mm;public void setMm(SchoolGirl mm) this.mm = mm;Overridepublic void GiveDolls() / TODO Auto-generated method stubSystem.out.println(mm.getName() + + 这个洋娃娃是给你的,你亲爱的 + + name);Overridepublic void GiveFlowers() / TODO Auto-generated method stubSystem.out.println(mm.getName() + + 这束鲜花是给你的,你亲爱的 + + name);Overridepublic void GiveChocolate() / TODO Auto-generated method stubSystem.out.println(mm.getName() + + 这块巧克力是给你的,你亲爱的 + + name);package Gift;public class SchoolGirl private String name;public SchoolGirl(String name) super(); = 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 stubSchoolGirl SS = new SchoolGirl(小笼包);Pursuit p = new Pursuit(SS, 过儿);Proxy 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() System.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).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 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 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.double 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.digits = 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 engine;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() 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 = 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

提交评论