Java adapter mode - are you jealous? ... let you remember the adapter pattern with the most vivid examples

Busy! Busy! Busy! I haven't updated the article all the time. Alas, the number of visitors has dropped dramatically! Fan...
Story introduction
Basic concepts
Class Adapter
object adapter

Busy! Busy! Busy! I haven't updated the article all the time. Alas, the number of visitors has dropped dramatically! Fans are growing slowly! Catch up with the project recently, update iteration! There is no time!!! What's more, the article I wrote last time is a bit indescribable. I have rectified it. Today, I can't easily spare some time to tell this story!

Article catalog

Story introduction

Continue to tell the story

"MMM -"! , stretched out a lazy shake, I sleep very comfortable! I think my sister is still sleeping, so I didn't bother her. I went to make breakfast after I got up. Today, I plan to go to school after breakfast!

I didn't expect that the student sister could sleep so well. When she got to 10 o'clock, I said, "let me heat your breakfast! We'll go to school after a rest! " The student sister nodded and agreed!

This is my first class! It's about Java adapter mode. Haha, this basic knowledge is nothing to me, but it's not the same for the little sister who just entered school as me! After class, a little sister asked me questions. She had many things she didn't understand. Of course, I could understand. The first time I touched such a deep concept, it was comparable to climbing to the sky! I told her the content of this lesson carefully, maybe she understood it! ha-ha! Maybe I'm sorry to ask!

After class, the little sister who asked me questions suddenly said to me, "let's go to dinner at noon!" "Sorry, I have an appointment at noon..." I replied politely. The following is a self-study class. I don't think she understands it. I'll go to her side and explain it to her! Until dinner time, she understood! At this time, I stand up to go out of the classroom, near the door, found that the elder sister has been waiting for me! However, it seems that it's a bit bad. I can feel the mood of Xuejie at this moment! "Jealous!" "What to eat?" I said to my elder sister in embarrassment "Dumplings, more vinegar!" And then, pull me and go

It's true that the student sister asked for a bowl of vinegar and even let me drink it! At this time, I thought, "well, it seems that I'm jealous!" "You are not allowed to sit next to female students in the future!" "Said the elder sister playfully! What can I do? Nod my head, then take a big sip of vinegar

After eating, the student sister said to me, "let's rent a house outside the school. It's convenient to take care of me..." I nodded and agreed!

Well, who knows what will happen when renting? Let's wait for the next article! Take a look at the adapter mode!

Basic concepts

We all hope that the little sister in the story can learn the adapter mode, but she doesn't understand what the teacher said. She needs me to teach, so I will take the responsibility of the adapter. I will teach her to let the little sister learn the adapter mode. Officially speaking, it is to convert one class interface into another interface that the customer wants. The adapter pattern allows classes that could not work together because the interfaces are incompatible to work together.

Next, we will talk about class adapter and object adapter. You can see the comments in the code!

Class Adapter

First of all, I need to talk about my little sister. I want to give her the concept first, then the code example!

Create my interface!

/** * On behalf of me, my little sister needs me to learn the adapter mode */ public interface IKnow { //Teach her basic concepts void teachBasic(); //Tell her code examples void teachCode(); }

Goal: little sister should learn the adapter pattern and create this target class

/** * Goal: the little sister in the same class should finally learn the adapter mode */ public class SmallSisterKnow { /** * Learn adapter mode */ public void know() { System.out.println("Little sister finally learned the adapter mode!"); } }

First of all, little sister knows the goal to be achieved. She works hard in this direction. The motivation to learn the adapter mode comes from this goal! So, little sister inherits this goal and realizes the interface that someone teaches her, that is, me! Then I'll teach her!

/** * Girls in the same class */ public class SmallSister extends SmallSisterKnow implements IKnow { /** * Rewrite the method of teaching the little sister (it can be understood that she needs to teach) */ @Override public void teachBasic() { System.out.println("I told my sister the basic concept"); } /** * Rewrite the method of teaching the little sister (it can be understood that she needs to teach) */ @Override public void teachCode() { System.out.println("I told the young lady the code example"); //Then the little sister understood (you can't still understand, if you do, you can take the lady in the story away) super.know(); } }

Next, put the whole event into "time machine", run this event!

/** * @author CSDN Cheng Yinan * @link https://myhub.blog.csdn.net */ public class Adapter { public static void main(String[] args) { SmallSister smallSister = new SmallSister(); //Started teaching the little sister adapter mode //Tell her the basic concepts smallSister.teachBasic(); //Tell her the code smallSister.teachCode(); } }

Look at the structure of the code:

Time machine is running!

See? If you understand, please take the lady away!

object adapter

The object adapter is slightly modified in the original example!

Let's talk about the changes of the class girls and sisters:

/** * Girls in the same class */ public class SmallSister implements IKnow { private SmallSisterKnow smallSisterKnow = new SmallSisterKnow(); /** * Rewrite the method of teaching the little sister (it can be understood that she needs to teach) */ @Override public void teachBasic() { System.out.println("I told my sister the basic concept"); } /** * Rewrite the method of teaching the little sister (it can be understood that she needs to teach) */ @Override public void teachCode() { System.out.println("I told the young lady the code example"); //Then the little sister understood (you can't still understand, if you do, you can take the lady in the story away) smallSisterKnow.know(); } }

Now, instead of inheriting from the goal, instantiate the goal (only by focusing on the goal can you learn), and then let me teach!

Time machine starts!

11 May 2020, 04:05 | Views: 2277

Add new comment

For adding a comment, please log in
or create account

0 comments