Decorative design mode:
When you want to enhance the functionality of an existing object, you can customize the class, pass in existing objects, and provide enhanced functionality based on the functionality of the existing objects. Then the custom class is called the adornment class.
The adornment class usually receives the decorated object by constructing the method. It provides enhanced functionality based on the functionality of the object being decorated.
myreader//A class that is dedicated to reading data.
|--mytextreader
|--mybuffertextreader
|--mymediareader
|--mybuffermediareader
|--mydatareader
|--mybufferdatareader
Class Mybufferreader
{
Mybufferreader (mytextreader text)
{}
Mybufferreader (Mymediareader Media)
{}
}
The above class has very poor extensibility.
Find the common type of its parameters. Through polymorphic forms. can improve scalability.
Class Mybufferreader extends Myreader
{
Private myreader R;
Mybufferreader (myreader R)
{}
}
myreader//A class that is dedicated to reading data.
|--mytextreader
|--mymediareader
|--mydatareader
|--mybufferreader
Previously, each subclass was buffered through inheritance.
Then the inheritance system will be complex and not conducive to expansion.
Now optimize your mind. Describe the buffer content separately.
The object that will need to be buffered. Passed in. That is, who needs to be buffered, and who is passed as a parameter to the buffer.
So the inheritance system becomes very simple. Optimized the architecture.
Decoration mode is more flexible than inheritance. Avoid the bloated inheritance system.
It also reduces the relationship between classes and classes.
Decoration class because of the enhancement of existing objects, has the same functionality and existing objects are the same, but provides more powerful features.
So decorative and decorated classes are usually part of a system.
Demo:
classperson{ Public voidChifan () {System.out.println (Eat); }}classSuperperson {PrivatePerson p; Superperson (person p) { This. P =p; } Public voidSuperchifan () {System.out.println ("Aperitif"); P.chifan (); System.out.println (Dessert); System.out.println ("Take one."); }}classpersondemo{ Public Static voidMain (string[] args) {person P=NewPerson (); //P.chifan ();Superperson SP=NewSuperperson (P); Sp.superchifan (); }}
View Code
Decorative design pattern