论坛首页 Java企业应用论坛

代理 与 子类化

浏览 4146 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-12-02  

最近看一个斯坦福的视频, 里面的Cocoa框架大量使用了代理模式,定制类的行为,

 

学生就发问了,为什么不采用子类化的策略呢,

 

老师就说了 子类化 和 代理都是起到代码分离的作用。无所谓对错,有人喜欢子类化,有人喜欢代理,这是一个需要综合考虑的设计决策

 

 

自己写点东西记一下

 

package delegate.subclass;

public class UIApplication {

    Object view = new Object();

    public String draw() {
        return "draw a button on view" + view;
    }
}

 

package delegate.subclass;

public class UIApplicationSubclassA extends UIApplication {

    public String draw() {
        return "draw a text on view " + view;
    }
}
 
package delegate.subclass;

public class UIApplicationSubclassB extends UIApplication {
    
    public String draw() {
        return "draw a label on view " + view;
    }
    
}

 

 

 

package delegate.delegate;

public class UIApplcation {
    
    Object view;

    IViewController delegate;
    

    public void setDelegate (IViewController delegate ){
        this.delegate = delegate;
        view=delegate.getView();
    }
    
    public String draw(){
        return delegate.draw();
    }
}

 

package delegate.delegate;

public interface IViewController {
    String draw();
    Object getView();
}

 

 

package delegate.delegate;

public class AppDelegateA implements IViewController {

    Object view = new Object();

    @Override
    public String draw() {
        return "draw a text on view " + view;
    }

    @Override
    public Object getView() {
        return view;
    }

}
 
package delegate.delegate;

public class AppDelegateB implements IViewController {

    Object view = new Object();

    @Override
    public String draw() {
        return "draw a label on view " + view;
    }

    @Override
    public Object getView() {
        return view;
    }

}

 

 

 

 

package delegate;

import delegate.delegate.AppDelegateA;
import delegate.delegate.AppDelegateB;
import delegate.subclass.UIApplication;
import delegate.subclass.UIApplicationSubclassA;
import delegate.subclass.UIApplicationSubclassB;

public class Client {
    public static void main(String[] args) {
        System.out.println("test subclass---------------------");
        UIApplication sub1 =new UIApplicationSubclassA();
        System.out.println(sub1.draw());
        
        UIApplication sub2 =new UIApplicationSubclassB();
        System.out.println(sub2.draw());
        
        
        System.out.println("test delegate---------------------");
        delegate.delegate.UIApplcation app1 =new  delegate.delegate.UIApplcation();
        AppDelegateA delegateA= new AppDelegateA();
        app1.setDelegate(delegateA);
        System.out.println(app1.draw());
        
        delegate.delegate.UIApplcation app2 =new  delegate.delegate.UIApplcation();
        AppDelegateB delegateB= new AppDelegateB();
        app2.setDelegate(delegateB);
        System.out.println(app2.draw());
    }
}

 

 

 

console

test subclass---------------------
draw a text on view java.lang.Object@14318bb
draw a label on view java.lang.Object@ca0b6
test delegate---------------------
draw a text on view java.lang.Object@1b67f74
draw a label on view java.lang.Object@173a10f
 
   发表时间:2010-12-05  
有一条Design Principle: Favor composition over inheritance.
0 请登录后投票
   发表时间:2010-12-05   最后修改:2010-12-05
对delegate的翻译,比较习惯用委托。
看到你说代理,还以为是Proxy
0 请登录后投票
   发表时间:2010-12-05  
组合总比继承好。。
0 请登录后投票
   发表时间:2011-03-03  
一般是组合或者聚合 这种关系比继承关系要好些,组合模式不一样。

采用组合或者聚合有助于保持每个类被封装到单个任务上,这样类和继承的层次规模保持最小,不可能增长成为庞然大物。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics