/ * * Dynamically add some additional duties to an object, just like a paint on the wall. It is more flexible to use the Decorator mode to achieve a function with a generated subclass.
Why use Decorator? We can usually use inheritance to implement functional expansion. If these needs to expand, it is necessary to generate a lot of subclasses, increase the complexity of the system, and use inheritance implementation, we must These expansion functions are foreseen, which is determined when compiling is static.
* / using system.collections;
Namespace pattern {///
PUBLIC CLASS Squarepeg: Work {public void insert () {system.console .writeline ("Square Pile Insert");}}
Public class decorator: Work {
Private work work; // Extra increasing features Packed in this list price arraylist = new arraylist ();
// Use a combination new method in the constructor to introduce a Work object; public decorator (Work Work) {this.work = work; er.add ("digging");
Others.Add ("Nail Machine");
Public void insert () {
Newmethod ();
/ / In a new method, we add other methods before INSERT. Here, the order is successfully designated by the user's flexible public (); work.insert ();
}
Public void othermethod () {ienumerator Listiterator = {system.movenext ()) {system.console .writeline ((String)) "Go");}
}
}
} // Decorative mode // Work Squarepeg = new Squarepeg (); // work decorator = new decorator (Squarepeg); // Decorator.insert ();