android设计模式二十三式(七)——装饰器模式(Decorator)

装饰器模式

所谓的装饰器,字面上的意思,就是对某个东西进行装饰,在原始的基础上,再添加上一些其他的功能,让整个效果更佳完善。所以,这里就有一个而基础功能,还有就是装饰功能。装饰模式的特点就是,装饰器和被装饰者都实现了同一个接口,装饰器中有被装饰者。

我们依旧来个小场景。假设我们有一个女朋友,今天你要带她出席一个很盛大的晚宴,所以她平时只会用水洗个头发,今天就得用上洗发液,护发素,弹力胶,最后还要造个型。

/**
 * @author: hx
 * @Time: 2019/5/8
 * @Description: GirlFriend
 */
public interface GirlFriend {
    /**
     * 所有的女朋友都会洗头发
     */
    void washHair();
}

/**
 * @author: hx
 * @Time: 2019/5/8
 * @Description: MyGirlFriend
 */
public class MyGirlFriend implements GirlFriend {
    @Override
    public void washHair() {
        System.out.println("用水洗头发");
    }
}

/**
 * @author: hx
 * @Time: 2019/5/8
 * @Description: Decorator 装饰器
 * 专门用来做头发的,来装饰一下我们的女朋友
 * 实现女朋友接口,所有的女朋友都会洗头嘛,就是洗头发要升级一下。
 */
public class Decorator implements GirlFriend {
    /**
     * 内部持有一个我们自己的女朋友
     */
    private GirlFriend mGirlFriend;

    public Decorator(GirlFriend girlFriend) {
        super();
        mGirlFriend = girlFriend;
    }

    @Override
    public void washHair() {
        System.out.println("加上洗发液");
        mGirlFriend.washHair();
        System.out.println("加上护发素");
        mGirlFriend.washHair();
        System.out.println("上弹力胶");
        System.out.println("造型");
    }
}

 来用一下这个装饰头发的新机器

public static void main(String[] args){
    GirlFriend girlFriend = new MyGirlFriend();
    Decorator decorator = new Decorator(girlFriend);
    decorator.washHair();
}

输出结果:
加上洗发液
用水洗头发
加上护发素
用水洗头发
上弹力胶
造型

我们看一下Decorator这个类,是不是很像对象的适配器模型,内部持有一个对象,将对象的进行处理,只不过我们没有对输出的接口做相应的规定。

装饰模式,在android的java.io中,使用的非常非常广泛,看一下源码

new BufferedOutputStream(new FileOutputStream()));
public class DataOutputStream extends FilterOutputStream implements DataOutput {
        

    /**
     * Creates a new data output stream to write data to the specified
     * underlying output stream. The counter written is
     * set to zero.
     *
     * @param   out   the underlying output stream, to be saved for later
     *                use.
     * @see     java.io.FilterOutputStream#out
     */
    public DataOutputStream(OutputStream out) {
        super(out);
    }

    /**
     * Writes a long to the underlying output stream as eight
     * bytes, high byte first. In no exception is thrown, the counter
     * written is incremented by 8.
     *
     * @param      v   a long to be written.
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FilterOutputStream#out
     */
    public final void writeLong(long v) throws IOException {
        writeBuffer[0] = (byte)(v >>> 56);
        writeBuffer[1] = (byte)(v >>> 48);
        writeBuffer[2] = (byte)(v >>> 40);
        writeBuffer[3] = (byte)(v >>> 32);
        writeBuffer[4] = (byte)(v >>> 24);
        writeBuffer[5] = (byte)(v >>> 16);
        writeBuffer[6] = (byte)(v >>>  8);
        writeBuffer[7] = (byte)(v >>>  0);
        out.write(writeBuffer, 0, 8);
        incCount(8);
    }
}

public class FilterOutputStream extends OutputStream {
    /**
     * The underlying output stream to be filtered.
     */
    protected OutputStream out;

    /**
     * Creates an output stream filter built on top of the specified
     * underlying output stream.
     *
     * @param   out   the underlying output stream to be assigned to
     *                the field this.out for later use, or
     *                null if this instance is to be
     *                created without an underlying stream.
     */
    public FilterOutputStream(OutputStream out) {
        this.out = out;
    }
}

装饰器模式的应用场景:

1、需要扩展一个类的功能。

2、动态的为一个对象增加功能,而且还能动态撤销。(继承不能做到这一点,继承的功能是静态的,不能动态增删。)

缺点:

产生过多相似的对象,不易排错!

你可能感兴趣的:(android,设计模式)