The application of decorator pattern in Java design pattern programming is illustrated by examples _java

Source: Internet
Author: User
Tags inheritance

Concept

The decorator pattern dynamically attaches responsibility to the object. To extend functionality, adorners provide a more flexible alternative than inheritance.

The adorner and the decorated object have the same super type.
You can wrap an object with one or more adorners.
Since the adorner has the same super type as the decorated object, it can be replaced with a decorated object in any situation where the original object (being wrapped) is required.
The decorator may, before and/or after the behavior of the person entrusted to be decorated, add his or her own actions to achieve a specific purpose.
Objects can be decorated at any time, so you can decorate them dynamically, without limits, with your favorite decorators at run time.
Object.

In Java, many of the classes under IO packages are the embodiment of a typical decorator pattern, 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,
is decorated class, does not care about specifically which implementation class to decorate it,
The same business method, the decorated class calls the method of the adornment class, enhances the function of the adornment class

Instance:
class Diagram

 public interface Ireader {void read (); public class Reader implements Ireader {@Override public void Read () {System.out.println ("read of Reade 
  R "); 
  } public class BufferedReader implements Ireader {private Ireader mreader; 
  Public BufferedReader (Ireader reader) {this.mreader = reader; 
    @Override public void Read () {System.out.println ("read of BufferedReader"); 
  Mreader.read (); 
    } 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 (); } 
} 

Features:
1. Decorative objects have the same interface as real objects. This allows the client object to interact with the decorated object in the same way as the real object.
2. A decorative 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, at run time, additional functionality can be added externally without modifying the structure of the given object. &NBSP
in object-oriented design, the extension of a feature to a given class is usually implemented through inheritance. And after the decoration, holds the real object, enhances its function. &NBSP
 
Adorner to adapter mode:
about new responsibilities: Adapters can also add new responsibilities when converting, but the main purpose is not here. The decorator pattern is mainly to add new responsibilities to the person being decorated.
The object about the package: The adapter is the details of the person who is being matched (that is, the Fit Class). The adorner knows only what its interface is,
as to its specific type (a base class or other derived class) that is known only at run time.

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.