标签:继承 static src 参考 扩展 ted gif list out
装饰模式 |
其通用类图源码如下:
public abstract class Component { public abstract void doSomething(); } public class ConcreteComponent extends Component{ @Override public void doSomething() { // TODO Auto-generated method stub System.out.println("this is a ConcreteComponent"); } } public abstract class Decrator extends Component{ private Component component = null; public Decrator(Component com){ this.component = com; } @Override public void doSomething(){ this.component.doSomething(); } } public class ConcreteDecrator1 extends Decrator{ public ConcreteDecrator1(Component com) { super(com); // TODO Auto-generated constructor stub } public void doSomething() { // TODO Auto-generated method stub this.method1(); super.doSomething(); } private void method1(){ System.out.println("this is a ConcreteDecrator"); } } public class ConcreteDecrator2 extends Decrator{ public ConcreteDecrator2(Component com) { super(com); // TODO Auto-generated constructor stub } public void doSomething(){ super.doSomething(); this.method2(); } private void method2(){ System.out.println("this is another ConcreteDecrator"); } } public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Component component = new ConcreteComponent(); component = new ConcreteDecrator1(component); component = new ConcreteDecrator2(component); component.doSomething(); } }
装饰模式的应用 |
最近刚刚过了双十一的网上购物狂潮,购买的商品也万里迢迢的飞奔而来,拿到快递那一刻的喜悦简直感觉自己就是世界上最幸福的,然而如果你买的是服装,相信你一定深有体会,为什么自己穿上就没有那种感觉了呢,冥思苦想,应该是商家对服装的装饰比较到位,下面我们就解析一下这种“差距”。
public abstract class Clothes { //展示服装信息 public abstract void parameter(); } public class Coat extends Clothes{ @Override public void parameter() { // TODO Auto-generated method stub System.out.println("this is a Coat"); System.out.println("It‘s size is XL"); System.out.println("It‘s color is dark blue"); } } public abstract class Decrator extends Clothes{ private Clothes clothes = null; public Decrator(Clothes clothes){ this.clothes = clothes; } @Override public void parameter(){ this.clothes.parameter(); } } public class Model extends Decrator{ public Model(Clothes clothes) { super(clothes); // TODO Auto-generated constructor stub } public void parameter(){ this.method2(); super.parameter(); } private void method2(){ System.out.println("A clothes model wears the coat"); } } public class Scarf extends Decrator{ public Scarf(Clothes clothes) { super(clothes); // TODO Auto-generated constructor stub } public void parameter() { // TODO Auto-generated method stub this.method1(); super.parameter(); } private void method1(){ System.out.println("this is a scarf to decrate the coat"); } } public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Clothes clothes = new Coat(); clothes = new Scarf(clothes); clothes = new Model(clothes); clothes.parameter(); } }
可怜了我们这些小白用户,不知道还能这么玩,蓝瘦香菇。
装饰模式的优点 |
装饰模式的缺点 |
标签:继承 static src 参考 扩展 ted gif list out
原文地址:http://www.cnblogs.com/zhanglei93/p/6075727.html