《HeadFirst设计模式(第二版)》第十一章代码——代理模式

news/2025/3/26 6:58:48/
代码文件目录:

 RMI:
MyRemote
package Chapter11_ProxyPattern.RMI;import java.rmi.Remote;
import java.rmi.RemoteException;public interface MyRemote extends Remote {public String sayHello() throws RemoteException;
}
MyRemoteClient
package Chapter11_ProxyPattern.RMI;import java.rmi.Naming;public class MyRemoteClient {public static void main(String[] args) {new MyRemoteClient().go();}public void go(){try{MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/RemoteHello");String s = service.sayHello();System.out.println(s);}catch (Exception ex){ex.printStackTrace();}}
}
MyRemoteImpl
package Chapter11_ProxyPattern.RMI;import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {private static final long serialVersion = 1L;public String sayHello() throws RemoteException {return "Server says: Hey!";}public MyRemoteImpl()throws RemoteException{}public static void main(String[] args) {try{MyRemote service = new MyRemoteImpl();Naming.rebind("RemoteHello",service);}catch (Exception ex){ex.printStackTrace();}}
}
能够远程监控的糖果机:

在上一章的代码的基础上做一些修改

GumballMachine
public class GumballMachineextends UnicastRemoteObject implements GumballMachineRemote{private static final long serialVersionUID = 2L;//加上地理位置支持String location;State soldOutState;State noQuarterState;State hasQuarterState;State soldState;State winnerState;State state = soldOutState;int count = 0;public GumballMachine(String location,int numberGumballs) throws RemoteException {soldOutState = new SoldOutState(this);noQuarterState = new NoQuarterState(this);hasQuarterState = new HasQuarterState(this);soldState = new SoldState(this);winnerState = new WinnerState(this);this.location = location;this.count = numberGumballs;if (numberGumballs > 0) {state = noQuarterState;}}
GumballMachineRemote
package Chapter11_ProxyPattern.Origin;import java.rmi.Remote;
import java.rmi.RemoteException;public interface GumballMachineRemote extends Remote {public int getCount() throws RemoteException;public String getLocation() throws RemoteException;public State getState() throws RemoteException;
}
GumballMachineTestDrive
package Chapter11_ProxyPattern.Origin;import java.rmi.Naming;/*** @Author 竹心* @Date 2023/8/19**/public class GumballMachineTestDrive {public static void main(String[] args) {GumballMachineRemote gumballMachine = null;int count;if (args.length < 2) {System.out.println("GumballMachine <name> <inventory>");System.exit(1);}try {count = Integer.parseInt(args[1]);gumballMachine =new GumballMachine(args[0], count);Naming.rebind("//" + args[0] + "/gumballmachine", gumballMachine);} catch (Exception e) {e.printStackTrace();}}
}
GumballMonitor
package Chapter11_ProxyPattern.Origin;import java.rmi.RemoteException;/*** @Author 竹心* @Date 2023/8/20**///糖果监视器
public class GumballMonitor {GumballMachineRemote gumballMachine;public GumballMonitor(GumballMachineRemote machine){this.gumballMachine = machine;}public void report(){try{System.out.println("Gumball Machine: "+this.gumballMachine.getLocation());System.out.println("Current inventory: "+this.gumballMachine.getCount()+" gumballs");System.out.println("Current State: "+this.gumballMachine.getState());}catch (RemoteException e){e.printStackTrace();}}
}
GumballMonitorTestDrive
package Chapter11_ProxyPattern.Origin;import java.rmi.Naming;public class GumballMonitorTestDrive {public static void main(String[] args) {String[] location = {"rmi://127.0.0.1/gumballmachine","rmi://127.0.0.1/gumballmachine","rmi://127.0.0.1/gumballmachine"};if (args.length >= 0){location = new String[1];location[0] = "rmi://" + args[0] + "/gumballmachine";}GumballMonitor[] monitor = new GumballMonitor[location.length];for (int i=0;i < location.length; i++) {try {GumballMachineRemote machine =(GumballMachineRemote) Naming.lookup(location[i]);monitor[i] = new GumballMonitor(machine);System.out.println(monitor[i]);} catch (Exception e) {e.printStackTrace();}}for (int i=0; i < monitor.length; i++) {monitor[i].report();}}
}
五个状态类:

同样的修改:

public class HasQuarterState implements State {private static final long serialVersionUID = 2L;Random randomWinner = new Random(System.currentTimeMillis());


http://www.ppmy.cn/news/1048285.html

相关文章

学习心得03:OpenCV

数学真是不可思议&#xff0c;不管什么东西&#xff0c;都能用数学来处理。OpenCV以前也接触过&#xff0c;这次是系统学习一下。 颜色模型 RGB&#xff0c;YUV&#xff0c;HSV&#xff0c;Lab&#xff0c;GRAY 颜色转换cvtColor()/convertTo()&#xff0c;通道分离split()&…

基于YOLOv5n/s/m不同参数量级模型开发构建茶叶嫩芽检测识别模型,使用pruning剪枝技术来对模型进行轻量化处理,探索不同剪枝水平下模型性能影响

今天有点时间就想着之前遗留的一个问题正好拿过来做一下看看&#xff0c;主要的目的就是想要对训练好的目标检测模型进行剪枝处理&#xff0c;这里就以茶叶嫩芽检测数据场景为例了&#xff0c;在我前面的博文中已经有过相关的实践介绍了&#xff0c;感兴趣的话可以自行移步阅读…

消息中间件篇

消息中间件篇 RabbitMQ 如何保证消息不丢失 面试官&#xff1a; RabbitMQ如何保证消息不丢失 候选人&#xff1a; 嗯&#xff01;我们当时MYSQL和Redis的数据双写一致性就是采用RabbitMQ实现同步的&#xff0c;这里面就要求了消息的高可用性&#xff0c;我们要保证消息的不…

快速连接服务器脚本 可从多个服务中选择并连接

使用 python 做一个可选择服务器登录连接的脚本 前置条件 需要有python 环境python --version 显示版本号即可检查 python 是否有 paramiko 包没有的话 python install paramiko创建一个python 文件,内容如下 # -*- coding: utf-8 -*-""" Authors: huxiaohua…

【腾讯云Cloud Studio实战训练营】用Vue+Vite快速构建完成交互式3D小故事

&#x1f440;前置了解&#xff1a;(官网 https://cloudstudio.net/) 什么是Cloud Studio&#xff1f; Cloud Studio 是基于浏览器的集成式开发环境&#xff08;IDE&#xff09;&#xff0c;为开发者提供了一个永不间断的云端工作站。用户在使用 Cloud Studio 时无需安装&#…

Linux面试笔试题(1)

1、以长格式列目录时&#xff0c;若文件test的权限描述为&#xff1a;drwxrw-r–&#xff0c;则文件test的类型及文件主的权限是__A____。 A.目录文件、读写执行 B.目录文件、读写 C.普通文件、读写 D.普通文件、读 在这个问题中&#xff0c;我们需要解析文件权限的描述&…

c#实现依赖注入

当谈到C#中的依赖注入(Dependency Injection,DI)时,我们可以使用一个简单的示例来说明它是如何工作的。依赖注入是一种设计模式,用于将依赖关系从一个类传递到另一个类,以实现松耦合和可测试性。 假设我们有一个简单的订单处理应用程序,其中包含两个主要类:OrderServi…

ModaHub魔搭社区:如何用Milvus Cloud和 LangChain手搓一个有“人格”的智能客服机器人?(二)

目录 6. 首次咨询问答 7. " 温度 " 8. " 监控保护 " 六、参考信息 6. 首次咨询问答 一个引导语的配置,可以规范机器人在接待时与客户打招呼的内容,交给客户自行设定即可。当然,要和自己的人设尽量保持一致。 以上内容的组合,通过产品界面配置,生…