zoukankan      html  css  js  c++  java
  • 代理类和装饰类的区别

      

    装饰模式


       装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

      装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不使用创造更多子类的情况下,将对象的功能加以扩展。

      在装饰模式中的角色有:

      ●  抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。

      ●  具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。

      ●  装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。

      ●  具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。

    源代码

      抽象构件角色

    public interface Component {
        
        public void sampleOperation();
        
    }

      具体构件角色

    复制代码
    public class ConcreteComponent implements Component {
    
        @Override
        public void sampleOperation() {
            // 写相关的业务代码
        }
    
    }
    复制代码

      装饰角色

    复制代码
    public class Decorator implements Component{
        private Component component;
        
        public Decorator(Component component){
            this.component = component;
        }
    
        @Override
        public void sampleOperation() {
            // 委派给构件
            component.sampleOperation();
        }
        
    }
    复制代码

      具体装饰角色

    复制代码
    public class ConcreteDecoratorA extends Decorator {
    
        public ConcreteDecoratorA(Component component) {
            super(component);
        }
        
        @Override
        public void sampleOperation() {
         super.sampleOperation(); // 写相关的业务代码 } }
    复制代码
     
     
     

       代理模式


       ●  抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。

      ●  具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。

      ●  代理(Decorator)角色:持有一个构件(ConcreteComponent)对象的实例,并定义一个与抽象构件接口一致的接口。

    1. publicinterface Component{  
    2. publicvoid method();  
    3. }  
    1. public class ConcreteComponent implements Component{  
    2.   
    3.     @Override  
    4.     public void method() {  
    5.         System.out.println("the original method!");  
    6.     }  
    7. }  
    1. public class Proxy implements Component{  
    2.   
    3.     private ConcreteComponent cc;  
    4.     public Proxy(){  
    5.         super();  
    6.         this.cc= new ConcreteComponent ();  
    7.     }  
    8.     @Override  
    9.     public void method() {  
    10.         before();  
    11.         cc.method();  
    12.         atfer();  
    13.     }  
    14.     private void atfer() {  
    15.         System.out.println("after proxy!");  
    16.     }  
    17.     private void before() {  
    18.         System.out.println("before proxy!");  
    19.     }  
    20. }  

    测试类:

    1. public class ProxyTest {  
    2.   
    3.     public static void main(String[] args) {  
    4.         Component c= new Proxy();  
    5.         c.method();  
    6.     }  
    7.   
    8. }  

    两者的区别:代理是控制访问的方式/途径不需要传参数,装饰是在原有基础上增加功能需要传参数,而参数就是具体构建对象


  • 相关阅读:
    MySQL百万级、千万级数据多表关联SQL语句调优
    不就是SELECT COUNT语句吗,居然有这么多学问
    分布式锁讲解
    Java 中堆和栈的区别
    Java中的回调机制
    在Eclipse上Maven环境配置使用
    项目忽然出现 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法
    HttpServletResponse
    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
    深入浅出java常量池
  • 原文地址:https://www.cnblogs.com/keyi/p/7552665.html
Copyright © 2011-2022 走看看