Design Pattern note 9 (modifier pattern)

Source: Internet
Author: User

Decoration Mode

The modifier mode dynamically adds some responsibilities, methods, and interfaces to an object.

We still need to understand this pattern in practical applications.

Some time ago, because of our huge success in the market, our profitability has been greatly improved, so our bosses no longer share the female Secretary:

Class nvmishu {public void fuwu () {system. Out. println ("service ");}}

After a while, a boss was no longer satisfied with the monotonous service of the female secretary. He also wanted services like sailors. The people in the Secretariat have the flexibility to recruit new secretaries because of this, but we can buy a sailor suit to put on this Secretary:

Class nvmishu {public void fuwu () {system. out. println ("service") ;}} class shuishou {private nvmishu NMS; Public shuishou (nvmishu NmS) {This. NMS = NMS;} public void fuwu () {This. NMS. fuwu ();} public void shuishoufeeling () {system. out. print ("sailor feeling"); this. NMS. fuwu () ;}} public class decorator {public static void main (string [] ARGs) {nvmishu NMS = new nvmishu (); shuishou Ss = new shuishou (NMS); SS. fuwu (); SS. shuishoufeeling ();}}

The other two bosses saw that this was good, so they also put forward their own requirements: a feeling of college students, a feeling of a good family. The first boss changed his mind and thought that the female secretary was not stimulating enough to recruit a male secretary. The people in the Secretariat all started to grow and work a lot at once, so they could only ask our senior consultant Lin Abai (well, the last article mentioned the words that should not be mentioned, only one name can be changed ~, Thanks again for the great self-XX ).

Lin abui said, this is easy. When you make clothes, do not follow the shape of the female secretary, but according to the secretary's shape. Anyway, both the female secretary and the male secretary are secretaries, and the clothes do not care what kind of body is wrapped:

Interface mishu {Abstract Public void fuwu ();} class nvmishu implements mishu {public void fuwu () {system. out. println ("female delegate service");} class nanmishu implements mishu {public void fuwu () {system. out. println ("male secretary's service");} class shuishou {private mishu MS; Public shuishou (mishu MS) {This. ms = MS;} public void fuwu () {This. ms. fuwu ();} public void shuishoufeeling () {system. out. print ("sailor feeling"); this. ms. fuwu () ;}} class liangjia {private mishu MS; Public liangjia (mishu MS) {This. ms = MS;} public void fuwu () {This. ms. fuwu ();} public void liangjiafeeling () {system. out. print ("excellent feeling"); this. ms. fuwu () ;}} public class decorator {public static void main (string [] ARGs) {shuishou Ss = new shuishou (New nanmishu (); SS. fuwu (); SS. shuishoufeeling (); mishu MS = new nvmishu (); liangjia lj = new liangjia (MS); LJ. fuwu (); LJ. liangjiafeeling (); shuishou SSA = new shuishou (MS); SSA. fuwu (); SSA. shuishoufeeling ();}}

The bosses are satisfied, but the secretaries are not happy. Sometimes the boss is anxious. Just put on a good home decoration, he has to take off his hand and change it back, how can this problem be solved? As a result, Lin Biao's consultant put forward an improvement suggestion: whether it's the "male secretary" who puts on the "sailors" server or the "good family" server ", "female Secretaries" must also be "Secretaries ". So long as the "sailors" and "liangjia" implement the "secretary" interface, they can directly wear other clothes without removing them:

Interface mishu {Abstract Public void fuwu ();} class nvmishu implements mishu {public void fuwu () {system. out. println ("female delegate service");} class nanmishu implements mishu {public void fuwu () {system. out. println ("male secretary services");} class shuishou implements mishu {private mishu MS; Public shuishou (mishu MS) {This. ms = MS;} public void fuwu () {This. ms. fuwu ();} public void shuishoufeeling () {system. out. print ("sailor feeling"); this. ms. fuwu () ;}} class liangjia implements mishu {private mishu MS; Public liangjia (mishu MS) {This. ms = MS;} public void fuwu () {This. ms. fuwu ();} public void liangjiafeeling () {system. out. print ("excellent feeling"); this. ms. fuwu () ;}} public class decorator {public static void main (string [] ARGs) {mishu MS = new nvmishu (); // now the secretary can work properly even with countless clothes on it. shuishou SSA = new shuishou (New liangjia (New shuishou (New liangjia (MS ))))) ); SSA. fuwu (); SSA. shuishoufeeling ();}}

The above is the complete decorator mode code, where the men and women secretaries are the decorator, while the sailors and the good home are the decorator.

Of course, there is a more classic example in Java: Java. Io. *. If you are interested, you can study it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.