A detailed introduction to the decorative mode of Java design pattern _java

Source: Internet
Author: User

1. Decoration mode (decorator) definition: Also known as packaging (wrapper) mode, decorative mode to the client transparent way to extend the function of the object is an inheritance of the relationship of an alternative.

2. Decoration mode dynamically attaches more responsibility to an object in a transparent manner to the client. In other words, the client does not feel the object before and after decoration what is the difference.

3. The decorative pattern can extend the function of an object without creating more subclasses.

4. The difference between decoration mode and class inheritance:

1 decoration mode is a dynamic behavior, the existing classes are randomly combined, and class inheritance is a static behavior, a class definition of what kind, the object of the class will have what kind of function, cannot change dynamically.

2 The decorative mode extends the function of the object, does not need to increase the number of classes, and class inheritance extension is the function of the class, in the inherited relationship, if we want to add the function of an object, we can only through the inheritance relationship, in the subclass to add two methods.

3) Decoration and inheritance comparison chart:

4 The decoration mode is to extend the function of an object dynamically, without changing the original class file and using inheritance, it is to wrap the object by creating a wrapper object, i.e. decoration.

5. Decoration mode delegates the call to the client to the decorated class, and the key to the decorative pattern is that the extension is completely transparent.

6. The composition of the decoration pattern:

1 Abstract build Role (Component): gives an abstract interface to standardize the preparation of objects that accept additional responsibility. Equivalent to the I/O stream inside Inputstream/outputstream and Reader/writer.

2 The specific build role (Concretecomponent): Define a class that will accept additional responsibilities. Equivalent to I/O inside FileOutputStream and FileInputStream.

3) Decorative Role (Docorator): holds a reference to an abstract build (Component) role and defines an interface that is consistent with the abstract artifacts. Equivalent to I/O inside Fileroutputstream and FilterInputStream.

4 The specific decorative role (Concretedecorator): Responsible for the construction object "affixed" additional responsibilities. Equivalent to the Bufferedoutputstream and Bufferedinputstream in the I/O stream as well as DataOutputStream and Datainputsrtream.

7. Characteristics of Decoration Mode:

1 The decorative object and the real object have the same interface, so that the client object can interact with the decorative object in the same way as the real object.

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

3 The Decoration object accepts all requests from the client, and it forwards these requests to the real object.

4) Decorative objects can add some additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the given object structure at run time. In object-oriented programming, an inherited relationship is typically used to extend the functionality of a given class.

8. Case:

1 Abstract Building Interface:

Copy Code code as follows:

Packagecom.abao.decorate;

public interface Component

{

public void dosomething ();

}


2) Specific building roles:
Copy Code code as follows:

Packagecom.abao.decorate;

public class Concretecomponent implements Component

{

@Override

public void DoSomething ()

{

System.out.println ("function A");

}

}


3) Decoration Role:
Copy Code code as follows:

Packagecom.abao.decorate;

public class decorate implements Component

{

Private Component Component;

Public decorate (Component Component)

{

This.component = component;

}

@Override

public void DoSomething ()

{

Component.dosomething ();

}

}


4) Specific Decoration role 1:
Copy Code code as follows:

Packagecom.abao.decorate;

public class ConcreteDecorate1 extends decorate

{

Public ConcreteDecorate1 (Component Component)

{

Super (component);

}

@Override

public void DoSomething ()

{

Super.dosomething ();

This.doanotherdosomething ();

}

private void Doanotherdosomething ()

{

System.out.println ("function B");

}

}


5) Specific Decoration role 2:
Copy Code code as follows:

Packagecom.abao.decorate;

public class ConcreteDecorate2 extends decorate

{

Public ConcreteDecorate2 (Component Component)

{

Super (component);

}

@Override

public void DoSomething ()

{

Super.dosomething ();

This.doanotherdosomething ();

}

private void Doanotherdosomething ()

{

System.out.println ("function C");

}

}


6) Client
Copy Code code as follows:

Packagecom.abao.decorate;

public class Client

{

public static void Main (string[] args)

{

Component Component = new ConcreteDecorate1 (

New ConcreteDecorate2 (New Concretecomponent ());

Component.dosomething ();

}

}


9. Finish!

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.