你是一个很牛B的程序员,并且你不想天天面对产品那张臭脸,听他叨逼叨。然后你就和他约定。你别舔着个脸来烦我,有什么事情换个肤白貌美气质佳的美女和我说。
使用代理,将代表某个真实的对象。
现在使用RMI 远程代理实现 远程监控糖果机状态。
首先要获得糖果机远程代理的对象的接口。
public interface GumballMachineRemote extends Remote { //获取糖果机数量 public int getCount() throws RemoteException; //获取位置 public String getLocation() throws RemoteException; //获取状态 public State getState() throws RemoteException; }
然后定义待监控的糖果机。
public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote { private static final long serialVersionUID = 2L; State soldOutState; State noQuarterState; State hasQuarterState; State soldState; State winnerState; State state = soldOutState; int count = 0; String location; //初始化方法,需要知道糖果机的位置,以及里面的糖果数 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.count = numberGumballs; if (numberGumballs > 0) { state = noQuarterState; } this.location = location; } public void insertQuarter() { state.insertQuarter(); } public void ejectQuarter() { state.ejectQuarter(); } public void turnCrank() { state.turnCrank(); state.dispense(); } void setState(State state) { this.state = state; } void releaseBall() { System.out.println("A gumball comes rolling out the slot..."); if (count != 0) { count = count - 1; } } public void refill(int count) { this.count = count; state = noQuarterState; } public int getCount() { return count; } public State getState() { return state; } public String getLocation() { return location; } public State getSoldOutState() { return soldOutState; } public State getNoQuarterState() { return noQuarterState; } public State getHasQuarterState() { return hasQuarterState; } public State getSoldState() { return soldState; } public State getWinnerState() { return winnerState; } public String toString() { StringBuffer result = new StringBuffer(); result.append("\nMighty Gumball, Inc."); result.append("\nJava-enabled Standing Gumball Model #2014"); result.append("\nInventory: " + count + " gumball"); if (count != 1) { result.append("s"); } result.append("\n"); result.append("Machine is " + state + "\n"); return result.toString(); } }
糖果机状态:
public interface State extends Serializable { public void insertQuarter(); public void ejectQuarter(); public void turnCrank(); public void dispense(); }
状态实现:
public class HasQuarterState implements State { private static final long serialVersionUID = 2L; Random randomWinner = new Random(System.currentTimeMillis()); GumballMachine gumballMachine; public HasQuarterState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter() { System.out.println("You can't insert another quarter"); } public void ejectQuarter() { System.out.println("Quarter returned"); gumballMachine.setState(gumballMachine.getNoQuarterState()); } public void turnCrank() { System.out.println("You turned..."); int winner = randomWinner.nextInt(10); if (winner == 0) { gumballMachine.setState(gumballMachine.getWinnerState()); } else { gumballMachine.setState(gumballMachine.getSoldState()); } } public void dispense() { System.out.println("No gumball dispensed"); } public String toString() { return "waiting for turn of crank"; } }
。。。。
接下来要写客户端相关对象。
首先是监视器:
public class GumballMonitor { GumballMachine machine; public GumballMonitor(GumballMachine machine) { this.machine = machine; } public void report() { System.out.println("Gumball Machine: " + machine.getLocation()); System.out.println("Current inventory: " + machine.getCount() + " gumballs"); System.out.println("Current state: " + machine.getState()); } }
下面是测试程序:
public class GumballMonitorTestDrive { public static void main(String[] args) { String[] location = {"rmi://santafe.mightygumball.com/gumballmachine", "rmi://boulder.mightygumball.com/gumballmachine", "rmi://seattle.mightygumball.com/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(); } } }