当前位置: 首页 > news >正文

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

代码文件目录:

 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());

相关文章:

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

代码文件目录&#xff1a; RMI&#xff1a; MyRemote package Chapter11_ProxyPattern.RMI;import java.rmi.Remote; import java.rmi.RemoteException;public interface MyRemote extends Remote {public String sayHello() throws RemoteException; }MyRemoteClient packa…...

QT的工程文件认识

目录 1、QT介绍 2、QT的特点 3、QT模块 3.1基本模块 3.2扩展模块 4、QT工程创建 1.选择应用的窗体格式 2.设置工程的名称与路径 3.设置类名 4.选择编译器 5、QT 工程解析 xxx.pro 工程配置 xxx.h 头文件 main.cpp 主函数 xxx.cpp 文件 6、纯手工创建一个QT 工程…...

typeScript安装及TypeScript tsc 不是内部或外部命令,也不是可运行的程序或批处理文件解决办法

一、typeScript安装&#xff1a; 1、首先确定系统中已安装node, winr 输入cmd 打开命令行&#xff0c;得到版本号证明系统中已经安装node node -v //v18.17.0 2、使用npm 全局安装typeScript # 全局安装 TypeScript npm i -g typescript 二、检查是否安装成功ts #检查t…...

SWUST 派森练习题:P111. 摩斯密码翻译器

描述 摩斯密码&#xff08;morse code)&#xff0c;又称摩斯电码、摩尔斯电码&#xff08;莫尔斯电码&#xff09;&#xff0c;是一种时通时断的信号代码&#xff0c;通过不同的信号排列顺序来表达不同的英文字母、数字和标点符号&#xff1b;通信时&#xff0c;将英文字母等内…...

如何在控制台查看excel内容

背景 最近发现打开电脑的excel很慢&#xff0c;而且使用到的场景很少&#xff0c;也因为mac自带了预览的功能。但是shigen就是闲不住&#xff0c;想自己搞一个excel预览软件&#xff0c;于是在一番技术选型之后&#xff0c;我决定使用python在控制台显示excel的内容。 具体的需…...

Echarts、js编写“中国主要城市空气质量对比”散点图 【亲测】

本次实验通过可视化工具Echarts来对全国主要城市的&#xff30;&#xff2d;2.5的值进行直观的展示&#xff0c;使人们可以快速的发现信息的关键点&#xff0c;从而对各个城市的空气质量情况有直观的了解。 先看效果 上代码&#xff1a; <!DOCTYPE html> <html>&…...

linux不分区直接在文件系统根上开swap

root下&#xff0c;直接创swapfile dd if/dev/zero of/swapfile bs1M count8192然后 mkswap swapfile swapon swapfile修改fstab # /etc/fstab: static file system information. # # Use blkid to print the universally unique identifier for a # device; this may be us…...

React请求机制优化思路 | 京东云技术团队

说起数据加载的机制&#xff0c;有一个绕不开的话题就是前端性能&#xff0c;很多电商门户的首页其实都会做一些垂直的定制优化&#xff0c;比如让请求在页面最早加载&#xff0c;或者在前一个页面就进行预加载等等。随着react18的发布&#xff0c;请求机制这一块也是被不断谈起…...

CompletableFuture总结和实践

CompletableFuture被设计在Java中进行异步编程。异步编程意味着在主线程之外创建一个独立的线程&#xff0c;与主线程分隔开&#xff0c;并在上面运行一个非阻塞的任务&#xff0c;然后通知主线程进展&#xff0c;成功或者失败。 一、概述 1.CompletableFuture和Future的区别&…...

使用Nginx调用网关,然后网关调用其他微服务

问题前提&#xff1a;目前我的项目是已经搭建了网关根据访问路径路由到微服务&#xff0c;然后现在我使用了Nginx将静态资源都放在了Nginx中&#xff0c;然后我后端定义了一个接口访问一个html页面&#xff0c;但是html页面要用到静态资源&#xff0c;这个静态资源在我的后端是…...

windows搭建WebDAV服务,并内网穿透公网访问【无公网IP】

windows搭建WebDAV服务&#xff0c;并内网穿透公网访问【无公网IP】 文章目录 windows搭建WebDAV服务&#xff0c;并内网穿透公网访问【无公网IP】1. 安装IIS必要WebDav组件2. 客户端测试3. cpolar内网穿透3.1 打开Web-UI管理界面3.2 创建隧道3.3 查看在线隧道列表3.4 浏览器访…...

PAT 1097 Deduplication on a Linked List

个人学习记录&#xff0c;代码难免不尽人意 Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value o…...

Flink 数据集成服务在小红书的降本增效实践

摘要&#xff1a;本文整理自实时引擎研发工程师袁奎&#xff0c;在 Flink Forward Asia 2022 数据集成专场的分享。本篇内容主要分为四个部分&#xff1a; 小红书实时服务降本增效背景Flink 与在离线混部实践实践过程中遇到的问题及解决方案未来展望 点击查看原文视频 & 演…...

jellyfin使用ipv6+DDNS实现外网访问

