装饰模式例子(Decorator Pattern)
2016-12-01 22:31
344 查看
/* * 人类 */ public class Person { public Person() { } public Person(String name){ this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void show(){ System.out.println("来自Person"); } }
/* * 服饰类 */ public class Decorator extends Person { protected Person person; public void Decorate(Person component){ this.person = component; } @Override public void show() { person.show(); } }
public class TShirts extends Decorator{ @Override public void show() { System.out.println("T恤"); super.show(); } }
public class BigTrouser extends Decorator{ @Override public void show() { System.out.println("垮裤"); super.show(); } }
/* * 客户端 */ public class App { public static void main(String[] args) { Person p1 = new Person("小菜"); TShirts ts = new TShirts(); BigTrouser bt = new BigTrouser(); ts.Decorate(p1); bt.Decorate(ts); bt.show(); } }
相关文章推荐
- 理解装饰模式(decorator pattern)两个小例子
- 装饰模式(Decorator Pattern)
- 解读设计模式----装饰模式(Decorator Pattern)
- .NET设计模式(10):装饰模式(Decorator Pattern)
- 装饰模式(Decorator/Wrapper Pattern)
- 装饰模式Decorator Pattern
- .NET设计模式-装饰模式(Decorator Pattern)
- 装饰模式(Decorator Pattern)
- .NET设计模式(10):装饰模式(Decorator Pattern)
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
- 装饰模式(Decorator Pattern)
- .NET设计模式-装饰模式(Decorator Pattern)
- 装饰模式(Decorator pattern)应用----日志信息输出
- 设计模式8:Decorator Pattern (装饰模式)
- (第Ⅲ部分 结构型模式篇) 第9章 装饰模式(Decorator Pattern)
- .NET设计模式(10):装饰模式(Decorator Pattern)
- 设计模式[6] Decorator Pattern 装饰模式
- NET设计模式之一:装饰模式(Decorator Pattern)
- 模式人生-从生活中的点点滴滴认识设计模式1-装饰器模式(Decorator Pattern)
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)