Java implementation Decorator (Decorator) mode

Source: Internet
Author: User

In Java, many of the classes under IO packages are typical of decorator patterns, such as:

New Bufferedoutputstream (OutputStream out)

New Bufferedinputstream (InputStream in);

New PrintWriter (OutputStream out)

New FilterReader (Reader in);


The decoration class implements the same interface as the decorated class,
Be decorated class, do not care specifically which implementation class to decorate it,
The same business method, decorated class calls the method of decoration class, enhance the function of decoration class

Example:

public interface Ireader {void read ();

public class Reader implements Ireader {@Overridepublic void read () {System.out.println ("read of Reader");}}

public class BufferedReader implements Ireader {private Ireader mreader;public BufferedReader (Ireader reader) { This.mreader = reader;} @Overridepublic void Read () {System.out.println ("read of BufferedReader"); Mreader.read ();}}

/* Features: * (1) The Decoration object and the real object have the same interface. This allows the client object to interact with the adornment object in the same way as the real object. (2) The Adornment object contains a reference to a real object (reference) (3) The Adornment object accepts all requests from the client. It forwards these requests to the real object. (4) Decorative objects can add additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance. And after the decoration, hold the real object, to enhance its function. The difference between decorator and adapter mode about new responsibilities: Adapters can also add new responsibilities at the time of conversion, but the primary purpose is not here. The decorator mode is mainly for the decorator to add new responsibilities. About the object of its package: The adapter is aware of the details of the person being adapted (that is, the appropriate class). The decorator knows only what its interface is, and whether its specific type (base class or other derived class) is known only during run time.  */public class Test {public static void main (string[] args) {Reader reader = new Reader (); Reader.read (); System.out.println ("----------"); BufferedReader BufferedReader = new BufferedReader (reader); Bufferedreader.read ();}}


Java implementation Decorator (Decorator) mode

Related Article

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.