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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/76308.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用swoole实现实时消息推送给客户端

一. 测试服务端 //测试服务端public function testServer(){$server new Server(192.168.0.144, 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);$server->on(request, function ($request, $response) {$response->header(Content-Type, text/plain);$response->end("He…

宝塔部署Java+Vue前后端分离项目经验总结

前言 之前部署服务器都是在Linux环境下自己一点一点安装软件&#xff0c;听说用宝塔傻瓜式部署更快&#xff0c;这次浅浅尝试了一把。 确实简单&#xff01; 1、 买服务器 咋买服务器略&#xff0c;记得服务器装系统就装 Cent OS 7系列即可&#xff0c;我装的7.6。 2、创建…

SpringBoot案例 调用第三方接口传输数据

一、前言 最近再写调用三方接口传输数据的项目&#xff0c;这篇博客记录项目完成的过程&#xff0c;方便后续再碰到类似的项目可以快速上手 项目结构&#xff1a; 二、编码 这里主要介绍HttpClient发送POST请求工具类和定时器的使用&#xff0c;mvc三层架构编码不做探究 pom.x…

每日刷题(翻转+二分+BFS)

食用指南&#xff1a;本文为作者刷题中认为有必要记录的题目 ♈️今日夜电波&#xff1a;凄美地—郭顶 1:10 ━━━━━━️&#x1f49f;──────── 4:10 &#x1f504; ◀️ ⏸ ▶️ ☰…

MyBatis快速入门以及环境搭建和CRUD的实现

目录 前言 一、MyBatis简介 1.MyBatis是什么 2.MyBatis的特点 3.mybatis的作用 4.MyBatis的应用场景 5.MyBatis优缺点 二、相关概念 1.ORM概述 2.常见的ORM框架 3.什么是持久层框架 三、MyBatis的工作原理 1.框架交互 2.工作原理 ​编辑 四、MyBatis环境搭建 1…

Docker容器:docker镜像的创建及dockerfile

Docker容器&#xff1a;docker镜像的创建及dockerfile案例 一.docker镜像的三种创建方法 创建镜像有三种方法&#xff1a;基于现有镜像创建、基于本地模板创建及基于dockerfile创建 1.基于现有镜像创建 1.1 启动镜像 #首先启动一个镜像&#xff0c;在容器里做修改 docker …

记录win 7旗舰版 “VMware Alias Manager and Ticket Service‘(VGAuhService)启动失败。

记录win 7旗舰版 "VMware Alias Manager and Ticket Service’(VGAuhService)启动失败。 描述如图 https://learn.microsoft.com/zh-CN/cpp/windows/latest-supported-vc-redist?viewmsvc-140#visual-studio-2015-2017-2019-and-2022 安装对应版本的VC 库就可以解决问…

机器人的运动范围

声明 该系列文章仅仅展示个人的解题思路和分析过程&#xff0c;并非一定是优质题解&#xff0c;重要的是通过分析和解决问题能让我们逐渐熟练和成长&#xff0c;从新手到大佬离不开一个磨练的过程&#xff0c;加油&#xff01; 原题链接 机器人的运动范围https://leetcode.c…

Python 学习笔记——代码基础

目录 Python基础知识 变量 赋值 数据类型 print用法 print格式化输出 运算符 if-else 数据结构 元组 in运算符 列表 切片 [ : ] 追加 append() 插入 insert&#xff08;&#xff09; 删除 pop() 字典 循环 for循环 for循环应用——遍历 for循环应用——累加…

【BASH】回顾与知识点梳理(二十九)

【BASH】回顾与知识点梳理 二十九 二十九. 进程和工作管理29.1 什么是进程 (process)进程与程序 (process & program)子进程与父进程&#xff1a;fork and exec&#xff1a;进程呼叫的流程系统或网络服务&#xff1a;常驻在内存的进程 29.2 Linux 的多人多任务环境多人环境…

STM32都学什么

一、什么是STM32? 对于STM32&#xff0c;从字面意思上来理解&#xff0c;ST是意法半导体&#xff0c;M是Microelectronics的缩写&#xff0c;其中32表示的是32位&#xff0c;那么整合起来理解就是&#xff1a;STM32就是指的ST公司开发的32位微控制器。在如今的32位控制器中&am…

​8th参考文献:[8]许少辉.乡村振兴战略下传统村落文化旅游设计[M]北京:中国建筑出版传媒,2022.

​&#xff18;th参考文献&#xff1a;&#xff3b;&#xff18;&#xff3d;许少辉&#xff0e;乡村振兴战略下传统村落文化旅游设计&#xff3b;&#xff2d;&#xff3d;北京&#xff1a;中国建筑出版传媒&#xff0c;&#xff12;&#xff10;&#xff12;&#xff12;&…