标签:
动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。
动态地给一个对象添加一些额外的职责。
public abstract class Component {
    /**
     * 抽象的方法
     */
    public abstract void operation();
}
抽象组件类里面只有一个抽象方法operation()
public class ConcreteComponent extends Component {
    @Override
    public void operation() {
    }
}
组件具体实现类继承自组建抽象类,在operation方法里面有具体的实现逻辑。
public class Decorator extends Component {
    private Component component;//持有一个component对象的引用
    public Decorator(Component component) {
        this.component = component;
    }
    @Override
    public void operation() {
        component.operation();
    }
}
抽象装饰者类也继承自Component类,在构造函数里面接收一个Component类型的Component对象的引用,在operation()方法里面调用component的operation方法。
public class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }
    @Override
    public void operation() {
        opetateA();
        super.operation();
        opetateB();
    }
    private void opetateB() {
    }
    private void opetateA() {
    }
}   
装饰者实现类里面,继承自Decorator类,在operation方法里面会调用父类operation方法,也可以自己进行相关逻辑操作。
public class Client {
    public static void main(String[] args) {
        //构造被装饰的组件对象
        ConcreteComponent concreteComponent = new ConcreteComponent();
        //构造装饰者对象A,并调用
        ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(concreteComponent);
        concreteDecoratorA.operation();
        System.out.println("----");
        //构造装饰者对象B,并调用
        ConcreteDecoratorA concreteDecoratorB = new ConcreteDecoratorA(concreteComponent);
        concreteDecoratorB.operation();
    }
}
为了让大家更好的理解装饰者设计模式,现在有如下场景:
比如我们现在要去买一份炒饭,如果什么都不加的话,只要五元;加青椒的话,多收五元;加肉丝的话多收三元。
public abstract class Rice {
    private String name;
    public String getName() {
        return name;
    }
    public abstract int getPrice();
}
米饭基类里面有一个名称和一个价格:
public class FryRice extends Rice {
    public FryRice() {
    }
    @Override
    public String getName() {
        return "炒饭";
    }
    @Override
    public int getPrice() {
        return 5;
    }
}
炒饭返回的价格是5元
public class Ingredient extends Rice {
    private Rice rice;
    public Ingredient(Rice rice) {
        this.rice = rice;
    }
    @Override
    public String getName() {
        return rice.getName();
    }
    @Override
    public int getPrice() {
        return rice.getPrice();
    }
}
public class Ham extends Ingredient {
    public Ham(Rice rice) {
        super(rice);
    }
    @Override
    public String getName() {
        return super.getName() + ",加火腿";
    }
    @Override
    public int getPrice() {
        return super.getPrice() + 3;
    }
}
public class Lean extends Ingredient {
    public Lean(Rice rice) {
        super(rice);
    }
    @Override
    public String getName() {
        return super.getName() + ",加瘦肉";
    }
    @Override
    public int getPrice() {
        return super.getPrice() + 4;
    }
}
public class Client {
    public static void main(String[] args) {
        //炒饭基类
        FryRice fryRice = new FryRice();
        System.out.println(fryRice.getName() + "," + fryRice.getPrice());
        //瘦肉炒饭基类
        Lean leanFryRice = new Lean(fryRice);
        System.out.println(leanFryRice.getName() + "," + leanFryRice.getPrice());
        //瘦肉火腿炒饭
        Ham ham = new Ham(leanFryRice);
        System.out.println(ham.getName() + "," + ham.getPrice());
    }
}
测试结果如下:
    炒饭,5
    炒饭,加瘦肉,9
    炒饭,加瘦肉,加火腿,12
在Android中,我们会经常在Activity里面使用startActivity()方法启动一个组件。
startActivity这个方法最开始在Context中定义,Context是一个抽象类,里面定义了许多抽象方法。Context相当于装饰设计模式里面的抽象组件。
startActivity具体实现在ContextImpl中完成的。ContextImpl相当于组件的具体实现。
Activity继承自ContextThemeWrapper,ContextWrapper又继承自Context。
在ContextWrapper里面有如下代码:
Context mBase;
public ContextWrapper(Context base) {
    mBase = base;
}
@Override
public void startActivity(Intent intent) {
    mBase.startActivity(intent);
}
可见这里的ContextWrapper就是我们装饰者。
最后给一下几个类的关系:
标签:
原文地址:http://blog.csdn.net/u010649376/article/details/51348235