Java design mode----Adorner mode

Source: Internet
Author: User

The decorator decorator, as the name implies, is dynamically adding some extra responsibilities to an object, just like decorating a house. Therefore, the adorner pattern has the following characteristics:

It must have an ornamental object.

It must have the same interface as the object being decorated.

It can add extra functionality to the object being decorated.

In a nutshell: Keep the interface, enhance performance.

Adorners extend their functionality by wrapping a decorative object without altering its interface, which is actually a variant of the object-based adapter pattern. The similarities and differences between it and the object's adapter pattern are as follows.

Same point: Both have a target object.

Different points: The adapter mode needs to implement an additional interface, and the adorner mode must implement the interface of the object.

Write an interface class first: sourcable

Package Model;public interface sourcable {public void opration ();

Then set up an implementation class: Source, overriding the method.

Package Model;public class Source implements Sourcable{public void Opration () {System.out.println ("original class Method");}}

Adorner classDecorator1.javaWith a typical object adapter pattern, it first has aSourcableObjectSource, the object is initialized by a constructor function. And then it implements theSourcable.java interface in order to maintain theSourceThe same interface, and in the overriddenOperation ()function is called in theSourceOfOperation () function, you can implement your own output before and after the call, which is the function that the adorner expands

Package Model;public class Decorator1 implements Sourcable{private sourcable sourcable;public Decorator1 (sourcable sourcable) {super (); this.sourcable=sourcable;} @Overridepublic void Opration () {System.out.println ("front of the first adorner"); Sourcable.opration (); SYSTEM.OUT.PRINTLN ("Rear of the first adorner");}}

Two more adorners to output different hintsDecorato2.java,decorator3.java

Package Model;public class Decorator2 implements Sourcable{private sourcable sourcable;public Decorator2 (sourcable sourcable) {super (); this.sourcable=sourcable;} @Overridepublic void Opration () {System.out.println ("front of the second adorner"); Sourcable.opration (); SYSTEM.OUT.PRINTLN ("Rear of second Adorner");}}

Package Model;public class Decorator3 implements Sourcable{private sourcable sourcable;public Decorator3 (sourcable sourcable) {super (); this.sourcable=sourcable;} @Overridepublic void Opration () {System.out.println ("front of the third adorner"); Sourcable.opration (); System.out.println ("After the third Adorner");}}

under test:

Package Model;public class Decoratortest {public static void main (string[] args) {sourcable source=new source ();//Decoration class object so Urcable sc=new Decorator1 (new Decorator2 (source)); Decorator3 ();}}

The output is:

First adorner before the second adorner before the third adorner before the original class method third adorner after the second adorner after the first adorner

Java design mode----Adorner mode

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.