Design patterns (3) - the decorator pattern

Decorator (Decorator) mode dynamically responsibility attached to an object, to extend the functionality Decorators provide a flexible alternative to the more than inherited.

  • Decorator and decorated objects have the same super type.
  • You can wrap an object with one or more decorators.
  • Since the decorator and the decorator objects have the same supertype, so any desired original object (packaged) in the case, the object can be decorated with it in place.
  • Decoration can be decorated in the behavior of those who commissioned before and after, with their own behavior in order to achieve a specific purpose.
  • Objects can be decorated at any time, so you can dynamically at run-time, limited quantity to decorate objects with the decorators you like.

Decorator class diagram as follows:

 

The code simulates the cafe menu. Beverages Beverage procedure to class as the base class, adding different spices to the beverage will become a different coffees, add seasoning is the decorator pattern.

In addition, Java I / O source code is also used to achieve a decorator mode, which is a decorator pattern I use a custom IOStream class, the input stream all uppercase characters into lowercase.

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class LowerCaseInputStream extends FilterInputStream {
//JavaI/O设计时使用了装饰者模式,编写自己的JavaI/O装饰者,把输入流的所有大写字符换成小写
    public LowerCaseInputStream(InputStream in) {
        super(in);
    }

    public int read() throws IOException {
        int c = super.read();
        return (c == -1 ? c : Character.toLowerCase((char)c));
    }

    public int read(byte[] b, int offset, int len) throws IOException {
        int result = super.read(b, offset, len);
        for (int i = offset; i < offset + result; i++) {
            b[i] = (byte)Character.toLowerCase((char)b[i]);
        }
        return result;
    }
}
import com.crow.JavaIO.LowerCaseInputStream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/** * Created by CrowHawk on 17/7/10. */
public class InputTest {
    public static void main(String[] args) throws IOException {
        int c;
        try {
            InputStream in = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream("test.txt")));

            while((c = in.read()) >= 0) {
                System.out.print((char)c);
            }

            in.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Decorator Pattern uses our fifth design principles: class should be open for extension, but closed for modification . Our goal is to allow easy extension class, without modifying existing code, you can with the new behavior. This design has flexible and can respond to change, you can accept the new features to respond to changes in demand.

Published 295 original articles · won praise 37 · views 30000 +

Guess you like

Origin blog.csdn.net/tianshan2010/article/details/104705954