首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计形式==装饰模式(Decorator)

2012-08-30 
设计模式==装饰模式(Decorator)/* * 装饰模式(Decorator) * **以对客户透明的方式来扩展对象的功能。 *用户

设计模式==装饰模式(Decorator)

/* * 装饰模式(Decorator) * *   *  以对客户透明的方式来扩展对象的功能。 *  用户根据功能需求随意选取组成对象的成分,通过方法的链式调用来实现。 *  可以给对象动态的增加功能,比继承灵活性更大。 */package model;public class TestDecorator {    public static void main(String[] args) {        Teacher t1 = new SimpleTeacher();        Teacher t2 = new CppTeacher(t1);        Teacher t3 = new JavaTeacher(t2);        t3.teach();        // t.teach();    }}abstract class Teacher {    public abstract void teach();}class SimpleTeacher extends Teacher {    public void teach() {        System.out.println("Good Good Study, Day Day Up");    }}class JavaTeacher extends Teacher {    Teacher teacher;    public JavaTeacher(Teacher t) {        this.teacher = t;    }    public void teach() {        teacher.teach();        System.out.println("Teach Java");    }}class CppTeacher extends Teacher {    Teacher teacher;    public CppTeacher(Teacher t) {        this.teacher = t;    }    public void teach() {        teacher.teach();        System.out.println("Teach C++");    }}
?

热点排行