java中23种设计模式之12-装饰模式(decorator pattern)

class Girl { public void showAppearance() { System.out.println("the girl: face without make up"); } } class TakeFlower extends Girl { Girl girl=null; public TakeFlower(Girl girl) { this.girl=girl; } public void showAppearance() { girl.showAppearance(); takeFlower(); } public void takeFlower() { System.out.println(" take flower"); } } class TakeNecklace extends Girl { Girl girl=null; public TakeNecklace(Girl girl) { this.girl=girl; } public void showAppearance() { girl.showAppearance(); takeNecklace(); } public void takeNecklace() { System.out.println(" take necklace"); } } class TakeEarrings extends Girl { Girl girl=null; public TakeEarrings(Girl girl) { this.girl=girl; } public void showAppearance() { girl.showAppearance(); takeEarrings(); } public void takeEarrings() { System.out.println(" take earrings"); } } public class DecoratorPatternTest { public static void main(String[] args) { Girl aGirl=new TakeNecklace(new TakeFlower(new Girl())); aGirl.showAppearance(); aGirl=new TakeFlower(new TakeEarrings(new Girl())); aGirl.showAppearance(); } }

posted on 2015-04-02 20:43  wudymand  阅读(158)  评论(0编辑  收藏  举报

导航