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

news2024/9/21 16:31:52
代码文件目录:

 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 GumballMachine
        extends 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.coloradmin.cn/o/902274.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

前端Vue自定义等分底部菜单导航按钮 自适应文字宽度 可更改组件位置

随着技术的发展&#xff0c;开发的复杂度也越来越高&#xff0c;传统开发方式将一个系统做成了整块应用&#xff0c;经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改&#xff0c;造成牵一发而动全身。 通过组件化开发&#xff0c;可以有效实现…

Windows 平台下微软开发的神器 PowerToys 使用笔记

文章目录 Part.I IntroductionPart.II 安装Part.III 常用操作Chap.I 快捷键Chap.II 分屏示例 Reference Part.I Introduction PowerToys 是一款来自微软的系统增强工具&#xff0c;就像是一个神奇的 Win10 外挂&#xff0c;整套软件由若干子组件构成&#xff0c;包括&#xff…

从零开始创建 Spring Cloud 分布式项目,不会你打我

目录 一、Spring Cloud 和 分布式 二、创建新项目 三、导入 Spring Cloud 依赖 四、配置 Spring Cloud 一、Spring Cloud 和 分布式 Spring Cloud是一个基于Spring框架的开源微服务框架&#xff0c;它提供了一系列工具和组件&#xff0c;用于帮助开发人员构建分布式系统中…

EndNote极简入门【如何使用】

在开始菜单栏打开&#xff1a; &#xff08;点左下角忽略即可&#xff09; 第一次打开呢就如下图所示&#xff1a; 空白的&#xff0c;只有一个灰色的界面&#xff1a; 新建一个自己库&#xff1a; 然后就会弹出东西啦&#xff1a; 导出endnote学术期刊的引文&#xff1a;&…

人工智能时代未来程序员必备的三大利器:异,理,说

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…

Python学习:迭代器与生成器的深入解析

函数在Python中扮演着重要角色&#xff0c;不仅可以封装代码逻辑&#xff0c;还能通过迭代器和生成器这两种强大的技术&#xff0c;实现更高效的数据处理和遍历。本篇博客将深入探讨Python函数的迭代器和生成器&#xff0c;结合实际案例为你揭示它们的神奇&#xff0c;以及如何…

《论文阅读18》 SSD: Single Shot MultiBox Detector

一、论文 研究领域&#xff1a; 2D目标检测论文&#xff1a;SSD: Single Shot MultiBox Detector ECCV 2016 论文链接论文github 二、论文概要 SSD网络是作者Wei Liu在ECCV 2016上发表的论文。对于输入尺寸300x300的网络 使用Nvidia Titan X在VOC 2007测试集上达到74.3%mA…

无涯教程-PHP - 条件判断

if... elseif ... else和switch语句用于根据不同条件进行判断。 您可以在代码中使用条件语句来做出决定&#xff0c; PHP支持以下三个决策语句- if ... else语句 - 如果要在条件为真时执行&#xff0c;而在条件不为真时执行另一个代码&#xff0c;请使用此语句 els…

nodejs+vue古诗词在线测试管理系统

一开始&#xff0c;本文就对系统内谈到的基本知识&#xff0c;从整体上进行了描述&#xff0c;并在此基础上进行了系统分析。为了能够使本系统较好、较为完善的被设计实现出来&#xff0c;就必须先进行分析调查。基于之前相关的基础&#xff0c;在功能上&#xff0c;对新系统进…

免费开源使用的几款红黑网络流量工具,自动化的多功能网络侦查工具、超级关键词URL采集工具、Burpsuite被动扫描流量转发插件

免费开源使用的几款红黑网络流量工具&#xff0c;自动化的多功能网络侦查工具、超级关键词URL采集工具、Burpsuite被动扫描流量转发插件。 #################### 免责声明&#xff1a;工具本身并无好坏&#xff0c;希望大家以遵守《网络安全法》相关法律为前提来使用该工具&am…

