Java mode (adapter mode)

Source: Internet
Author: User

Today read the next Java adapter mode, the following will be small to summarize 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". As a simple example, there is a "source" is an object person, he has 2 skills to speak Japanese and speak English, and a position (target) 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 means of speaking to people so that we are able to meet the needs of our goals.

Then discuss how to add French to this method, perhaps you would say, why not directly in the "source" directly into the method, my understanding is that the adaptation is to achieve a certain purpose for a temporary combination of a source class and some method, so can not destroy the structure of the original class. Not doing this at the same time 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 it. This "source" adds a method without destroying the "source" structure itself.

There are 2 adapter modes, the first is class-oriented adapter mode, and the other 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 only for a class to implement, one will mention, we first show such a class adaptation mode code implementation.

The source code for example is the following:

Public class Person {private string name;private string sex;private int age;public void Speakjapanese () {SYSTEM.OUT.PR Intln ("I can speak japanese!");} public void Speakenglish () {System.out.println ("I can speak english!");} ...//omit the Get and set methods for member variables below}

The code for the target interface is as follows:

Public interface Job {public abstract void Speakjapanese ();p ublic abstract void Speakenglish ();p ublic abstract void SP Eakfrench ();}

The code for the adapter is as follows:

Public class Adapter extends person implements job{public void Speakfrench () {}}

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 a single-inheritance language like Java, it means that he can no longer inherit other classes, so that the adapter only serves the class of man. So it is called a class adaptation mode.

To finish the class adaptation mode, we start by saying the 2nd object's adapter mode. 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:

Public class Adapter implements Job {person person;public Adapter (person person) {This.person = person;} public void Speakenglish () {person.speakenglish ();} public void Speakjapanese () {Person.speakjapanese ();} New addpublic void Speakfrench () {}}

The adapter mode of the object, the "source" as a construction parameter into the adapter, and then run 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 for the analysis of 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 selection 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 adapter situation no longer exist, Because the number of sources can be more, so the detailed implementation of the conditions to choose a branch is more, not too clear.

2. The adapter mode is mainly used in several cases: (1) The system needs to use the existing classes, but the existing classes do not fully meet the need. (2) A class that does not have much association with each other is introduced to complete a work (object adaptation).

Finally, let's talk about the default adapter mode: The core of such a pattern is the following: 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 detailed implementation class, The whole method of implementing the target interface is to use abstract class to empty. The detailed implementation class simply needs to cover the way it needs to be completed. The code is as follows:

Interface class:

Public interface Job {public abstract void Speakjapanese ();p ublic abstract void Speakenglish ();p ublic abstract void SP Eakfrench ();p ublic abstract void Speakchinese ();}

Abstract class: Public abstract class Jobdefault implements Job{public void Speakchinese () {}public void Speakenglish () {}public void Speakfrench () {}public void Speakjapanese () {}}

Implementation class:

Public class Jobimpl extends Jobdefault{public void Speakchinese () {System.out.println ("I can speak chinese!");}}

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

Java mode (adapter mode)

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.