Java mode (adapter model)

Source: Internet
Author: User

Read Java in this adapter mode today, here is a small summary and a little talk about feelings. For future use.

First of all. Let's talk about adapters first.

Adaptation is "source" to "target" adaptation. Where the connection between the two is the adapter. It is responsible for "source" over to "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 (goal) need you to speak Japanese, English, and French at the same time, well, now our task is to people this "source" adaptation of this post. How do you fit 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 we discuss how to add French to this method. Perhaps you would say, why not directly in the "source" to join the method directly, my understanding is. Adaptation is the temporary addition of a method to a source class for some purpose, so that the structure of the original class cannot be destroyed. Not doing this at the same time also conforms to the principle of high cohesion and low coupling in Java. Since it cannot be added directly. And 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 primarily used for a single class that implements the adaptation of this pattern. Why do you just do it for a class, and then mention that we first show the code implementation of such a class adaptation pattern.

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? It is very obvious that the adapter class inherits the person class. In the case of a single-inheritance language like Java, it means that he cannot inherit another class, so that the adapter serves only the person. So it is called a class adaptation mode.

Complete the class's adaptation pattern. We're going to start with the adapter pattern for the 2nd type of 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:

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 the selection logic, very clear. The adaptive mode of the object can be used for multi-source adaptation, which makes up for the insufficiency of the class adaptation mode, so that it is no longer possible to write very many adapters in the original class adaptation mode. The weakness is that 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 situations: (1) The system needs to use the existing classes. But the existing classes do not fully meet the needs. (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 boils down to 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. Using the default adapter pattern, his approach is to add an abstract class to the interface and to 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!");}}

All right. In the first adapter mode speaking of this, I want to be able to improve myself and we have one.

Java mode (adapter model)

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.