Java design pattern: from the [Mouse interface] [Starcraft's military behavior] analysis Adapter Pattern

Source: Internet
Author: User

The adapter converts an interface of a class to another interface that the customer wants. The following uses three examples to reflect the different uses of the adapter:

Scenario 1:

I bought a PS2 interface mouse, but my computer does not have PS2 interface, only USB interface. In order not to waste this mouse, I ran to the store and bought a PS2 to USB adapter, so I used the PS2 mouse. A usb adapter is a typical adapter.

Interface USBPort {void connect ();} interface PS2Port {void connect ();} class USBMouse implements USBPort {public void connect () {System. out. println ("the mouse is connected to the USB port! ") ;}} Class PS22USBMouseAdapter implements USBPort {private USBPort usbMouse; public PS22USBMouseAdapter (USBPort usbPort) {usbMouse = usbPort;} public void connect () {usbMouse. connect () ;}} class Adapter1 {public static void main (String [] args) {USBMouse mouse = new USBMouse (); mouse. connect (); USBPort adapter = new PS22USBMouseAdapter (mouse); adapter. connect ();}}

PS22USBMouseAdapter is an adapter from PS2 to USB interface. Because it outputs USB information, it inherits USB port. When we finally instantiate the adapter, its type is USB port-the design pattern is like this. We generally use the abstract object to declare and define it, rather than the specific class. PS22USBMouseAdapter implements the transformation from PS2 interface to USB interface.

Scenario 2:

In Starcraft, the top personnel of the family are battle cruiser ). The cruiser has a skill called Yamato gun ).

Suppose we didn't think it could mount a large gun when designing a cruiser, so there was only one attack method in our cruiser class. Later, we wanted to add a large and gun skill to the cruiser, but we didn't want to modify the code of the cruiser class. At this time, we can use an adapter to implement it. At this time, the adapter can be understood, we installed a conversion device on the cruiser so that it can be installed and fired with a large gun.

Class Battlecruiser {public void attack () {System. out. println ("Dahe warship launches an attack! ") ;}} Class YamatoAdapter extends Battlecruiser implements Yamato {public void yamato () {System. out. println (" attack! ") ;}} Interface Yamato {void attack (); void yamato ();} class Adapter2 {public static void main (String [] args) {Yamato superBattlecruiser = new YamatoAdapter (); superBattlecruiser. attack (); superBattlecruiser. yamato ();}}

Note that the Battlecruiser class was designed from the very beginning. We have created a Yamato interface to attack the attack method of the original cruiser, and added a yamato method to attach the Yamato to the cruiser. Finally, a YamatoAdapter class inherited from a cruiser is created. In the test class, a cruiser is instantiated with Yamato. It can be both a common attack and an attack with a large gun.

You may wonder why YamatoAdapter has inherited the Battlecruiser interface? The reason is as mentioned earlier. We use "interfaces" to define objects, rather than specific classes.

Case 3:

I need to design some abstract processes of the StarCraft clan. For example, all arms must inherit from one interface, which includes attack, stop, move, and other methods. This means that attack, stop, and move methods must be implemented for any type of soldiers.

In this way, there will be a problem: not all arms have attack, stop, and move actions. For example, Medic does not act as an attack (in the game, she cannot attack the enemy), and Vulture, it can be used to bury a spider Ray. When the enemy approaches, the spider Ray will launch an automatic attack. That is to say, spider Ray does not have the stop and move methods, but only the attack method.

It can be seen that once there are more interface methods, we have to manually write a lot of code to implement them. If some of these methods are not used by our class, we have to write an empty method. To reduce code writing, we can use the adapter mode of the interface:

Interface StarcraftSoldier {void attack (); void stop (); void move ();} abstract class SoldierAdapter implements StarcraftSoldier {public void attack () {} public void stop () {} public void move () {}} class SpiderMine extends SoldierAdapter {public void attack () {System. out. println! ") ;}} Class Adapter3 {public static void main (String [] args) {new SpiderMine (). attack ();}}

The SoldierAdapter of the interface implements empty implementations for all methods of the StarcraftSoldier interface. When we create SpiderMine and inherit SoldierAdapter, we will not implement all methods in the interface once.

The above is the purpose of the adapter I understand. I understand it as a compatible device or a conversion device. If you have any questions, you can point them out.

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.