代理模式,适配器模式,远程方法调用案例实验报告
软件设计与体系结构
实 实
验
报
告 课程名称 软件设计与体系结构 课程编号 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;
}
@Override
public void GiveDolls(){
// TODO Auto-generated method stub
gg.GiveDolls();
}
@Override
public void GiveFlowers(){
// 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.mm = mm;
this.name = 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 method 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();
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.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, "过儿");
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 = "//" + args[0] + "/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.大文斗范文网的资料来自互联网以及用户的投稿,用于非商业性学习目的免费阅览。
2.《代理模式,适配器模式,远程方法调用案例实验报告》一文的著作权归原作者所有,仅供学习参考,转载或引用时请保留版权信息。
3.如果本网所转载内容不慎侵犯了您的权益,请联系我们,我们将会及时删除。
