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

代码文件目录:
RMI:
MyRemote
java 复制代码
package Chapter11_ProxyPattern.RMI;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyRemote extends Remote {
    public String sayHello() throws RemoteException;
}
MyRemoteClient
java 复制代码
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
java 复制代码
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
java 复制代码
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
java 复制代码
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
java 复制代码
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
java 复制代码
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
java 复制代码
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();
        }
    }
}
五个状态类:

同样的修改:

java 复制代码
public class HasQuarterState implements State {
    private static final long serialVersionUID = 2L;
    Random randomWinner = new Random(System.currentTimeMillis());
相关推荐
hstar95278 小时前
三十四、面向对象底层逻辑-SpringMVC九大组件之FlashMapManager接口设计哲学
java·spring·设计模式·架构
秋田君9 小时前
深入理解JavaScript设计模式之单例模式
javascript·单例模式·设计模式
Dave_Young10 小时前
上位机开发过程中的设计模式体会(1):工厂方法模式、单例模式和生成器模式
c++·设计模式
on the way 12311 小时前
行为设计模式之Command (命令)
java·开发语言·设计模式
哆啦A梦的口袋呀11 小时前
基于Python学习《Head First设计模式》第八章 模板方法模式
python·学习·设计模式
qqxhb11 小时前
零基础设计模式——行为型模式 - 责任链模式
java·设计模式·责任链模式
昕冉13 小时前
利用Axure 9中继器绘制数据统计表原型图
设计模式·设计
黎䪽圓16 小时前
【Java多线程从青铜到王者】单例设计模式(八)
java·开发语言·设计模式
小小神仙19 小时前
JSCommon系列 - 为什么前端没有 Apache Commons?
前端·javascript·设计模式
蔡蓝20 小时前
设计模式-抽象工厂模式
设计模式·抽象工厂模式