Design Pattern 13-Decorator

xiaoxiao2021-03-06  63

/ * * 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 {///

/// Summary description for class1. /// public interface work {void insert ();

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 ();

转载请注明原文地址:https://www.9cbs.com/read-111834.html

New Post(0)