Decorator pattern _java of Java design pattern series

Source: Internet
Author: User
Tags class definition inheritance

What is the decorator pattern (decorator)?
Dynamically add some additional responsibilities to an object. Decorator mode is more flexible than generating subclasses in terms of adding functionality.
first, the structure

Component: defines an object interface that can dynamically add responsibilities to these objects.

Interface Component {public
  void operation ();
}
Concretecomponent: Implements the interface defined by Component.
class Concretecomponent implements Component {
  @Override public
  void operation () {
    SYSTEM.OUT.PRINTLN ("initial behavior");
  }

Decorator: decorate the abstract class, inherits the Component, expands the Component class function from the outer class, but for Component, does not need to know the decorator existence.

Class Decorator implements Component {
  //holds a Component object, and Component forms an aggregation relationship protected Component
  >//incoming objects to be further decorated public
  decorator (Component Component) {
    this.component = Component;
  }
  
  @Override
  //calls the original method of the object to be modified public
  void operation () {
    component.operation ()}
}

Concretedecorator: A specific decorative object that plays a role in adding responsibilities to Component.

Class Concretedecoratora extends Decorator {
  private String addedstate = "new property 1";
  
  Public Concretedecoratora (Component Component) {
    super (Component);
  }
  
  public void operation () {
    super.operation ();
    SYSTEM.OUT.PRINTLN ("Add attribute:" + addedstate);
  }

Class Concretedecoratorb extends Decorator {public
  Concretedecoratorb (Component Component) {
    super ( component);
  }

  public void operation () {
    super.operation ();
    Addedbehavior ();
  }
  
  public void Addedbehavior () {
    System.out.println ("Add Behavior");
  }


Test code

public class Decoratorpattern {public
  static void Main (string[] args) {
    Component Component = new Concretecompone NT ();
    Component.operation ();
    
    System.out.println ("======================================");
    Decorator Decoratora = new Concretedecoratora (component);
    Decoratora.operation ();
    
    System.out.println ("======================================");
    Decorator Decoratorb = new Concretedecoratorb (Decoratora);
    Decoratorb.operation ();
  }

Run results

Initial behavior
======================================
Initial behavior
Add attribute: New Property 1
=============================== =======
Initial Behavior
Add Property: New Property 1
Add behavior

second, the application scene

1, the need for dynamic, transparent for an object to add responsibility, that is, does not affect other objects.
2, need to dynamically add functions to an object, these functions can be dynamically revoked.
3, the need to increase by a number of basic functions of the arrangement of a very large number of functions, so that the inheritance relationship becomes unrealistic.
4. When the method of generating subclasses cannot be expanded. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, making the number of subclasses explode. Another scenario might be because the class definition is hidden, or the class definition cannot be used to generate subclasses.

Iii. Main points

1, decorative objects and real objects have the same interface. This allows the client object to interact with the decorated object in the same way as the real object.

2. The decorative object contains a reference to a real object (reference).

3, decorate the object to accept 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. In object-oriented design, a function extension to a given class is usually implemented through inheritance.

The above is about the Java Decorator Model related content introduction, I hope to help you learn.

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.