Adapter mode-adapter pattern-story (original) about a girl competition with Shen and Ji Xiaolan)

Source: Internet
Author: User

Preface:I am also a beginner. If you have any problems, I hope you can give your comments. In addition, please indicate the author and the source. After all, it took nearly a week to complete the reprinting..

View More design patterns and stories of Shen and Ji Xiaolan

They said that the people Qianlong, Heshen, and Ji Xiaolan traveled to Beijing and came to a village one day. A beautiful girl and her mother and wife lived in the village. Girls love three things:Food, shopping, tourismHowever, the three things are the most annoying of the old lady. She wants her daughter to do two things:Reading and WritingBut the girl has a headache when she hears these things.

Qianlong wanted to examine the capabilities of the two Ministers after learning about these situations, so he gave Shen and Ji Xiaolan three days for each of them to see who can bring the girl to the door and let Shen come first.

He Shen thought, isn't that easy? I think it's good for girls,Eating,Shopping,TravellingThree moves to get her done. The girl was very satisfied when she got down from the first day. She did not expect her wife to get angry. She told her that he was an unlearned hacker and never let him in again. And Shen's transfer plan ended in failure.

Ji Xiaolan thought, I can't say that I want to make an appointment with a girl like Shen. when I leave the house, I want to say that my wife loves listening. What I do is what I love and do what I do, in this way, both of them can be done. The following code is used:

Reading: The old lady thought she was going to study. She actually went out of the door.EatingAndShoppingNow

Writing: The old lady thought it was to practice the word. In fact, she went out of the door.TravellingNow

This set of dark signs is actually an adapter. It extracts the external interface from what the girl wantsEating,Shopping,TravellingBecome what Mrs.ReadingAndWritingIn this way, the old lady can pass the customs smoothly; but what the girl actually wants isEating,Shopping,TravellingIn this way, the girl is also satisfied.

Haha, that should be what it is. Let's say it's a set! On the surface, things are one thing. In fact, internal implementations may be another thing. In the end, Ji Xiaolan won the championship.

Next we useCodeTo tell this story:

Adapter. CS

Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Adapter
... {
// The interface the girl hopes: but the old lady does not like these things and cannot use them directly.
Public Class Girl
... {
Public Void Eating ()
... {
System. Console. writeline ("You don't have to pretend it is. I want to eat hairy crabs!");
System. Console. writeline ();
}

Public Void Shopping ()
... {
System. Console. writeline ("You don't have to pretend to be. Buy the most popular vest this year!");
System. Console. writeline ();
}

Public Void Travelling ()
... {
System. Console. writeline ("You don't have to pretend to be like that. houshan is a good place!");
System. Console. writeline ();
}

}

// What the old lady wants: to ask a girl to come out, she must tell her that we are doing what you like.
Public Interface Mother
... {
VoidReading ();
VoidWriting ();
}

// The essence of Ji Xiaolan's agreement with the girl is that the customer will let him see what he liked and what he actually did is the original set of things.
Public Class Agreement: Mother
... {
Girl girl = New GIRL ();

// I'm talking about reading books. It's for my wife (client). What is it actually?
Public Void Reading ()
... {
Girl. Eating ();
Girl. Shopping ();
}

Public Void Writing ()
... {
Girl. travelling ();
}

}
}

Client:Client. CS

Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Adapter
... {
Class Client
... {
Static Void Main ( String [] ARGs)
... {
Agreement AG = New Agreement ();

Console. writeline ( " Ji Xiaolan: old lady, we are going to study! " );
Console. writeline ( " Old lady: Good, read it! " );
Ag. Reading ();
Console. writeline ();

Console. writeline ( " Ji Xiaolan: old lady, we have gone to practice the word! " );
Console. writeline ( " Mrs! " );
Ag. Writing ();

System. Console. Read ();
}
}
}

The running result is as follows:

View More design patterns and stories of Shen and Ji Xiaolan

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.