Java design mode-adapter Mode

Source: Internet
Author: User
Tags mercurial dota

Java design mode-adapter Mode
Introduction:

We always have some difficulties in one thing. A typical case is that, for example, I have a dual-hole socket, but my computer has a three-pin plug. How can I insert this three-pin plug into this two-hole socket?

For a DotAer, I was wondering if I could let a hero from the Wei army do the operations of the natural disasters army? You can solve these problems by using the adapter mode described in this article. Let's see how I did it.

Definition:

Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.

 

Example Background:

Now let's assume we are playing a DotA disk. We know that in DotA, there are natural disasters and threat groups.

The mission of the legion of natural disasters is to protect the frozen throne and attack the tree of the world. The use of the Threat army is to attack the frozen throne and protect the tree of the world.

In the natural disasters army, we chose UG, while in the near-Wei army, we chose Xiao Hei.

A disk of DotA starts like this...

 

Class diagram:

Figure-1 adapter mode class diagram

Graph Analysis:

Now let's analyze this class chart. From the class diagram, we can see that Traxex and Mercurial are simple interfaces for their respective camps. There is nothing to say about it. However, let's take a closer look at the DisguiserAdapter adapter class. We can see that this class holds the object of a security group, but it shoulders the mission of some natural disasters. This is the key to the adaptation mode. It's easy to understand. If I say that I am the adapter of the natural disasters army, and I hold a hero of the near-Wei army, I am still doing the work of the near-Wei army (Note: this refers to explicit calls in public methods, rather than the hero of the anti-DDoS corps.), What do I need this adapter? For more information, see the following code analysis.

 

Code implementation: KonoeHero ):

There is nothing to explain about this interface. It defines two missions and requires implementation classes. The interfaces of the natural disasters corps are the same.

Public interface KonoeHero {/*** attack the frozen throne */public void attackFrozenThrone ();/*** protect the world tree */public void protectWorldTree ();}

 

Implementation of the near-wei military corps interface (Traxex ):

 

Here, let's take Xiao Hei as an example. She implemented the interfaces of the near-wei military Regiment. You need to implement the interface method of the near-wei military Regiment.

Public class Traxex implements KonoeHero {@ Override public void attackFrozenThrone () {System. out. println ("I am" + Traxex. class. getSimpleName () + ", I am attacking the frozen throne. ") ;}@ Override public void protectWorldTree () {System. out. println (" I am "+ Traxex. class. getSimpleName () +", I am protecting the tree of the world. ");}}
Adapter implementation (DisguiserAdapter ):

Here we take the adapter of the natural disasters corps as an example (of course, which camp is the same, not to mention ). The adaptation contains an object of the near-wei military regiment. Because it is an object of the near-wei military Regiment, this object will only fulfill the mission of the near-wei military Regiment, that is, attacking the frozen throne and protecting the tree of the world.

Speaking of this, you don't have any questions. Why is it the mission of your camp? In this case, why should I use this adapter mode to directly take the heroes of the Wei army? We will leave a suspense on this point, which will be discussed later.

Public class DisguiserAdapter implements DisasterHero {// private KonoeHero hero; public DisguiserAdapter (KonoeHero _ hero) {hero = _ hero;} @ Override public void dispatch () {hero. attackFrozenThrone () ;}@ Override public void attackWorldTree () {hero. protectWorldTree ();}}
As we can see above, this hero object of the anti-DDoS army is called by a method that looks like the mission of the natural disasters army, but actually does what the anti-DDoS army does. Now let's answer the question. Why is the adapter actually fulfilling its own camp mission? We said that if your socket is to provide v ac power, and our computer needs 20 V. We use an adapter for adaptation so that the v ac can be provided to the computer normally. The computer is actually directly powered by a 20 V voltage, but it looks like it is powered by a V voltage. Adapter implementation improvement (DisguiserAdapter2 ):

Now, the adapter mode is basically the same. However, in the constructor of the above adapter, an object of the Wei army is introduced. This may seem confusing. The following is an improvement in the form of anonymity, which makes us feel like we have used a hero of the natural disasters army and done the same thing as the anti-DDoS army.

 

Public class DisguiserAdapter2 implements DisasterHero {// private KonoeHero hero; public DisguiserAdapter2 () {initEvent ();} private void initEvent () {if (hero = null) {hero = new KonoeHero () {@ Override public void protectWorldTree () {System. out. println. ") ;}@ Override public void attackFrozenThrone () {System. out. println (" I Am a Wei army, and I am protecting the world tree. ") ;}}}@ Override public void protectFrozenThrone () {hero. attackFrozenThrone () ;}@ Override public void attackWorldTree () {hero. protectWorldTree ();

}}

 

Test class:
Public class WorldOfWarcraft {public static void main (String [] args) {System. out. println ("\ n -------------------- --------------"); KonoeHero konoeHero = new Traxex (); konoeHero. attackFrozenThrone (); konoeHero. protectWorldTree (); System. out. println ("\ n -------------------- task of the natural disaster Corps ------------------"); DisasterHero disasterHero = new Mercurial (); disasterHero. protectFrozenThrone (); disasterHero. attackWorldTree (); System. out. println ("\ n -------------------- the adapter has done this kind of thing --------------------"); DisguiserAdapter adapter = new DisguiserAdapter (new Traxex (); adapter. attackWorldTree (); adapter. protectFrozenThrone (); System. out. println ("\ n ------------------ the adapter did this"); DisguiserAdapter2 adapter2 = new DisguiserAdapter2 (); adapter2.attackWorldTree (); then ();}}
Test results:
------------------ The task of the near-Wei army regiment ------------------ I am Traxex and I am attacking the frozen throne. I am Traxex, And I am protecting the tree of the world. ------------------ Task of the natural disasters Corps ------------------ I am Mercurial and I am protecting the frozen throne. I am Mercurial, And I am attacking the tree of the world. ------------------ The adapter has done this kind of thing ------------------ I am Traxex, And I am protecting the tree of the world. I'm Traxex, And I'm attacking the frozen throne. ------------------ The adapter has done this kind of thing ------------------ I am a near-Wei army, and I am attacking the frozen throne. I am a close guard and I am protecting the tree of the world.
Download GitHub source code:

Https://github.com/William-Hai/DesignPattern-Adapter

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.