Design Pattern _ decoration pattern
Decorator PatternAttach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality. (add some additional responsibilities to an object dynamically. The decoration mode is more flexible than the subclass generation)
Unfamiliar WordsDynamically adv dynamic
Alternative n available transactions
Functionality n function
ExampleWhen I was a child, I handed my transcript to my father. I had 60 maths: 68 nature: 87. I want to know if I didn't get this score in elementary school. It's impossible to ask my parents to sign the score, so I need to describe it, that is to say, this exam has a maximum of 70 Chinese languages, a maximum of 70 mathematics, and a maximum of 100 mathematics. After I finish the exam, I will add a sentence to my ranking, and so on ..... That is to say, some people may directly use subclass inheritance to write the code based on the original two functions, but this is not conducive to multiple functions. In case of changes, the principle of opening and closing is followed, how can I implement the change based on the original one?
public abstract class SchoolReport { public abstract void report(); public abstract void sign(String name);}
Public class FourthGradeSchoolReport extends SchoolReport {@ Override public void report () {System. out. println (xxx); System. out. println (...............); system. out. println (Language: 60 mathematics: 68 nature: 87);} public void sign (String name) {System. out. println (parent signature: + name );}}
Start Modification
public abstract class Decorator extends SchoolReport { private SchoolReport sr; public Decorator(SchoolReport sr){ this.sr=sr; } @Override public void report() { this.sr.report(); } @Override public void sign(String name) { this.sr.sign(name); }}Decorator Class Analysis1. Why inherit SchoolReport? If it is not inherited, the following modifier class inherits and has a SchoolReport member. In this way, each modifier requires two methods to be implemented: super. xxx (), so the amount of code is increased. 2. Why do I need a SchoolReport member? If not, it cannot be passed to implement multi-layer packaging. OK. After this is done, the circle is drawn according to the template.
Public class HighScoreDecorator extends Decorator {public HighScoreDecorator (SchoolReport sr) {super (sr);} private void reportHighScore () {System. out. println (the maximum number of Chinese characters in this exam is 70, the highest number of mathematics is 70, and the highest is 100);} @ Override public void report () {this. reportHighScore (); super. report ();}}
Public class SortDecorator extends Decorator {public SortDecorator (SchoolReport sr) {super (sr);} private void reportSort () {System. out. println (my ranking is 38);} @ Override public void report () {super. report (); this. reportSort ();}}
Test
Public static void main (String [] args) {SchoolReport sr; sr = new FourthGradeSchoolReport (); sr = new HighScoreDecorator (sr); sr = new SortDecorator (sr); sr. report (); sr. sign (Dad); // This makes Dad happy to sign}
WuTo make full use of inheritance, you must understand the meaning of rewriting, which is not a problem. Generally, this should not be done much.
AdvantagesDynamic scaling is not coupled
DisadvantagesIf there is a problem with the modification in the innermost part, it will be a lot of pain. So try to reduce the number of modifier classes when using them, otherwise...
Use CasesThe extended class function is dynamically added. The class function is modified or installed for the sibling class.
I'm a cainiao. I'm on the road.