Definition: Dynamically extend the functionality of an object without having to change the original file and use inheritance. It wraps the real object by creating a wrapper object, that is, decoration.
Features:
(1) Decorative objects and real objects have the same interface. This way 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
(3) The decorative object accepts all requests from the client. It forwards these requests to the real object.
(4) Decorative objects can add some additional functions before or after forwarding these requests. This ensures that at runtime, additional functions can be added externally without modifying the structure of a given object. In object-oriented design, functional extensions to a given class are usually achieved through inheritance.
Applications in enterprise-level development and commonly used frameworks: IO streaming underlying architecture
composition:
(1) Abstract component role: give an abstract interface to regulate objects that are ready to receive additional responsibilities.
(2) Concrete Component role: define a class that will receive additional responsibilities.
(3) Decorator role: holds an instance of a component object and implements an interface consistent with the abstract component interface.
(4) Concrete Decorator role: responsible for adding additional responsibilities to component objects.
Specific examples:
/** * The following examples of decorating houses*/public class Demo { public static void main(String[] args) { GenericHouse house = new GenericHouse(); Garage garage = new Garage(house); garage.doSomething(); Kitchen kitchen = new Kitchen(house); kitchen.doSomething(); }}/** * Abstract component role: an interface to regulate the location of specific decoration*/interface AbstractHouse{ public void doSomething();}/** * Decorative role: Hold the object to be decorated*/class Master implements AbstractHouse{ private AbstractHouse abstractHouse; public Master(AbstractHouse abstractHouse) { this.abstractHouse = abstractHouse; } public void doSomething() { System.out.println("Decorative role: Decorative role holder, here is the owner of the house"); abstractHouse.doSomething(); } }/** * Specific component role: specific object that needs to be decorated*/class GenericHouse implements AbstractHouse{ public void doSomething() { System.out.println("Specific constructor role: the house can live in people and shelter from wind and rain!"); }}/** * Specific decorative role: The specific decoration is made here, and it inherits from the decorative role. Because the decorative role holds the decorative object, * So it also holds the decorative object and can be decorated*/class Garage extends Master{ public Garage(AbstractHouse abstractHouse) { super(abstractHouse); } public void doSomething() { super.doSomething(); System.out.println("Specific decorative role: do decoration here, decorate one of the house into a garage, so the house can store the car"); }}/** * Specific decorative role: The specific decoration is made here, it inherits from the decorative role, because the decorative role holds the decorative object, * So it also holds the decorative object and can be decorated*/class Kitchen extends Master{ public Kitchen(AbstractHouse abstractHouse) { super(abstractHouse); } public void doSomething() { super.doSomething(); System.out.println("Specific decorative role: do decoration here, decorate one of the house into a kitchen, so the house can cook"); }}In actual development, the packaging function of decorating patterns plays a great role. We can do some other operations on the object without changing the original object, which can prevent us from transforming the object, but at the same time we can complete some operations well.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.