Java mode (adapter mode) "Reprint"

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/elegant_shadow/article/details/5006175

Today read the next Java adapter mode, the following will be a small summary of the peace talks to discuss the feelings for later use.

First, let's talk about the adapter first. Adaptation is the adaptation of "source" to "target", and the link between them is the adapter. It is responsible for the "source" over to the "target". To give a simple example, for example, there is a "source" is an object person, he has 2 skills are to speak Japanese and English, and a position (goal) need you back to speak Japanese, English, and French, well, now our task is to people this "source" adaptation of this post, how to adapt it? It is obvious that we need to add a way of saying words in order to meet the needs of the target.

Then we discuss how to add French to this method, perhaps you would say, why not directly in the "source" directly adding the method, my understanding is that the adaptation is to achieve a certain purpose and temporarily add a method for a source class, so can not break the structure of the original class. At the same time, it also conforms to the principle of high cohesion and low coupling in Java. Since it can't be added directly, then we're going to come up with a way to do this. Add a method to the "source" without destroying the "source" structure itself.

There are 2 adapter modes, the first is class-oriented adapter mode, and the second is the object-oriented adapter pattern.

First say "class-oriented adapter mode". As the name implies, this type of adapter pattern is mainly used for a single class to achieve the adaptation of such a pattern, why say that only for a class to implement, one will mention, we first show this kind of class adaptation mode code implementation.

The source code is as follows:

1  Public classPerson {2     3     PrivateString name;4     PrivateString sex;5     Private intAge ;6     7      Public voidSpeakjapanese () {8System.out.println ("I can speak japanese!");9     }Ten      One      Public voidSpeakenglish () { ASystem.out.println ("I can speak english!"); -     } -...//The get and set methods of the following omitted member variables the}

The code for the target interface is as follows:

 1  public  interface   Job { 2  3  public  abstract  void   Speakjapanese ();  4  public  abstract  void   Speakenglish ();  5  public  abstract  void   Speakfrench ();  6  7 } 

The code for the adapter is as follows:

1  Public class extends Implements job{23      Public void Speakfrench () {4         5     }6     7 }

Well, the code read and then to do some explanation, the previous question, why call it a class adaptation mode? Obviously, the adapter class inherits the person class, and in the case of a single-inheritance language like Java, it means that he can no longer inherit other classes, so that the adapter is only serving the class of man. So it is called a class adaptation mode.

To finish the class adaptation mode, we're going to start talking about the adapter mode of the 2nd object. The object adapter pattern is to aggregate the "source" as an object into the adapter class. The same words do not say much, put the code:

The source code and the target code above, again no longer repeat.

Only the adapter code is posted:

1  Public classAdapterImplementsJob {2 3 person person ;4 5      PublicAdapter (person person) {6          This. person =Person ;7     }8 9      Public voidSpeakenglish () {Ten person.speakenglish (); One     } A  -      Public voidSpeakjapanese () { - Person.speakjapanese (); the     } -  -     //new Add -      Public voidSpeakfrench () { +          -     } +  A}

The adapter mode of the object, passing "source" as a construction parameter to the adapter, and then executing the method required by the interface. This adaptation mode can be adapted for multiple sources. Make up the insufficiency of the model of class adaptation.

Now let's analyze 2 adaptation modes:

1. The adaptive mode of the class is used for the adaptation of a single source, because its source of a single word, code implementation without writing the choice logic, very clear, and the object adaptation mode can be used for multi-source adaptation, to compensate for the lack of class adaptation mode, so that the original class adaptation mode needs to write a lot of adapters no longer exist, Due to the number of sources can be more, so the specific implementation conditions to choose a branch more, not too clear.

2. The adapter mode is mainly used in several situations: (1) The system needs to use the existing classes, but the existing classes do not fully meet the needs. (2) Classes that do not have much relevance to each other are introduced to complete a work together (object adaptation).

Finally, let's talk about the default adapter mode: The core of this model is summed up as follows: When you want to implement an interface but do not want to implement all the interface methods, just want to implement a part of the method, the default adapter mode is used, his method is to add an abstract class in the interface and the implementation class, Instead, use abstract classes to empty all methods of implementing the target interface. The specific implementation class only needs to overwrite the method that it needs to complete. The code is as follows:

Interface class:

1  Public InterfaceJob {2     3      Public Abstract voidSpeakjapanese ();4      Public Abstract voidspeakenglish ();5      Public Abstract voidspeakfrench ();6      Public Abstract voidSpeakchinese ();7     8}

Abstract class:

1  Public Abstract classJobdefaultImplementsjob{2 3      Public voidSpeakchinese () {4         5     }6 7      Public voidSpeakenglish () {8         9     }Ten  One      Public voidSpeakfrench () { A          -     } -  the      Public voidSpeakjapanese () { -          -     } -  +}

Implementation class:

1  Public class extends jobdefault{2     3      Public void Speakchinese () {4         System.out.println ("I can speak chinese!" ); 5     }6     7 }

OK, the adapter mode is the first to talk about this, I hope to own and everyone has a raise.

Java mode (adapter mode) "Reprint"

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.