Design Mode note 6 (adapter Mode)

Source: Internet
Author: User

Adapter Mode

The adapter mode is to convert an interface of a class into an interface required by another customer class, so that both classes do not need to change their logic. In other words, it is difficult to make up for it.

 

Our lab has been very embarrassing recently because our lab is displaying our products around the world, one of which is to show the status of our products at the factory.

There is no problem in displaying beasts and demons, but it does not pass the inspection by the relevant national departments when displaying normal men and women, because our products are red fruits when they are released.

Class Ren {} class renyao extends Ren {Public String head = "Demon head"; Public String body = "demon body-none "; // you all know what it is: 'none' Public String foot = "demon foot"; Public void show () {system. out. println (this. head + "" + this. body + "" + this. foot) ;}} class shouren extends Ren {Public String head = "orcs head"; Public String body = "orcs body-heterogeneous"; // It does not matter if it is different, I didn't see any puppies because the red fruit was caught by the relevant departments. Public String foot = "Orc foot"; Public void show () {system. out. println (this. head + "" + this. body + "" + this. foot) ;}} class Nanren extends Ren {Public String head = "men's head"; Public String body = "men's body-Men"; Public String foot = "men's feet "; public void show () {system. out. println (this. head + "" + this. foot); // do not show fruits} class nvren extends Ren {Public String head = "Female head"; Public String body = "female body-female "; public String foot = ""; Public void show () {system. out. println (this. head + "" + this. foot); // display of red fruits is not allowed} public class adapter {public static void main (string [] ARGs) {New shouren (). show (); // display all new renyao (). show (); // display all new Nanren (). show (); // only show the head and feet -- this is not consistent with our original intention new nvren (). show (); // only show the head and feet -- this is not consistent with our original intention }}

This is the greatest embarrassment we have encountered. Our products have been created, but when we exhibited them, we found that some exhibits were useless, and neither of these two factors (categories) could be easily changed. At this time, a person from the Institute of Sun's literature and art film fans said that this problem is a good solution, and Sun's country has a good precedent. Their literature and art films are divided into sales and sales types, if you want to sell out-of-the-box films to the market, you only need to start a horse (well, here we will popularize the knowledge of love action movies in the island countries of the ugly, short, and poor countries: domestic films sold in the island countries cannot be exposed to XXX, but only for foreign films in Europe and America. Therefore, some post-code processing is required if the export is converted to internal sales. Of course, there are also retail outlets, if it is not released, no one cares ).

Now we all understand.

Class Ren {} class renyao extends Ren {Public String head = "Demon head"; Public String body = "demon body-none "; // you all know what it is: 'none' Public String foot = "demon foot"; Public void show () {system. out. println (this. head + "" + this. body + "" + this. foot) ;}} class shouren extends Ren {Public String head = "orcs head"; Public String body = "orcs body-heterogeneous"; // It does not matter if it is different, I didn't see any puppies because the red fruit was caught by the relevant departments. Public String foot = "Orc foot"; Public void show () {system. out. println (this. head + "" + this. body + "" + this. foot) ;}} class Nanren extends Ren {Public String head = "men's head"; Public String body = "men's body-Men"; Public String foot = "men's feet "; public void show () {system. out. println (this. head + "" + this. foot); // do not show fruits} class nvren extends Ren {Public String head = "Female head"; Public String body = "female body-female "; public String foot = ""; Public void show () {system. out. println (this. head + "" + this. foot); // unavailable for red fruits} class nanrenadapter {Nanren NR; Public nanrenadapter () {Nr = new Nanren ();} public void show () {system. out. println (Nr. head + "" + "CAPTCHA:" + Nr. body + "" + Nr. foot); // human bypass} class nvrenadapter {nvren NR; Public nvrenadapter () {Nr = new nvren ();} public void show () {system. out. println (Nr. head + "" + "CAPTCHA:" + Nr. body + "" + Nr. foot); // human bypass} public class adapter {public static void main (string [] ARGs) {New shouren (). show (); // display all new renyao (). show (); // display all new nanrenadapter (). show (); // display all-CAPTCHA new nvrenadapter (). show (); // show all-CAPTCHA evasion }}

The adapter mode is the least recommended mode. As long as unified interfaces are possible, they should be unified in the design phase.

The adapter mode is not a design mode, but a maintenance mode. However, most of today's IT workers work on the Code bodies of their predecessors, so do not modify the code of their predecessors, because you do not know if there are other people using this interface, so use the adapter mode!

It is better to make up for 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.