Java design mode--Adapter mode

Source: Internet
Author: User
Tags mercurial dota

Introduction:

We always have some difficulty in one thing. A more classic case is, for example, I have a two-hole socket, but my computer is a three-pin plug, how can I put this three-pin plug into the two-hole socket inside?

For a dotaer, I was wondering if I could get a near-guardian hero to do the scourge thing? These conditions can be resolved using the adapter pattern described in this article. Let's see how I do it.

This article link:http://blog.csdn.net/lemon_tree12138/article/details/50326851--coding-naga
--Reprint please indicate the source


Definition:

Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible to work together those classes that would otherwise not work together because of incompatible interfaces.


Example background:

Now let's assume we're playing a game of DotA. We know that in DotA there are the Scourge Corps and the Guardian Corps.

The mission of the Scourge Corps is to protect the frozen throne and attack the tree of the world, while the Guardian Corps is used to attack the frozen throne and protect the tree of the world.

We chose UG inside the Scourge Corps, and we chose the little black in the corps.

A plate of DotA started ...


class Diagram:


Figure-1 Adapter Pattern class diagram


class Diagram Analysis:

Now let's analyze This class diagram. From the class diagram we can see that both traxex and mercurial are simply implementing the interfaces of their respective camps. There's nothing to say about this. However, taking a closer look at the Disguiseradapter adapter class, we can see that this class holds the object of a near-guardian regiment, but it has the mission of some scourge corps. This is the key to the adaptation model. It's good to understand that if I had an adapter for the scourge, I had the hero of a near-Guardian corps, or did the near-Legion thing ( Note: This refers to the explicit invocation in the public method, not the mission of the Legion of the near-Guardian's heroes. ), what do I need this adapter for? Detailed description, in the following code analysis for further analysis.


Code implementation:Guards Mission Interface (Konoehero):

There's not much to explain about this interface. is to define two missions that need to be implemented by their implementation classes. The same is true of the Scourge Corps interface.

Public interface Konoehero {    /**     * Attack Frozen Throne * * Public    void Attackfrozenthrone ();        /**     * Conservation of the World Tree *     * public    void Protectworldtree ();

Guards Mission Interface Implementation (TRAXEX):

Here's a little Helai example, she realizes the near-Guardian Corps interface. You need to implement the interface method of the Guards 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'm protecting the tree of the world. ");    }}

Adapter Implementation (Disguiseradapter):

Here we take the example of the Scourge's adapter (which, of course, illustrates the same, needless to say). Fit inside holds an object of the Guardian Corps, because it is the object of the Guardian Corps, so this object will only fulfill the mission of the Guards Regiment, that is to attack the frozen throne, the protection of the World Tree.

Speaking of which, you are not in doubt, how is this the mission of your own camp? If so, why should I use this adapter mode, directly take the hero of the Guards Regiment is not good? We'll leave a suspense on this point, and we'll talk about it later.

public class Disguiseradapter implements Disasterhero {    //hold a near Guardian Regiment object    private Konoehero hero;        Public Disguiseradapter (Konoehero _hero) {        hero = _hero;    }        @Override public    void Protectfrozenthrone () {        hero.attackfrozenthrone ();    }    @Override public    void Attackworldtree () {        hero.protectworldtree ();    }}
As we can see above, the hero of the near-Guardian Corps is called by a method that looks like the mission of the scourge, and actually does the work of the Guardian Corps. Now to answer the question,Why is the adapter essentially fulfilling its own faction's mission? We say that if your outlet is to provide 220V of AC power, and our computer needs 20V. We used an adapter to fit so that 220V of AC power could be provided to the computer normally. The computer is actually powered directly by a 20V voltage, just as it looks like it is powered by a 220V voltage.

Adapter Implementation Improvements (DISGUISERADAPTER2):

Okay, here's the adapter mode, that's basically it. However, in the constructor of the adapter above, a near-guardian object is passed in. This is something that seems logical and confusing. The following is an improvement in the way anonymous classes are used, so that we really feel as if we were using a hero of the Scourge Corps and did the same thing as the Guardian Corps.

public class DisguiserAdapter2 implements Disasterhero {    //hold a near Guardian Regiment object    private Konoehero hero;        Public DisguiserAdapter2 () {        initevent ();    }        private void Initevent () {        if (Hero = = null) {            hero = new Konoehero () {                                @Override public                void Protectworld Tree () {                    System.out.println ("I am a near-Guardian, I am attacking the frozen throne.") ");                }                                @Override public                void Attackfrozenthrone () {                    System.out.println ("I am a near-guardian, I am protecting the World Tree.") ");                }            };        }    }        @Override public    void Protectfrozenthrone () {        hero.attackfrozenthrone ();    }    @Override public    void Attackworldtree () {        hero.protectworldtree ();<p>    }}</p>

test class:
public class Worldofwarcraft {public static void main (string[] args) {System.out.println ("\ n----------        ----------mission of the Guards--------------------");        Konoehero Konoehero = new Traxex ();        Konoehero.attackfrozenthrone ();                Konoehero.protectworldtree ();        System.out.println ("\ n--------------------the mission--------------------of the Scourge Corps");        Disasterhero Disasterhero = new Mercurial ();        Disasterhero.protectfrozenthrone ();                Disasterhero.attackworldtree ();        System.out.println ("\ n--------------------adapter did such a thing--------------------");        Disguiseradapter adapter = new Disguiseradapter (new Traxex ());        Adapter.attackworldtree ();                Adapter.protectfrozenthrone ();        System.out.println ("\ n--------------------adapter did such a thing--------------------");        DisguiserAdapter2 adapter2 = new DisguiserAdapter2 ();        Adapter2.attackworldtree ();    Adapter2.protectfrozenthrone (); }}
Test Results:
--------------------the mission of the Guards--------------------I am traxex, I am attacking the frozen throne. I am traxex, I am protecting the tree of the world. --------------------the task of the scourge--------------------I am mercurial, I am protecting the frozen throne. I am mercurial, I am attacking the tree of the world. --------------------adapter did such a thing--------------------I was traxex, I was protecting the tree of the world. I am traxex, I am attacking the frozen throne. --------------------adapter did such a thing--------------------I was near the guard, and I was attacking the frozen throne. I am the Guardian Army, I am protecting the World tree.

GitHub source Download:

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

Java design 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.