Decorator mode in Design Mode

Source: Internet
Author: User

As the name suggests, the Decorator is to dynamically add some additional responsibilities to an object, just like decorating a house. Therefore, the decorator mode has the following features:

It must have a decoration object.

It must have the same interface as the decorated object.

It can add additional functions to the decorated object.

In one sentence, we will summarize: maintain interfaces and enhance performance.

The decorator extends its functions by encapsulating a decoration object without changing its interfaces. This is actually a variant of the object-based adapter mode. Its similarities and differences with the object's adapter mode are as follows.

Similarities: each object has a target object.

Difference: the adapter mode needs to implement another interface, and the decoration mode must implement the interface of this object.

The source code of the Sourcable class is shown in program 12-22, which defines an interface function operation ().

Program 12-22 source interface Sourcable. java

Java code
package pattern.decorator;public interface Sourcable {public void operation();}

(2) Source. java is an implementation of Sourcable. java. Its function operation () Outputs a string to the console: the method of the original class. The source code is shown in program 12-23.

Program 12-23 Source class Source. java

Package pattern. decorator; public class Source implements Sourcable {public void operation () {System. out. println ("original class method ");}}

(3) Decorator1.java adopts the typical Object Adapter mode. It first owns a Sourcable object source, which is initialized by the constructor. Then it implements Sourcable. java interface, in order to maintain the same interface as source, and call the source operation () function in the rewritten operation () function, you can implement your own output before and after the call, this is the extended function of the decorator. The source code is shown in the 12-24 program.

Program 12-24 Decorator1.java

Package pattern. decorator; public class Decorator1 implements Sourcable {private Sourcable sourcable; public Decorator1 (Sourcable sourcable) {super (); this. sourcable = sourcable;} public void operation () {System. out. println ("before the first decorator"); sourcable. operation (); System. out. println ("after the first ornament ");}}

The Decorator2.java class Decorator2.java is another decorator. The difference is that the decoration content is different, that is, different strings are output. The source code is shown in program 12-25.

Program 12-25 Decorator2.java

Package pattern. decorator; public class Decorator2 implements Sourcable {private Sourcable sourcable; public Decorator2 (Sourcable sourcable) {super (); this. sourcable = sourcable;} public void operation () {System. out. println ("before the second decorator"); sourcable. operation (); System. out. println ("after the second decorator ");}}

Decorator1.java is the third decorator. The difference is that the decoration content is different, that is, different strings are output. The source code is shown in program 12-26.

Program 12-26 Decorator3.java

Package pattern. decorator; public class Decorator3 implements Sourcable {private Sourcable sourcable; public Decorator3 (Sourcable sourcable) {super (); this. sourcable = sourcable;} public void operation () {System. out. println ("before the third modifier"); sourcable. operation (); System. out. println ("after the third ornament ");}}

In this case, we can use these decorators just like the object's adapter mode, and use different decorators to achieve different decorative effects. As shown in program 12-27, You need to first create a source Class Object source, and then use Decorator1 to decorate the object, and then use Decorator2 and Decorator3 for decoration, the decorated object has the same interface as source.

Program 12-27 test class DecoratorTest. java

Package pattern. decorator; public class DecoratorTest {/*** @ param args */public static void main (String [] args) {Sourcable source = new Source (); // decoration Class Object Sourcable obj = new Decorator1 (new Decorator2 (new Decorator3 (source); obj. operation ();}}

The output of running the program is as follows:

1st decorator front

2nd decorator front

3rd decorator front

Methods of the original Class

3rd decorator

2nd decorator

1st decorator

The output results show that the source class objects are decorated by Decorator1, Decorator2, and decorator3.

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.