文章标题:转载:设计模式之装饰模式(Decorator)
文章链接:https://blog.csdn.net/elizabethxxy/article/details/118608823
代码:
package com.study.javastudy.designpattern.decorator.dressingexample; public class Person { private String name; public Person() {} public Person(String name) { System.out.println("in class Person, method Person, this=" + this + ", name=" + name); this.name = name; } public void show() { System.out.println("in class Person, method show, this=" + this + ", name=" + name); System.out.println(name + "的装扮:"); } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; // 服装类 public class Finery extends Person { protected Person component; public void Decorate(Person component) { System.out.println("in class Finery, method Decorate, this=" + this + ", component=" + component); this.component = component; } @Override public void show() { System.out.println("in class Finery, method show, component=" + component); if(component != null) { component.show(); } } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; public class TShirts extends Finery { @Override public void show() { super.show(); System.out.println("in class TShirts, method show, this=" + this + ", component=" + component); System.out.println("TShirts "); } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; public class Sneakers extends Finery { @Override public void show() { super.show(); System.out.println("in class Sneakers, method show, this=" + this + ", component=" + component); System.out.println("Sneakers "); } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; public class Trouser extends Finery { @Override public void show() { super.show(); System.out.println("in class Trouser, method show, this=" + this + ", component=" + component); System.out.println("Trouser "); } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; public class LeatherShoes extends Finery { @Override public void show() { super.show(); System.out.println("in class LeatherShoes, method show, this=" + this + ", component=" + component); System.out.println("LeatherShoes "); } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; public class Tie extends Finery { @Override public void show() { super.show(); System.out.println("in class Tie, method show, this=" + this + ", component=" + component); System.out.println("Tie "); } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; public class Suits extends Finery { @Override public void show() { super.show(); System.out.println("in class Suits, method show, this=" + this + ", component=" + component); System.out.println("Suits "); } }
----
package com.study.javastudy.designpattern.decorator.dressingexample; public class Client { public static void main(String[] args) { //adam的换装 Person adam = new Person("adam"); Suits a = new Suits(); Tie b = new Tie(); LeatherShoes c = new LeatherShoes(); a.Decorate(adam); b.Decorate(a); c.Decorate(b); c.show(); System.out.println("\n--------------"); //bill的换装 Person bill = new Person("bill"); TShirts x = new TShirts(); Trouser y = new Trouser(); Sneakers z = new Sneakers(); x.Decorate(bill); y.Decorate(x); z.Decorate(y); z.show(); } }