装饰模式简单的代码:一个对象在构造函数的时候,持有另一个对象的引用。
package com.main.test; public class ZhuangShiPattener { public static void main(String[] args) { // TODO Auto-generated method stub Person person = new Person(); SuperPerson superPerson = new SuperPerson(person); superPerson.chiFan(); } } class Person{ public void chifan(){ System.out.println("我就是吃饭而已"); } } class SuperPerson{ private Person person; public SuperPerson(Person person){ this.person = person; } public void chiFan(){ System.out.println("先喝汤,开胃"); person.chifan(); System.out.println("喝酒,干杯"); System.out.println("甜点"); } }