[Design mode] study Note 3: decorator Mode)

Source: Internet
Author: User

From http://blog.csdn.net/shuangde800

Understanding the modifier Mode

When purchasing coffee at Starbucks, you can add a variety of spices as required, such as steamed milk, soy milk, Moka or covered milk. Starbucks charges different fees based on the added spices.

How to design this class?

In the modifier mode, we will use drinks as the main body, and then "Decorate" drinks with various spices during operation. For example, if a customer wants Moka and deep coffee with milk bubbles:

1. Take a cup of deep baked coffee as the subject object

2. decorate it with a Moka object

3. decorate it with a milk bubble object

4. Call the cost method to calculate the total price.

Define the decorator Mode

The modifier mode dynamically attaches the responsibility to the object. To expand functions, the modifier mode provides more flexible alternatives than inheritance.

Class Diagram

Java I/O in the decorator mode: In the I/O design in Java, the decoration mode bufferedinputstream and linenumberinputstream are extended from filterinputstream, while filterinputstream is an abstract decoration class. Decoration java. Io classThe design of "output stream" is the same. Write your own Java I/O decorators:Compile a modifier to convert all uppercase characters in the input stream into lowercase letters,
package headfirst.decorator.io;import java.io.*;public class LowerCaseInputStream extends FilterInputStream {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;}}

Test class:

package headfirst.decorator.io;import java.io.*;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();}}}
DisadvantagesHowever, Java I/O also leads to a "disadvantage" of the decorator mode: using the decorator mode, there are often a large number of small classes in the design, the number is too large, it may be a problem for programmers who use this API. However, after understanding how the decorator works, when using a large number of decorative APIs of others, it is easy to identify how their decorator classes are organized, to facilitate the use of packaging to obtain the desired behavior.

Design principles

Open-Close principle: the class should be open to expansion and closed to modification.

The purpose of the design is to allow classes to expand easily. New behaviors can be matched without modifying the existing code.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.