Decorative patterns of Java design patterns

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaoyanjun6/article/details/56488020

Preface

In fact, we can understand the adorner pattern in this way, and take the example of yourself, and you think of yourself as naked as the object of being decorated. Your shoes, your clothes, your coats, your watches, your hats and so on, are your decorations, and you and these decorations, are decorated and decorated with the relationship.

Example Show

Well, now we use the code to understand the concept.

First of all, we find that whether it's a naked person, or your shoes or hats, there are features that we call the Show method.

We define an interface, which has the function of showing, namely show (),

Package com.user;/** * Defines interface * @author T * */public interface Abstractperson {    //has a function of show    Void Show ();}

Should now define a naked self, the Me class

Package com.user;/** * Define a specific person that is the decorator * @author T * */public class Me implements Abstractperson {    @Override    publi c void Show () {        System.out.println ("Nothing to wear, I show nude");}    }

Below that definition, shoes, hats, watches and other decorations, etc. don't worry first, we should first define a shoe, hat, watch abstract parent class abstractclothes. In fact, the abstract parent class has a constructor, the parameters inside the constructor are abstract humans, and the usage here is ingenious, which is an essential step to achieve the decorative function.

Package com.user;/** * Definition Abstract Ornament * @author T * */public abstract class Abstractclothes implements Abstractperson {    abstr Actperson Abstractperson;    Public abstractclothes (Abstractperson abstractperson) {        This.abstractperson = Abstractperson;    }    @Override public    Void Show () {        abstractperson.show ();    }}

Below start definition, hat ornament, inheritance Abstractclothes class

Package com.user;/** * Hat Ornament * @author T * */public class Hat extends Abstractclothes {public    hat (Abstractperson Abst Ractperson) {        super (Abstractperson);    }        @Override public    Void Show () {        super.show ();        Say ();    }        public void Say () {        System.out.println ("I Show a Hat");}    }

Define shoe decoration class Shoes, Inherit Abstractclothes class

Package com.user;/** * Shoe Ornament * @author T * */public class Shoes extends Abstractclothes {public    Shoes (Abstractperson Abstractperson) {        super (Abstractperson);    }        @Override public    Void Show () {        super.show ();        Say ();    }        public void Say () {        System.out.println ("I show a pair of shoes");}    }

Create Test class tests

Package Com.user;public class Test {public    static void Main (string[] args) {        //created by decorator        me me = new Me ();        The naked man was decorated with a hat, with the ability to show a hat hat        hat = new Hat (me);        The man with the hat was decorated with shoes and had the ability to show the shoes        Shoes Shoes = new Shoes (hat);        Shoes.show ();    }}

Operation Result:

I showed naked, I showed a hat, I showed a pair of shoes.
class diagram for adorner mode

After studying a small example, we try to summarize the class diagram of the adorner pattern.

Adorner Pattern class diagram:

    • Component Abstract component roles: real objects and decorative objects have the same interface. This allows the client object to interact with the adornment object in the same way as the real object.
    • Concretecompoent Concrete Build role (Real object): Defines a class that will receive additional responsibilities.
    • Decorator Decorative Role: holds a reference to an abstract artifact. The adornment object accepts requests from all clients and forwards those requests to the real object. In this way, you can add new functionality before and after the real object is called.
    • Concretedecorate Specific Decorative role: responsible for adding new functions to the component objects.

Application of decoration mode in Java I/O library

IO Stream Implementation Details:

    • Component Abstract Component role: Inputstream,outputstream,reader,writer in IO stream
    • Concretecomponent specific component roles: Fileinputstream,fileoutputstream in IO streams
    • Decorate decorative role: holding the reference to abstract artifacts, filterinputstream,filteroutputstream
    • Concretedecorate Specific Decorative roles: responsible for adding new responsibilities to component objects, Bufferedinputstream,bufferedoutputstream, etc.

Advantages
    • Extended object functionality is more flexible than inheritance and does not result in a dramatic increase in the number of classes.
    • You can decorate an object multiple times, creating a combination of different behaviors and getting more powerful objects.
    • The concrete component class and the concrete adornment class can change independently, the user can add the new concrete component subclass and the concrete adornment subclass according to the need.

Disadvantages
    • produce many small objects. A large number of small objects occupy memory and, to a certain extent, affect performance.
    • Decoration mode error-prone, debugging and troubleshooting more trouble.
Summarize
    • Decoration mode (decorate) is also called packing mode (Wrapper)
    • The adornment mode reduces the coupling degree of the system, can dynamically increase or remove the responsibility of the object, and makes the concrete construction class and the concrete adornment class which need adornment can change independently, in order to add the new concrete construction class and the concrete adornment class.

Decorative patterns of Java design patterns

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.