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