标签:
对一个类或者接口进行装饰
以下为例子:
package cn.test.demo; //装饰者设计模式练习 interface Work{ void work(); } class Son implements Work{ private String name; Son(String name){ this.name=name; } @Override public void work() { // TODO Auto-generated method stub System.out.println("画画"); } } class Monther implements Work{ Work work; private String name; Monther(String name,Work work){ this.name=name; this.work=work; } @Override public void work() { // TODO Auto-generated method stub work.work(); System.out.println("上涂料"); } } class Father implements Work{ String name; Work work; Father(String name,Work work){ this.name=name; this.work=work; } @Override public void work() { // TODO Auto-generated method stub work.work(); System.out.println("上画框"); } } public class Test9 { public static void main(String[] args) { Son s=new Son("小明"); s.work(); Monther m=new Monther("母亲", s); m.work(); Father father =new Father("父亲", m); father.work(); } }
标签:
原文地址:http://www.cnblogs.com/may12138/p/5763248.html