Java design mode dialysis: Adapters (Adapter)

Source: Internet
Author: User

Early in the morning, your leader rushed over to find you: "Quick, quick, urgent task!" Recently ChinaJoy is about to start, and the boss asks for an intuitive way to see the number of people on the line in each of our new online games. ”

You looked at the date, didn't you! This where is to start immediately, clearly is already started! How could it be too late?

"It's okay. "Your leader Comfort you:" The function is really very simple, the interface has been provided, you just need to call the line. ”

Well, you are reluctant to accept that you are accustomed to this sudden new demand.

Your leader to give you a specific description of the demand, your game currently has three, a dress has been open for some time, two and three clothes are new to open clothes. The design of the interface is very lightweight, you only need to call Utility.getonlineplayercount (int), the corresponding value of each suit can be used to get the corresponding number of online players, such as the introduction of 1, two into the 2, the three are incoming 3. If you pass in a nonexistent suit, you will return-1. Then you just have to assemble the data into XML, and the specific display function is done by your leader.

Well, it doesn't sound very complicated, so if you start starting now it seems like there's still time, so you hit the code right away.

First, define an interface Playercount for counting the number of online people, the following code:

Public interface Playercount {  
      
    String getservername ();  
      
    int Getplayercount ();  
      
}

Then the definition of three statistical classes implements the Playercount interface, corresponding to three different suits, as follows:

public class ServerOne implements Playercount {  
      
    @Override public
    String getServerName () {return  
        "one Suit";  
    }  
      
    @Override public
    int Getplayercount () {return  
        utility.getonlineplayercount (1);  
    }  
      
}
public class Servertwo implements Playercount {  
      
    @Override public
    String getServerName () {return  
        "two suits"; c14/>}  
      
    @Override public
    int Getplayercount () {return  
        utility.getonlineplayercount (2);  
    }  
      
}
public class Serverthree implements Playercount {  
      
    @Override public
    String getServerName () {return  
        "three suits";  
    }  
      
    @Override public
    int Getplayercount () {return  
        utility.getonlineplayercount (3);  
    }  
      
}

Then you define a Xmlbuilder class that encapsulates the data in each service into an XML format, as follows:

public class Xmlbuilder {public  
      
    static String buildXML (Playercount player) {  
        StringBuilder builder = new Stringbu Ilder ();  
        Builder.append ("<root>");  
        Builder.append ("<server>"). Append (Player.getservername ()). Append ("</server>");  
        Builder.append ("<player_count"). Append (Player.getplayercount ()). Append ("</player_count>");  
        Builder.append ("</root>");  
        return builder.tostring ();  
    }  
      

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.