Decorative design mode:When you want to enhance the functionality of an existing object, you can define a class, pass in an existing object, and, based on existing functionality,and provide enhanced functionality, then the custom class is called the adornment class. decorative classes typically receive decorated objects by constructing methods, and are based on the functions of the decorated objects, providing a strongerfunction. ///Example 1:
classperson{ Public voidChifan () {System.out.println (Eat); }}classsuperperson{PrivatePerson p; PublicSuperperson (person p) { This. P =p; } Public voidChifan () {System.out.println ("Aperitif"); P.chifan (); System.out.println (Dessert); System.out.println ("Smoke One."); }}classpersondemo{ Public Static voidMain (string[] args) {person P=NewPerson (); //P.chifan ();Superperson SP =NewSuperperson (P); Sp.chifan (); }}
the difference between decoration and inheritance:before the feature is enhanced:myreader//classes that are designed to read data. |--mytextreader|--mybufferedtextreader|--mymediareader|--mybufferedmedireader|--mydatareader|--mybuffereddatareaderFeature Enhancements:class Mybufferedreader{
Mybufferedreader (mytextreader text) {} Mybufferedreader (mymediareader media) {} Mybufferedreader (mydatareader data) {}
}//The above class has poor extensibility. find the common type of its parameters. In polymorphic form, scalability can be improved. class Mybufferedreader extends myreader{
Private myreader R; Mybufferedreader (myreader R) { }
}after the feature is enhanced:myreader//classes that are designed to read data. |--mytextreader|--mymediareader|--mydatareader|--mybufferreader decoration mode is more flexible than inheritance, and avoids 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 is the same, but provides more powerful features. So decorative and decorated classes are usually in the same individual system.
Java: Decorative Design Patterns