A. Decoration mode:
1). Decorative mode is the ability to extend objects transparently in a client-side way, and is an alternative solution to an inherited relationship.
2). Decoration mode dynamically attaches more responsibility to an object in a transparent way to the client.
3). Decoration mode you can extend the functionality of an object without creating more subclasses.
Roles in decorating mode:
a). Abstract component Role (Component): An abstract interface is given to standardize the objects that are ready to receive additional responsibilities.
b). Specific component roles (concrete Component): Defines a class that will receive additional responsibilities.
c). Decorative Role (Decorator): holds a reference to a component (Component) object and defines an interface that is consistent with the abstract component interface.
d). Specific decorative roles (concrete Decorator): Responsible for attaching responsibilities to component objects.
Instance:
Abstract component Role (Component):
<strong>public interface Component {public void dosomething ();} </strong>
Specific component roles (concrete Component):
public class Concretecomponent implements component{@Overridepublic void DoSomething () {System.out.println ("function A");}}
Decorative character (Decorator):
public class Decorator implements Component{public Component component;public Decorator (Component Component) { This.component = component;} @Overridepublic void DoSomething () {//TODO auto-generated method Stubcomponent.dosomething ();}}
Specific decorative characters (concrete Decorator):
public class ConcreteDecorator1 extends Decorator{public ConcreteDecorator1 (Component Component) {super (Component);} public void DoSomething () {super.dosomething (); this.doanotherthing ();} public void doanotherthing () {System.out.println ("additional function B");}}
public class ConcreteDecorator2 extends Decorator{public ConcreteDecorator2 (Component Component) {super (Component);} public void DoSomething () {super.dosomething (); this.doanotherthing ();} public void doanotherthing () {System.out.println ("additional function C");}}
Two additional features.
Test class:
public class Test {public static void main (string[] args) {//This is the IO in which the node stream is filtered by the stream wrapper component com = new ConcreteDecorator2 (New Con CreteDecorator1 (New Concretecomponent ())); com.dosomething ();}}
Print:
Function A
Additional function B
Additional Functions C
Two. Characteristics of decoration Mode:
The decoration mode is often used in the IO stream mode, the characteristics of the decoration mode: 1. The decoration mode and the real object have the same interface. This allows the client object to interact with the adornment object in the same way as the real object. 2). The Adornment object contains a reference to a real object. 3). The adornment object receives all requests from the client, which forwards the requests to the real object. 4). Decorative objects can add additional functionality before or after forwarding these requests, thus ensuring that, at run time, additional functionality can be added externally without modifying the structure of a given object, and in the face of object design, inheritance is often implemented to extend the given functionality.
Java (26)-Decorative mode