11.代理模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cuicanxingchen123456/article/details/84313261

Head-First-Design-Patterns-master\src\headfirst\designpatterns\proxy

定义:为另一个对象提供一个替身或占位符以控制对这个对象的访问

控制和管理访问。

案例:

远程监视糖果机器

客户堆:

客户对象调用客户辅助对象

服务器堆:

客户辅助对象调用服务辅助对象

服务辅助对象调用服务对象。

RMI:

RMI为客户提供了客户辅助对象和服务辅助对象

步骤一:制作远程接口
public interface MyRemote extends Remote{
    public String sayHello()throws RemoteException;
}

步骤二:制作远程实现
public class  MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
    public String sayHello(){}
    publicMyRemoteImpl()throws RemoteException{}
    public static void main(String[] args){
       try{
           MyRemote service=new MyRemoteImpl();
            Naming.rebind("RemoteHello",service);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

}
步骤三:产生Stub和Skeleton
使用rmic来生成

步骤四执行remiregistry:
开启一个终端,启动rmiregistry

步骤五启动服务:
开启一个终端,从实现类的main启动,先实例化一个服务对象MyRemoteImpl,然后倒RMI registry中注册


客户端代码:
public class MyRemoteClient{
  public static void main(String[] args){
        new MyRemoteClient().go();
    }
    public void go(){
        try{
            MyRemote service =(MyRemote) Naming.looking(rmi://127.0.0.1/RemoteHello);
            String s=service.sayHello();
        }catch(){

        }
     }
}
使用java api的reflect包做代理
代理模式和装饰者模式的区别

使用java api的reflect包做代理

代理模式和装饰者模式的区别

代理模式的种类:

远程代理

虚拟代理

动态代理

这本书代理模式讲的不太容易理解,可以参考网上其他博客学习学习。

https://www.cnblogs.com/chentingk/p/6433372.html

https://www.cnblogs.com/daniels/p/8242592.html

https://blog.csdn.net/qq_34178598/article/details/78630934

猜你喜欢

转载自blog.csdn.net/cuicanxingchen123456/article/details/84313261