代理模式vs装饰模式

 代理模式

 先上类图结构
关键点:一个抽象接口Subject,一个实现类RealSubject,一个实现类Proxy,该类中包含一个属性Subject realSubject;

静态代理类
 
抽象接口Subject如下:
public interface Subject {            
        public void operation();
 
}
 
实现类RealSubject如下:
public class RealSubject implements Subject {
 
        @Override
        public void operation() {
               // TODO Auto-generated method stub
              System. out.println("We will do something..." );
 
       }
 
}
代理类Proxy实现如下:
public class Proxy implements Subject {
 
        private Subject realSubject = null;
        public Proxy(Subject _realSubject)
       {
               this .realSubject = _realSubject;
       }
 
        private void doSomething()
       {
              System. out .println("Before execute the operation,we may do a filter or a notify...");
       }
        @Override
        public void operation() {
               // TODO Auto-generated method stub
               this .doSomething(); //代理类负责接口限定:是否可以调用真是角色,以及是否对发送到真是角色的消息进行变形处理,他不对被主题角色的功能做任何处理,保证原汁原味的调用,
               this .realSubject .operation();
 
       }
 
}
在代理类中,封装实际对象的操作,在调用实际对象方法之前,可以对请求进行过滤或者向服务端或者客户单发送通知。
 
场景类Client如下:
public class Client {
 
        /**
        * @param args
        */
        public static void main(String[] args) {
               // TODO Auto-generated method stub
 
              Subject realSubject = new RealSubject();
              Subject proxy = new Proxy(realSubject);
              proxy.operation();
 
       }
 
}

动态代理类
借助于java的反射机制
抽象主题,实际主题均同上
动态代理需要调用Proxy.newProxyInstance(loader, interfaces, handler);
 
因此需要实现InvocationHandler接口
public class MyInvocationHandler implements InvocationHandler {
        private Object target = null;
        public MyInvocationHandler(Object _target)
       {
               this.target = _target;
       }
 
        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
                      throws Throwable {
               // TODO Auto-generated method stub
               if(method.getName().toString() == "operation" )
              {
                     System. out.println("Before execute the operation,we may do a filter or a notify..." );
              }
              
               return method.invoke(target , args);
       }
 
}
 
场景类client实现
public class Client {
 
        /**
        * @param args
        */
        public static void main(String[] args) {
               // TODO Auto-generated method stub
 
              Subject realSubject = new RealSubject();
              InvocationHandler handler = new MyInvocationHandler(realSubject);
              Subject proxy = (Subject)Proxy.newProxyInstance(realSubject.getClass().getClassLoader(), realSubject.getClass().getInterfaces(), handler);
               proxy.operation();
              
 
       }
 
}

装饰模式
 
 
装饰模式其实是一种特殊的代理,主要包括四个组成部分:Component、ConcreteComponent、Decorater、ConcreteDecorator。
Component实现如下:
public interface Component {
        public void operation();
 
}
ConcreteComponent实现如下:
public class ConcreteComponent implements Component {
 
        @Override
        public void operation() {
               // TODO Auto-generated method stub
              System. out.println("we will do some opertaion..." );
 
       }
 
}
Decorater实现如下:
public class Decorator implements Component{
        private Component com = null;
        public Decorator(Component _com)
       {
               this.com = _com;
       }
        @Override
        public void operation() {
               // TODO Auto-generated method stub
               this.com .operation();
              
       }      
 
}
ConcreteDecorator实现如下:
public class ConcreteDecorator extends Decorator {
 
        public ConcreteDecorator(Component _com) {
               super(_com);
              
               // TODO Auto-generated constructor stub
       }
 
        private void doSomething()
       {
              System. out.println("do something else......" );
       }
        @Override
        public void operation() {
               // TODO Auto-generated method stub
               this.doSomething(); //保证接口不变的情况下加强类的功能,他保证的是被修饰的对象功能比原始对象丰富。但不做准入/条件和准入参数过滤。
               super.operation();
       }
       
}
场景类模拟如下:
public class Client {
 
        /**
        * @param args
        */
        public static void main(String[] args) {
               // TODO Auto-generated method stub
 
              Component com = new ConcreteComponent();
              Component dec = new ConcreteDecorator(com);
              dec.operation();
       }
 
}

代理模式与装饰模式对比
 
代理模式与装饰模式的区别:
代理模式是把当前的行为或功能委托给其他对象执行,代理类负责接口限定:是否可以调用真是角色,以及是否对发送到真是角色的消息进行变形处理,他不对被主题角色的功能做任何处理,保证原汁原味的调用,代理模式使用到机制开发就是AOP,它使用了代理和反射的技术。
装饰模式是在要保证接口不变的情况下加强类的功能,他保证的是被修饰的对象功能比原始对象丰富。但不做准入条件和准入参数过滤。
 
原文地址:https://www.cnblogs.com/benshan/p/3069133.html