The adapter mode for Java design mode

Source: Internet
Author: User

What is the adapter, said the text, the adaptation should be a verb, and the use of ancient prose should be used to make use of the law, translation into, so ... A device that fits the old things and works with new things.

Once upon a time, I was fortunate enough to have sold a laptop, and it was there that I first heard the word adapter. A small black block connected to the power supply and computer, which indicates the properties of voltage, current and so on, this is the adapter. Here may be suitable for a number of things, suitable for distribution sources, the role of pressure, and suitable for distribution lines, so that the triangle socket can be turned into a circular hole plug into the power port of the notebook computer.


So, let's use the vernacular, or the words, to explain what an adapter is. Use a device to let the original thing a no longer need to change the conditions can meet the requirements of thing B.

Here again, stick to the encyclopedia above the adapter mode annotation:

In computer programming, the adapter pattern (sometimes referred to as a wrapper style or wrapper) fits the interface of a class into what the user expects.

An adaptation allows classes that are usually not working together because of incompatible interfaces, by wrapping the class's own interface in an existing class.

It should be easy to understand what adapter mode is when you look at this very obscure thing.

OK, said a lot of nonsense, will Java us, in the end how to use the Java language to describe the adapter mode it? Here we use the example of the notebook to describe the adapter mode!

First, let's start with a few things in this example of notebooks.

1. Wire 220V: Used to plug in the power outlet and connect electrical appliances, transmission power 220V voltage and a three-hole power outlet.

2. Wire 110V: The user connects the adapter and the notebook computer, can transmit 110V voltage and a single hole power outlet.

3. Adaptor: For connecting wire 220v and wire 110v, convert 220V voltage to 110V voltage. The 3-well power outlet for the down wire translates into a single-hole power supply for the notebook computer.

4. General home computer Mainframe, directly connected to the 220V 3-well power cord directly.

5. Laptop, can only connect 110V wire and has a single-hole power inlet.

6. People, turn on the computer

First define the interface of the wire.

/** * wire connection, supply voltage and 3-well power outlet * will understand the next, my English level is like this */public interface Line1 {//provide 220V voltage public void voltage220 ();// Supply 3-Well power outlet public void kong3 ();


When the wire definition is complete, we need a PC host to realize the function of this wire.

public class Pcmechine implements LINE1 {@Overridepublic void Kong3 () {System.out.println ("3 hole sockets Inserted");} @Overridepublic void voltage220 () {System.out.println ("Connect power supply, supply 220V voltage");}}

At this time we define a human class, this human is responsible to start the computer

public class Person {/** * uses 220V power cord to turn on the computer * @param line1 */public void Startpc (Line1 line1) {line1.voltage220 (); Line1.kong3 ( ); System.out.println ("Start computer");}}


let's test it.

public class Test {/** * @param args *///take a host directly connected to the 220V power cord <span style= "White-space:pre" ></span>line1 Mechine= new Pcmechine (); <span style= "White-space:pre" ></span>person p = new Person (); <span style= " White-space:pre "></span>p.startpc (Mechine);}
Output Result:

Connect the power supply with 220V voltage
3-hole receptacle insertion
Host Boot

And now we have a notebook, this sells The notebook is also a conscience, give us a 110V single-hole line.

Power cord:

Public interface Line2 {//provides 110V voltage public void voltage110 ();//provides 1-hole power outlet public void kong1 ();

Notebook PC

public class Notepc implements Line2 {@Overridepublic void kong1 () {System.out.println ("single hole socket Insertion");} @Overridepublic void voltage110 () {System.out.println ("Using 110V voltage");}}

This time the problem comes, this wire for our PC host is can be used directly, because the PC host is directly using the 220V voltage has 3 holes in the power supply.

But if we now have one more notebook, the notebook is using 110V voltage, and can only use a single-hole power supply, how to do. And we have a single-hole 110V power cord, but the problem is that the power cord can not be plugged into the socket, what to do?

We fantasize about, the line of the 220V line first to take off a few to become 110V, and then cut off 3 head and 110V of the line screwed together? No problem, we can finally use the laptop after we have a big fight. At this time, I suddenly remembered that I have important information in the desktop computer, finished, now the line let me cut off, can not connect to the desktop computer.

Well, this time the adapter appears.

No matter ascetics, buy or hand-done, in short we have this adapter. This adapter, like a PC, has a 3-hole power port and requires 220V of voltage. But after accessing the 220V power supply, he can convert the 220V power supply to 110V power output, and convert the 3-well input to a single-hole output.

public class Adapter implements line1{//adapter can connect 110V single-hole power cable private Line2 line2;public Adapter (Line2 line2) {this.line2= Line2;} @Overridepublic void Kong3 () {System.out.println ("Convert 3-hole power supply to a single-hole power supply"); Line2.kong1 ();} @Overridepublic void voltage220 () {System.out.println ("Connect power, convert 220V voltage to 110V voltage"); line2.voltage110 ();}}



OK, the adapter we have done, the adapter and 220V power cord, 110V power cord are connected, test it, to see if the laptop can not be used properly.

public class Test {public static void main (string[] args) {//Take a notebook with a 110V power cord to Line2 notepc = new Notepc ();//Build an adapter and connect 220 V power cord, then plug the 110V power cord connected to the notebook into the adapter Line1 adapter= new Adapter (NOTEPC); Personal p = new person (),//The adapter attached to the laptop and the 220V power cord is provided to the human, and the P.startpc (Adapter) is switched on;}}
Output:

Connect the power supply to convert the 220V voltage to a 110V voltage
Using 110V voltage
Converting a 3-well power supply to a single-hole power supply
Single hole receptacle Insertion
Start your computer


Wow, the laptop started up.


At this point, the adapter mode is complete. Let's go back and summarize what the adapter is.

As I have above the code, the adapter is a tool that is compatible with the original thing (220V power cord) and fits with the new thing (110V power cord).

Here the 220V power cord, equivalent to the original code, he can meet the requirements of the PC host.

And laptops are new customers, or new requirements.

Without rebuilding the original code, create a new tool to meet the new requirements. This is the adapter mode.


Through the adapter mode, we look at Java, in fact, the adapter mode is a kind of Java polymorphism of the use. An abstract approach that can be implemented in a variety of ways. Just like a 220V power cord, you can connect directly to a PC, or you can connect an adapter with a variable-voltage function. This is a different way of connecting (implementation), when different connections generate new results.

Is this the best manifestation of Java polymorphism?


The text is a bit messy, please forgive me.

This article is original, reproduced please indicate the source. Thank you.


The adapter mode for Java design 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.