/* Decorative mode is one of the structural design patterns that dynamically extends the functionality of an object without changing the class file and using inheritance, and is one of the alternatives to inheritance.
It wraps the real object by creating a wrapper object, which is the decoration.
Definition: To dynamically add some additional responsibilities to an object, the adornment pattern is more flexible than generating subclasses in terms of adding functionality.
In decorating mode, there are the following roles: Component: An abstract component, which can be an interface or abstract class, the most primitive object to be decorated. Concretecomponent: A component-specific implementation class.
The concrete realization class of component, the concrete object that is decorated. Decorator: Abstract decorator, from the outer class to expand the function of the component class, but for componenet without knowing the existence of Decorator.
There must be a private variable in its properties that points to the component abstract component.
Concretedecorator: The concrete implementation class of the decorator.
*/* Decorator mode Simple implementation decorator mode in real life, there are many examples, such as a person to wear all kinds of clothes, paint a picture, the frame and so on.
To give an example of martial arts cultivation martial arts: Yang over itself will be all true sword, there are seven male and Ouyang Fung to teach the dog stick law and toad work.
Hong Seven public and Ouyang Fung on the "decoration" Yang over the role.
*/1. Abstract components as a martial arts will definitely use martial arts, we first define a martial arts abstract class, there is the use of martial arts abstract method: Public abstract class Swordsman {/** * Swordsman Martial Arts have the use of martial arts abstract method.
*/public abstract void attackmagic (); } 2. The concrete object that the component realizes the class is decorated, here is the concrete martial arts which is imparted military study, namely Yang too. Yang will of course military study: public class Yangguo extends swordsman {@Override public void attackmagic () {//Yang over the initial military study is all true sword System.out.pr
Intln ("Yang has used all true sword"); }} 3. Abstract decorator The abstract decorator maintains a reference to the abstract component, facilitating the invocation of methods in the adorner object. In this case, it is military study's predecessor to hold a reference to martial arts, convenient to teach him military study and make him digest: Public abstract class Master extends Swordsman {privatE Swordsman Mswordsman;
Public Master (Swordsman mswordsman) {This.mswordsman = Mswordsman;
} @Override public void Attackmagic () {mswordsman.attackmagic (); }} 4. The decorator concrete implementation class This example uses two decorators to implement the class concretely, respectively is the flood seven male and the Ouyang Fung, they are responsible for teaches the new martial arts to the young, as follows: The public class HongQiGong extends Master {public Hongqigon
G (Swordsman Mswordsman) {super (Mswordsman);
} public void Teachattackmagic () {System.out.println ("Seven professor hung the Dog Stick Law");
System.out.println ("Yang used to beat the dog Stick Method");
} @Override public void Attackmagic () {super.attackmagic ();
Teachattackmagic ();
}} public class Ouyangfeng extends Master {public Ouyangfeng (swordsman Mswordsman) {super (Mswordsman);
} public void Teachattackmagic () {System.out.println ("Professor Ouyang Fung Toad Gong");
System.out.println ("Yang uses Toad Power");
} @Override public void Attackmagic () {super.attackmagic ();
Teachattackmagic ();
}} 5. Client call after flood seven public and Ouyang Fung Teaching, Yang had not only the true sword and learned to play dog stick law and toad work.
public class Client {public static void main (string[] args) {//create Yang over Yangguo Myangguo = new Yangguo (); Hong Seven publicTeach to play dog stick method, Yang has learned to play dog stick method HongQiGong Mhongqigong = new HongQiGong (Myangguo);
Mhongqigong.attackmagic ();
Ouyang Fung Teach Toad work, Yang had learned toad work Ouyangfeng Mouyangfeng = new Ouyangfeng (Myangguo);
Mouyangfeng.attackmagic ();
}}/* Decorator mode usage scenarios and pros and cons usage scenarios: Add responsibilities to individual objects in a dynamic, transparent manner without affecting other objects.
You need to dynamically add functionality to an object that can be removed dynamically.
When the system can not be extended or inherited in a way that is not conducive to system expansion and maintenance.
Pros: Dynamically extend the functionality of an object by combining rather than inheriting, selecting different adorners at run time to achieve different behaviors.
Effectively avoids the use of inheritance to extend the functionality of the object, resulting in a poor flexibility, the subclass unrestricted expansion of the problem.
The concrete component class and the concrete adornment class can change independently, the user may add the new concrete component class and the concrete adornment class according to the need, in the use and then the combination, the original code does not need to change, conforms to the open close principle. Disadvantage: Because all objects inherit from component, if the component internal structure changes, it inevitably affects all subclasses (decorators and decorators).
If the base class changes, it is bound to affect the interior of the object. More flexible than inheritance, it also means that the decoration mode is more error-prone than inheritance, and it is difficult to debug. For many decorative objects, debugging may need to troubleshoot the error, more cumbersome.
Therefore, only when necessary to use the decorative mode.
The number of decorative layers can not be too much, otherwise it will affect efficiency.
* * Learn more about the Advanced Android Light