标签:
装饰模式的定义:
Attach additional responsibilities to an object dynamically keeping the same interface.Decorators provide a flexible alternative to subclassing for extending functionality.
动态给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。
结构类
多层的装饰是比较复杂,要尽量减少装饰类的数量,以便降低系统的复杂度
Component类—抽象构件
Component是一个接口或者是抽象类
ConcreteComponent 类—-具体构件
ConcreteComponent 是最核心,最原始,最基本的接口或抽象类的实现,你要装饰的就是它
Decorator —-装饰角色
一般是一个抽象类,实现接口或者抽象方法,它的属性里必然有一个private 变量指向Component抽象构件
ConcreteDecorator1 ConcreteDecorator2 —具体装饰角色
ConcreteDecorator1 ConcreteDecorator2 —具体装饰角色,把构件装饰成其它的东西。
Component类:
public abstract class Component {
public abstract void operate();
}
ConcreteComponent 类:
public class ConcreteComponent extends Component {
@Override
public void operate() {
// TODO Auto-generated method stub
System.out.println("ConcreteComponent---operate()");
}
}
Decorator 类:
public class Decorator extends Component {
private Component component;
public Decorator(Component component) {
super();
this.component = component;
}
@Override
public void operate() {
// TODO Auto-generated method stub
component.operate();
}
}
ConcreteDecorator1 类:
public class ConcreteDecorator1 extends Decorator {
public ConcreteDecorator1(Component component) {
super(component);
// TODO Auto-generated constructor stub
}
private void method1(){
System.out.println("ConcreteDecorator1---method1()");
}
@Override
public void operate() {
// TODO Auto-generated method stub
System.out.println("ConcreteDecorator1---operate()");
super.operate();
method1();
}
}
ConcreteDecorator2 类:
public class ConcreteDecorator2 extends Decorator {
public ConcreteDecorator2(Component component) {
super(component);
// TODO Auto-generated constructor stub
}
private void method2(){
System.out.println("ConcreteDecorator2---method2()");
}
@Override
public void operate() {
// TODO Auto-generated method stub
System.out.println("ConcreteDecorator2---operate()");
super.operate();
method2();
}
}
Client 类:
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Component component = new ConcreteComponent();
component = new ConcreteDecorator1(component);
component.operate();
System.out.println("-------------------------");
component = new ConcreteDecorator2(component);
component.operate();
}
}
ConcreteDecorator1---operate()
ConcreteComponent---operate()
ConcreteDecorator1---method1()
-------------------------
ConcreteDecorator2---operate()
ConcreteDecorator1---operate()
ConcreteComponent---operate()
ConcreteDecorator1---method1()
ConcreteDecorator2---method2()
样例:ToastingBurstFacadeDecorator
./packages/apps/Camera2/src/com/android/camera/burst/ToastingBurstFacadeDecorator.java
uml图:
简单分析:
//注释也明确说明这是一个装饰模式--A simple decorator
/**
* A simple decorator for a {@link BurstFacade} that shows toasts for when a
* burst starts or stops.
* <p>
* This class can simply be removed once we have proper UI for this.
*/
//类ToastingBurstFacadeDecorator 装饰的类就是BurstFacade,这也是is-a的实现
public class ToastingBurstFacadeDecorator implements BurstFacade {
//这就是BurstFacade类,要被装饰的对象
/** Shows burst-related toasts to the user. */
public static class BurstToaster {
private final Context mContext;
public BurstToaster(Context context) {
mContext = context;
}
public void showToastBurstStarted() {
Toast.makeText(mContext, MSG_BURST_STARTED, Toast.LENGTH_SHORT).show();
}
public void showToastBurstStopped() {
Toast.makeText(mContext, MSG_BURST_STOPPED, Toast.LENGTH_SHORT).show();
}
}
......
......
//这就是have-a的实现
private final BurstFacade mBurstFacade;
private final BurstToaster mToaster;
/**
* Initialize the toasting burst facade decorator.
*
* @param facadeToDecorate the facade to decorate.
* @param toaster the toaster to use to show toasts about the burst status.
*/
public ToastingBurstFacadeDecorator(BurstFacade facadeToDecorate, BurstToaster toaster) {
mBurstFacade = facadeToDecorate;
mToaster = toaster;
}
@Override
public void startBurst(CaptureSession.CaptureSessionCreator captureSessionCreator,
DeviceOrientation deviceOrientation, Facing cameraFacing, int imageOrientationDegrees) {
mToaster.showToastBurstStarted();
mBurstFacade.startBurst(captureSessionCreator, deviceOrientation, cameraFacing,
imageOrientationDegrees);
}
@Override
public boolean stopBurst() {
boolean burstStopped = mBurstFacade.stopBurst();
// Only show the toast if a burst was actually stopped.
if (burstStopped) {
mToaster.showToastBurstStopped();
}
return burstStopped;
}
@Override
public void initialize(SurfaceTexture surfaceTexture) {
mBurstFacade.initialize(surfaceTexture);
}
@Override
public void release() {
mBurstFacade.release();
}
@Override
public Surface getInputSurface() {
return mBurstFacade.getInputSurface();
}
@Override
public void setBurstTaker(BurstTaker burstTaker) {
mBurstFacade.setBurstTaker(burstTaker);
}
}
我们平时经常使用的是继承类和实现接口,这是一种is-a形式,
而我们另外也经常使用一个将对象以属性的形式包含到一个类中的have-a形式。
装饰模式就是一种have-a和is-a模式的二个结合。
至于装饰模式是怎么做到这二者的结合,类图就给了我们详细的解释。
(1).设计模式之禅—第17章 装饰模式
(2)装饰模式
https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/decorator/tiny-times
设计模式之装饰模式--- Pattern Decorator
标签:
原文地址:http://blog.csdn.net/hfreeman2008/article/details/52195188