zoukankan      html  css  js  c++  java
  • 委派模式

    委派模式(Delegate)是面向对象设计模式中常用的一种模式。这种模式的原理为类B和类A是两个互相没有任何关系的类,B具有和A一模一样的方法和属性;并且调用B中的方法,属性就是调用A中同名的方法和属性。B好像就是一个受A授权委托的中介。第三方的代码不需要知道A的存在,也不需要和A发生直接的联系,通过B就可以直接使用A的功能,这样既能够使用到A的各种公能,又能够很好的将A保护起来了。一举两得,岂不很好!下面用一个很简单的例子来解释下:

    <span style="font-size:18px;">class A{
        void method1(){...}
        void method2(){...}
    }
    class B{
        //delegation
        A a = new A();
       //method with the same name in A
        void method1(){ a.method1();}
        void method2(){ a.method2();}
        //other methods and attributes
        ...
    }
    public class Test{
         public static void main(String args[]){
        B b = new B();
        b.method1();//invoke method2 of class A in fact
        b.method2();//invoke method1 of class A in fact
        }
        
    }</span>
  • 相关阅读:
    问题2017S03
    问题2017S02
    高等代数问题1
    无穷积分换元法的严格解释
    线性空间的同构理论
    问题2017S01
    朴素贝叶斯分类
    决策树
    温习MATLAB
    感知机
  • 原文地址:https://www.cnblogs.com/bendantuohai/p/4658178.html
Copyright © 2011-2022 走看看