C++ string 的用法

目录 string类string类接口函数及基本用法构造函数&#xff0c;析构函数及赋值重载函数元素访问相关函数operator[]atback和front 迭代器iterator容量操作size()和length()capacity()max_sizeclearemptyreserveresizeshrink_to_fit string类对象修改操作operatorpush_backappen…

vue3组件多个根节点报错

打开扩展商店搜索下载 vetur 打开设置命令面板 搜索eslint 将下面的勾选取消

Spring MVC相关异常类

Spring MVC相关异常类 使用&#xff20;ResponseStatus修饰异常类使用&#xff20;ExceptionHandler修饰异常处理方法 使用&#xff20;ResponseStatus修饰异常类 如果希望程序抛出自定义异常时也能被异常解析器解析成HTTP状态码&#xff0c;从而显示Web服务器提供的错误页面&…

统一规范化,东盟区域化发展,致力于建立电动汽车生态系统

根据印尼外交部东盟经济合作司司长的透露&#xff0c;东盟正在致力于建立一个电动汽车生态系统&#xff0c;并期望得到东亚邻国的支持。具体而言&#xff0c;东盟计划在“APT”机制下宣布新的电动汽车生态系统合作计划&#xff0c;并预计在下个月举行的第 43 届东盟峰会上正式宣…

回归预测 | MATLAB实现WOA-SVM鲸鱼算法优化支持向量机多输入单输出回归预测(多指标,多图)

回归预测 | MATLAB实现WOA-SVM鲸鱼算法优化支持向量机多输入单输出回归预测&#xff08;多指标&#xff0c;多图&#xff09; 目录 回归预测 | MATLAB实现WOA-SVM鲸鱼算法优化支持向量机多输入单输出回归预测&#xff08;多指标&#xff0c;多图&#xff09;效果一览基本介绍程…

自己实现 SpringMVC 底层机制 系列之--实现任务阶段 2- 完成客户端浏览器可以请求控制层

&#x1f600;前言 本文是自己实现 SpringMVC 底层机制的第二篇之完成实现任务阶段 2- 完成客户端浏览器可以请求控制层 &#x1f3e0;个人主页&#xff1a;尘觉主页 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TabAxc96-1692497234107)(https://…

Spring MVC异常处理

Spring MVC异常处理 Spring MVC异常处理机制HandlerExceptionResolver的实现类DefaultHandlerExceptionResolver实现类DefaultHandlerExceptionResolver 在Controller的请求处理方法中手动使用try…catch块捕捉异常&#xff0c;当捕捉到指定的异常时&#xff0c;系统返回对应的…

ABBYY FineReader15最新专业的PDF OCR图片文字识别软件

ABBYY FineReader PDF15是专业的OCR图片文字识别软件&#xff0c;可以快速、准确、方便地将扫描纸质文件、PDF格式及数字或移动电话图像转换成可编辑格式——Microsoft Word、Excel、PowerPoint、可检索的PDF、HTML、DjVu等。99.8%的识别准确率即刻识别文本&#xff0c;复制和粘…

Stanley 轨迹跟踪算法研究

Stanley 轨迹跟踪算法研究 理论基础 首先还是阅读论文 《基于改进鲸鱼优化算法的Stanley算法研究》 《复杂环境下的移动机器人路径规划技术研究》 《Stanley:The Robot that Won the DARPA Grand Challenge》 《无人驾驶汽车轨迹跟随控制研究》 《基于混合算法的校园智能车路…

【kafka】-分区-消费端负载均衡

一.为什么kafka要做分区&#xff1f; 因为当一台机器有可能扛不住&#xff08;类比&#xff1a;就像redis集群中的redis-cluster一样&#xff0c;一个master抗不住写&#xff0c;那么就多个master去抗写&#xff09;&#xff0c;把一个队列的单一master变成多个master&#xf…