JAVA设计模式学习笔记-代理模式

远程糖果机监控项目

监控糖果机:地点、糖果库存和当前状态
本地糖果机监控怎么做

package com.java.jikexueyuan.agentmode.candymachine;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import com.java.jikexueyuan.agentmode.candymachinermi.CandyMachineRemote;

public class CandyMachine extends UnicastRemoteObject implements CandyMachineRemote{

    State mSoldOutState;
    State mOnReadyState;
    State mHasCoin;
    State mSoldState;
    State mWinnerState;
    private String location="";
    private State state;
    private int count = 0;

    public CandyMachine(String location,int count) throws RemoteException{
        this.location=location;
        this.count = count;
        mSoldOutState = new SoldOutState(this);
        mOnReadyState = new OnReadyState(this);
        mHasCoin = new HasCoin(this);
        mSoldState = new SoldState(this);
        mWinnerState = new WinnerState(this);
        if (count > 0) {
            state = mOnReadyState;
        } else {
            state = mSoldOutState;
        }
    }
    public String getLocation()
    {
        return location;
    }
    public void setState(State state) {
        this.state = state;
    }

    public void insertCoin() {
        state.insertCoin();
    }

    public void returnCoin() {
        state.returnCoin();
    }

    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    void releaseCandy() {

        // TODO Auto-generated method stub
        if (count > 0) {
            count = count - 1;
            System.out.println("a candy rolling out!");
        }

    }

    public int getCount() {
        return count;
    }

    public void printstate() {
        state.printstate();
    }
    public State getstate() {
        return state;
    }
}
package com.java.jikexueyuan.agentmode;

import java.util.ArrayList;

import com.java.jikexueyuan.agentmode.candymachine.CandyMachine;

public class Monitor {
    private ArrayList<CandyMachine> candyMachinelst;

    public Monitor() {
        candyMachinelst = new ArrayList<CandyMachine>();
    }

    public void addMachine(CandyMachine mCandyMachine) {
        candyMachinelst.add(mCandyMachine);
    }

    public void report() {
        CandyMachine mCandyMachine;
        for (int i = 0, len = candyMachinelst.size(); i < len; i++) {
            mCandyMachine = candyMachinelst.get(i);
            System.out.println("Machine Loc:" + mCandyMachine.getLocation());
            System.out.println("Machine Candy count:"
                    + mCandyMachine.getCount());
            System.out.println("Machine State:"
                    + mCandyMachine.getstate().getstatename());

        }

    }

}
package com.java.jikexueyuan.agentmode;

import java.rmi.RemoteException;

import com.java.jikexueyuan.agentmode.candymachine.CandyMachine;

public class MainTest {
    public static void main(String[] args) throws RemoteException {
        Monitor mMonitor=new Monitor();
        CandyMachine mCandyMachine = new CandyMachine("NY",6);
        mMonitor.addMachine(mCandyMachine);
        
        mCandyMachine = new CandyMachine("TK",4);
        mCandyMachine.insertCoin();
        mMonitor.addMachine(mCandyMachine);
        
        mCandyMachine = new CandyMachine("Bj",14);
        mCandyMachine.insertCoin(); 
        mCandyMachine.turnCrank();
        mMonitor.addMachine(mCandyMachine);

        mMonitor.report();
    }
}

远程代理:远程对象的本地代表,通过它可以让远程对象当本地对象来调用。
远程代理通过网络和真正的远程对象沟通信息。
JAVA设计模式学习笔记-代理模式

利用Java RMI实现远程代理
JAVA设计模式学习笔记-代理模式

代理模式:为一个对象提供一个替身,以控制对这个对象的访问
被代理的对象可以是远程对象、创建开销大的对象或需要安全控制的对象
代理模式有很多变体,都是为了控制与管理对象访问

RMI远程方法调用是计算机之间通过网络实现对象调用的一种通讯机制。
使用这种机制,一台计算机上的对象可以调用另外一台计算机上的对象来获取远
程数据。
在过去,TCP/IP通讯是远程通讯的主要手段,面向过程的开发
而RPC使程序员更容易地调用远程程序,但在面对复杂的信息传讯时,RPC依然
未能很好的支持
RMI被设计成一种面向对象开发方式,允许程序员使用远程对象来实现通信

制作远程接口:接口文件
远程接口的实现:Service文件
RMI服务端注册,开启服务
RMI代理端通过RMI查询到服务端,建立联系,通过接口调用远程方法

接口

package com.java.jikexueyuan.agentmode.rmi;

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

public interface MyRemote extends Remote{

    public String sayHello() throws RemoteException;
    
}
package com.java.jikexueyuan.agentmode.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

@SuppressWarnings("serial")
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{

    protected MyRemoteImpl() throws RemoteException {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String sayHello() throws RemoteException {
        // TODO Auto-generated method stub
        return "Hello World!";
    }
    public static void main(String[] args) {
        
        try {
            MyRemote service=new MyRemoteImpl();
            // LocateRegistry.createRegistry(6600);  
            Naming.rebind("rmi://127.0.0.1:6600/RemoteHello", service);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println( e.toString());
        } 
        
        
    }
}
package com.java.jikexueyuan.agentmode.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:6600/RemoteHello");
            String s=service.sayHello();
            System.out.println(s);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
}

示例项目类结构图
JAVA设计模式学习笔记-代理模式

package com.java.jikexueyuan.agentmode.candymachinermi;

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

import com.java.jikexueyuan.agentmode.candymachine.State;

public interface CandyMachineRemote extends Remote{
    public String  getLocation() throws RemoteException;
    public int getCount() throws RemoteException;
    public State getstate() throws RemoteException;
}
上一篇:java-RMI问题:服务器关闭后,对象引用会发生什么?


下一篇:Java中间件之RMI及实例介绍 · zijian's blog