Decorator mode
1. Intention: to dynamically add some additional responsibilities to an object. For added functionality, the decorator mode is more flexible than generating subclasses. This mode extends the functionality of the object in a transparent manner to the client.
2. Applicable environment
(1) Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects.
(2) to deal with duties that can be undone.
(3) When a method of generating subclasses cannot be used to augment. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
3. Participants
1.Component (base class of objects being decorated)
Defines an object interface that can dynamically add responsibilities to these objects.
2.ConcreteComponent (specifically decorated objects)
Defines an object that can add some responsibility to the object.
3.Decorator (Decorator abstract Class)
Maintains a reference to the component instance and defines an interface that is consistent with the component interface.
4.ConcreteDecorator (concrete decorator)
Specific decorative objects, to be held within the specific decorative objects, to increase the specific responsibilities.
4. Class Diagram
5. Role-related
(1) Abstract components: Define an abstract interface to standardize the classes that prepare additional functionality
(2) Specific components: Classes that will be added to the function, implementing an abstract component role interface
(3) Abstract decorator: holds a reference to a specific component role and defines an interface that is consistent with the abstract component role
(4) Specific decoration: to achieve the role of abstract decorator, responsible for the specific components to add additional functionality.
6. Code
Component
Public Interface Person { void eat ();}
Concretecomponent
Public class Implements Person { publicvoid eat () { System.out.println ("Man is Eating");} }
Decorator
Public Abstract class Implements Person { protected person person ; Public void Setperson (person person) { this. Person = person ; } Public void eat () { person.eat (); }}
Concretedectrator
Public classMandecoratoraextendsDecorator { Public voideat () {Super. Eat (); Reeat (); System.out.println ("Mandecoratora class"); } Public voidreeat () {System.out.println ("Have another meal."); }} Public classMandecoratorbextendsDecorator { Public voideat () {Super. Eat (); System.out.println ("==============="); System.out.println ("Mandecoratorb class"); }}
Test
class Test { public static void main (string[] args) {man ma n = new man (); Mandecoratora md1 = Mandecoratora (); Mandecoratorb md2 = Mandecoratorb (); Md1.setperson (man); Md2.setperson (MD1); Md2.eat (); }}
7, decorator Mode Summary:
OO Principle: Dynamically attaches responsibility to an object. To extend functionality, decorators provide an alternative to inheritance.
8. Key points:
1, inheritance is one of the extension forms, but it is not necessarily the best solution to achieve elastic design.
2, in our design, the behavior should be allowed to be extended without the need to modify the existing code.
3, combinations, and delegates can be used to dynamically add new behavior at run time.
4, in addition to inheritance, decorator mode also allows us to expand behavior.
5, decorator mode means a group of decorator classes, which are used to package specific components.
6, the Decorator class reflects the type of component being decorated (in fact, they have the same type,are implemented through interfaces or inheritance).
7, the decorator can be in front of the behavior of the decorator and / or after adding their own behavior, or even be the adornment of the entire act to replace the behavior, and achieve a specific purpose.
8. You can have an unlimited number of decorators packaging a component.
9, decorator-like for the formation of the customer is transparent generally to the formation of the customer is transparent, unless the client program depends on the specific type of component.
Java Mode-Decorator mode