Easy to master Java decorator pattern _java

Source: Internet
Author: User
Tags inheritance

definition: dynamically extend the functionality of an object without changing the original class file and using inheritance. It is by creating a wrapper object that is decorated to wrap the real object.

Characteristics:

(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) The decorated 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. In object-oriented design, a function extension to a given class is usually implemented through inheritance.

Enterprise-level development and applications in common frameworks: IO stream underlying architecture

Composition

(1) the abstract widget (Component) role: gives an abstract interface to the specification of the object to receive the additional responsibility.
(2) The specific widget (concrete Component) Role: Defines a class that will receive additional responsibility.
(3) decorative (decorator) Role: Holds an instance of a component (Component) object and implements an interface that is consistent with the interface of an abstract component.
(4) The specific decoration (concrete decorator) role: Responsible for adding additional responsibilities to the component objects.

Concrete Examples:

/** * Following decorative house examples/public class Demo {public static void main (string[] args) {Gene
 Richouse House = new Generichouse ();
 Garage garage = new Garage (house);
 Garage.dosomething ();
 Kitchen Kitchen = new Kitchen (house);
 Kitchen.dosomething ();

}/** * Abstract widget role: an interface used to standardize the placement of specific decorations * * Interface abstracthouse{public void dosomething ();}

 /** * Decoration Role: Holding the object to decorate/class Master implements abstracthouse{private Abstracthouse abstracthouse;
 Public Master (Abstracthouse abstracthouse) {this.abstracthouse = Abstracthouse;
 public void DoSomething () {System.out.println ("Decoration Role: Decoration role holder, here is the owner of the House");
 Abstracthouse.dosomething (); }/** * Specific widget roles: specific objects to be decorated/class Generichouse implements abstracthouse{public void DoSomething () {System.ou T.PRINTLN ("Concrete building role: The house can live, shelter!")
 "); }/** * Specific decorative role: The specific decoration is done here, it inherits from the decorative role, because the decorative role holds the decorated object, * so it also holds the decorated object, can do decoration/class garage extends master{public Garage (Abstracthouse abstracthouse) {sUper (Abstracthouse);
 public void DoSomething () {super.dosomething ();
 SYSTEM.OUT.PRINTLN ("Specific decorative role: The decoration here, the house is decorated in a garage, so the house can store the car"); }/** * Specific decorative role: The specific decoration is done here, it inherits from the decorative role, because the decorative role holds the decorated object, * so it also holds the decorated object, can do decoration/class Kitchen extends master{public
 Kitchen (Abstracthouse abstracthouse) {super (abstracthouse);
 public void DoSomething () {super.dosomething ();
 SYSTEM.OUT.PRINTLN ("Specific decorative role: here to do the decoration thing, the house in a decorated kitchen, so the house can Cook"); }
}

In the actual development, decorative pattern packaging role has a great effect, we can not change the original object, the object to do some other operations, so that we can avoid the transformation of objects, but at the same time we can do a good job to complete some operations.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.