java的装饰设计模式

类似python中的装饰器。

示例:

public class Test5 {
    public static void main(String[] args) {
        Worker w = new Worker(new Students());
        w.code();
    }
}

interface Coder {
    public abstract void code();
}

class Students implements Coder {

    @Override
    public void code() {
        System.out.println("java se");
        System.out.println("java web");
    }
}

class Worker implements Coder{

    Students s;

    public Worker(Students s) {
        this.s = s;
    }

    @Override
    public void code() {
        s.code();
        System.out.println("Database");
        System.out.println("ssm");
        System.out.println("ssm");
    }
}

// 输出结果:
java se
java web
Database
ssm
ssm

 

posted @ 2019-01-16 15:18  苦瓜爆炒牛肉  阅读(146)  评论(0编辑  收藏  举报