(Transfer) Design Patterns Notes II: Adapter Yakuu [Original]

xiaoxiao2021-03-06  87

Foreword: Oh, start from this, how is the method of changing a record? Search people, entertaining. The first time I tried, I laughed. ^ _ ^

Definition: Adapter: Converts a class interface into another interface that customers want. Adapter mode makes it impossible for the interface that is not compatible

Those classes work together can work together.

The role of setting:

Helen, the first-time beauty programmer (no BF).

Andytao, master, combat power reaches N-class, and has a considerable understanding of design patterns.

statement of problem:

Helen buried in its own small space, (by white: she is a beautiful woman, but the level of writing code is still good.) A bit asked

The question is fighting, at this time, Andytao came out next to it, "Hey, beauty, go to some, eat together?" "No, I am annoying.

! Helen wrinkled. "Wow, who, who is bullying we are helen? ? ? "Sliced, who dares to bully me." "What happened?" "Andytao one

The look of anger is filled with (this kid, Sima Zhao's heart).

"No, I have a bad process here." "Hey, I will see!" Andytao didn't know when he was behind him.

On the screen:

Public class drawcircle {

Public void drawcircle () {

......

}

Public void insert (String MSG) {

System.out.println ("DrawCircle INSERT ():" MSG);

}

......

}

Public class drawanle {

Public void Drawangle () {

......

}

Public void insert (String MSG) {

System.out.println ("Drawangle INSERT ():" MSG);

}

......

}

"I have to call Drawangle in drawcircle now, but the class has been distributed, can't be modified, what should I do?"

Looking at the beauty frowning, Andytao a bit of heartache, "This, I will prompt you, write an adapter class." "What is Adapter?

"

"Oh, in the actual coding process, because of a need, we often interact two unrelated classes. So we need to make some amendment

, Modify the interfaces of each class, which usually does not meet the code requirements, and if we do not have source code, what should we do? So, we have to use

Adapter, that is, the meaning of the adapter. As you above, we can't modify the class interface, so we write an Adapter class. "

Public class drawadapter extends drawcircle {

Private Drawangle Drawangle;

Public DrawAdapter (Drawangle Angle) {

THIS.DRAWANGLE = Angle;

}

Public void insert (String MSG) {

Drawangle.insert (STR);

}

......

}

"In the above code, Drawangle belongs to Adaptee, is adapter. Drawadapter is Adapter, adapting Adaptee (Drab Drawangle) and Target (Target Drawcircle). In fact, this is a combination method (Composition) and Inheritance method comprehensive use

One example. In this way, you don't need to modify the original class, even don't even need to know its internal structure and source code. "

"What we are talking about is the object adapter, which allows Client and Adaptee to completely unrelated, only the adapter knows both.

exist. In addition, there is a class adapter, which uses multiple inheritance. Icon object adapter (Object adapter)

It is the last example, DrawAdapter inherits DrawCircle, but DrawAdapter can also inherit Drawangle. Due to Java does not support

Multiple inheritance, this is to say that one of our classes must be defined as an interface (Interface). We can define Drawangle as an interface idRawangle

This code is as follows. "

Public interface idrawangle {

Public void insert (String MSG);

......

}

Public class drawan {

Public void Drawangle () {

......

}

Public void insert (String MSG) {

System.out.println ("Drawangle INSERT ():" MSG);

}

......

}

Public Class Drawadapter Extends Drawcircle Implements Idrawangle {

Private Drawangle Drawangle;

Public DrawAdapter (Drawangle Angle) {

THIS.DRAWANGLE = Angle;

}

Public void insert (String MSG) {

Drawangle.insert (STR);

}

......

}

"I saw it, so we have implemented the Adapter mode. However, the class adapter has a problem if we

Target and Adaptee have a method of the same name, Adapter does not guarantee that these two methods have the same meaning or behavior, which is very dangerous.

of. So pay attention to this situation! "

"Oh, I know, the use of Adapter mode is to make it work together for those classes that are not compatible without compatibility and cannot work together. From

And solve my current problem. "

"Beauty is not stupid. This meal, please, ... ..."

"Want to chase me? This meal, please, huh, huh, let's talk. Thank you, let's go." "Ok!"

If you want to know how you, please wait. ^ _ ^

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

New Post(0)