前言 原本使用frp的方案进行外网访问jellyfin&#xff0c;但是阿里云的轻量服务器的带宽只有5M&#xff0c;只能支持看1080p的视频&#xff0c;看4K有点吃力&#xff0c;为了有更好的观影体验&#xff0c;选择ipv6DDNS的方式实现外网访问&#xff0c;此方案能跑满群晖的上行带宽…...

Codeforces EDU 151 Div.2

文章目录 A. Forbidden IntegerB. Come TogetherC. Strong PasswordD. Rating SystemE. Boxes and Balls A. Forbidden Integer Problem - A - Codeforces 给定整数n&#xff0c;从1~k中选择除了x的数&#xff0c;使这些数之和为n&#xff0c;每个数可以选择无限次 爆搜&…...

V2board缓存投毒漏洞复现

1.什么是缓存投毒 缓存投毒&#xff08;Cache poisoning&#xff09;&#xff0c;通常也称为域名系统投毒&#xff08;domain name system poisoning&#xff09;&#xff0c;或DNS缓存投毒&#xff08;DNS cache poisoning&#xff09;。它是利用虚假Internet地址替换掉域名系…...

2023面试八股文 ——Java基础知识

Java基础知识 一.Java概述何为编程什么是Javajdk1.5之后的三大版本JVM、JRE和JDK的关系什么是跨平台性&#xff1f;原理是什么Java语言有哪些特点什么是字节码&#xff1f;采用字节码的大好处是什么什么是Java程序的主类&#xff1f;应用程序和小程序的主类有何不同&#xff1f…...

在linux系统中修改mysql数据目录

目录 1.查看mysql默认存储路径2.停止mysql服务3.移动或复制原数据目录4.修改配置文件5.修改启动文件6.配置AppArmor访问控制规则7.重启apparmor服务8.启动mysql 1.查看mysql默认存储路径 在/etc/mysql/mysql.conf.d/mysqld.cnf中的datadir配置项。 datadir /var/lib/mysql2…...

ORB-SLAM2学习笔记9之图像帧Frame

先占坑&#xff0c;明天再完善… 文章目录 0 引言1 Frame类1.1 成员函数1.2 成员变量 2 Frame类的用途 0 引言 ORB-SLAM2学习笔记8详细了解了图像特征点提取和描述子的生成&#xff0c;本文在此基础上&#xff0c;继续学习ORB-SLAM2中的图像帧&#xff0c;也就是Frame类&#…...

面试热题(不同的二分搜索树)

给你一个整数 n &#xff0c;求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种&#xff1f;返回满足题意的二叉搜索树的种数。 经典的面试题&#xff0c;这部分涉及了组合数学中的卡特兰数&#xff0c;如果对其不清楚的同学可以去看我以前的博客卡特兰数 …...

利用最小二乘法找圆心和半径

#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...

Spark 之 入门讲解详细版(1)

1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室&#xff08;Algorithms, Machines, and People Lab&#xff09;开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目&#xff0c;8个月后成为Apache顶级项目&#xff0c;速度之快足见过人之处&…...

DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径

目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...

MODBUS TCP转CANopen 技术赋能高效协同作业

在现代工业自动化领域&#xff0c;MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步&#xff0c;这两种通讯协议也正在被逐步融合&#xff0c;形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...

Python爬虫(一):爬虫伪装

一、网站防爬机制概述 在当今互联网环境中&#xff0c;具有一定规模或盈利性质的网站几乎都实施了各种防爬措施。这些措施主要分为两大类&#xff1a; 身份验证机制&#xff1a;直接将未经授权的爬虫阻挡在外反爬技术体系&#xff1a;通过各种技术手段增加爬虫获取数据的难度…...

汇编常见指令

汇编常见指令 一、数据传送指令 指令功能示例说明MOV数据传送MOV EAX, 10将立即数 10 送入 EAXMOV [EBX], EAX将 EAX 值存入 EBX 指向的内存LEA加载有效地址LEA EAX, [EBX4]将 EBX4 的地址存入 EAX&#xff08;不访问内存&#xff09;XCHG交换数据XCHG EAX, EBX交换 EAX 和 EB…...

uniapp中使用aixos 报错

问题&#xff1a; 在uniapp中使用aixos&#xff0c;运行后报如下错误&#xff1a; AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...

JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案

JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停​​ 1. ​​安全点(Safepoint)阻塞​​ ​​现象​​:JVM暂停但无GC日志,日志显示No GCs detected。​​原因​​:JVM等待所有线程进入安全点(如…...

听写流程自动化实践,轻量级教育辅助

随着智能教育工具的发展&#xff0c;越来越多的传统学习方式正在被数字化、自动化所优化。听写作为语文、英语等学科中重要的基础训练形式&#xff0c;也迎来了更高效的解决方案。 这是一款轻量但功能强大的听写辅助工具。它是基于本地词库与可选在线语音引擎构建&#xff0c;…...

RSS 2025|从说明书学习复杂机器人操作任务:NUS邵林团队提出全新机器人装配技能学习框架Manual2Skill

视觉语言模型&#xff08;Vision-Language Models, VLMs&#xff09;&#xff0c;为真实环境中的机器人操作任务提供了极具潜力的解决方案。 尽管 VLMs 取得了显著进展&#xff0c;机器人仍难以胜任复杂的长时程任务&#xff08;如家具装配&#xff09;&#xff0c;主要受限于人…...