当前位置: 首页 > 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;如果对其不清楚的同学可以去看我以前的博客卡特兰数 …...

Leetcode 3576. Transform Array to All Equal Elements

Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接&#xff1a;3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到&#xf…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例

文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...

在Ubuntu中设置开机自动运行(sudo)指令的指南

在Ubuntu系统中&#xff0c;有时需要在系统启动时自动执行某些命令&#xff0c;特别是需要 sudo权限的指令。为了实现这一功能&#xff0c;可以使用多种方法&#xff0c;包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法&#xff0c;并提供…...

DBAPI如何优雅的获取单条数据

API如何优雅的获取单条数据 案例一 对于查询类API&#xff0c;查询的是单条数据&#xff0c;比如根据主键ID查询用户信息&#xff0c;sql如下&#xff1a; select id, name, age from user where id #{id}API默认返回的数据格式是多条的&#xff0c;如下&#xff1a; {&qu…...

成都鼎讯硬核科技!雷达目标与干扰模拟器,以卓越性能制胜电磁频谱战

在现代战争中&#xff0c;电磁频谱已成为继陆、海、空、天之后的 “第五维战场”&#xff0c;雷达作为电磁频谱领域的关键装备&#xff0c;其干扰与抗干扰能力的较量&#xff0c;直接影响着战争的胜负走向。由成都鼎讯科技匠心打造的雷达目标与干扰模拟器&#xff0c;凭借数字射…...

如何在网页里填写 PDF 表格?

有时候&#xff0c;你可能希望用户能在你的网站上填写 PDF 表单。然而&#xff0c;这件事并不简单&#xff0c;因为 PDF 并不是一种原生的网页格式。虽然浏览器可以显示 PDF 文件&#xff0c;但原生并不支持编辑或填写它们。更糟的是&#xff0c;如果你想收集表单数据&#xff…...

云原生玩法三问:构建自定义开发环境

云原生玩法三问&#xff1a;构建自定义开发环境 引言 临时运维一个古董项目&#xff0c;无文档&#xff0c;无环境&#xff0c;无交接人&#xff0c;俗称三无。 运行设备的环境老&#xff0c;本地环境版本高&#xff0c;ssh不过去。正好最近对 腾讯出品的云原生 cnb 感兴趣&…...

LangChain知识库管理后端接口:数据库操作详解—— 构建本地知识库系统的基础《二》

这段 Python 代码是一个完整的 知识库数据库操作模块&#xff0c;用于对本地知识库系统中的知识库进行增删改查&#xff08;CRUD&#xff09;操作。它基于 SQLAlchemy ORM 框架 和一个自定义的装饰器 with_session 实现数据库会话管理。 &#x1f4d8; 一、整体功能概述 该模块…...

深度学习水论文:mamba+图像增强

&#x1f9c0;当前视觉领域对高效长序列建模需求激增&#xff0c;对Mamba图像增强这方向的研究自然也逐渐火热。原因在于其高效长程建模&#xff0c;以及动态计算优势&#xff0c;在图像质量提升和细节恢复方面有难以替代的作用。 &#x1f9c0;因此短时间内&#xff0c;就有不…...