Java design mode-adapter (Adapter)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/guolin_blog/article/details/9400141

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

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

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

Well, you reluctantly accepted, for this sudden new demand, you are already accustomed to.

Your leader to you a specific description of the demand, your game currently has three clothing, a service has been open for some time, the second and three are new clothes. Design of the interface is very light, you only need to call Utility.getonlineplayercount (int), the number of incoming per service can be obtained to the corresponding amount of online players, such as the incoming 1, the second pass 2, the three services are passed in 3. If you pass in a non-existent service, it will return-1. Then you just have to assemble the resulting data into XML, and the specific display function is done by your leader.

Well, it sounds like it's not very complicated, and if it's time to start the work now, you're going to have to hit the code right away.

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

[Java] view plain copy  
    1. Public interface Playercount {
    2. String getServerName ();
    3. int Getplayercount ();
    4. }

The three statistical classes are then defined to implement the Playercount interface, which corresponds to three different services, as shown below:

[Java] view plain copy  
  1. Public class ServerOne implements Playercount {
  2. @Override
  3. Public String getServerName () {
  4. return "one service";
  5. }
  6. @Override
  7. public int Getplayercount () {
  8. return Utility.getonlineplayercount (1);
  9. }
  10. }
[Java] view plain copy  
  1. Public class Servertwo implements Playercount {
  2. @Override
  3. Public String getServerName () {
  4. return "second service";
  5. }
  6. @Override
  7. public int Getplayercount () {
  8. return Utility.getonlineplayercount (2);
  9. }
  10. }
[Java] view plain copy  
  1. Public class Serverthree implements Playercount {
  2. @Override
  3. Public String getServerName () {
  4. return "three suits";
  5. }
  6. @Override
  7. public int Getplayercount () {
  8. return Utility.getonlineplayercount (3);
  9. }
  10. }

Then define a Xmlbuilder class that encapsulates the data for each service in XML format, with the following code:

[Java] view plain copy  
  1. Public class Xmlbuilder {
  2. public static String buildXML (Playercount player) {
  3. StringBuilder builder = new StringBuilder ();
  4. Builder.append ("<root>");
  5. Builder.append ("<server>"). Append (Player.getservername ()). Append ("</server>");
  6. Builder.append ("<player_count"). Append (Player.getplayercount ()). Append ("</player_count>");
  7. Builder.append ("</root>");
  8. return builder.tostring ();
  9. }
  10. }

In that case, all the code is done, and if you want to see the number of players online, just call:

[Java] view plain copy  
    1. Xmlbuilder.buildxml (new ServerOne ());

View two online players only need to call:

[Java] view plain copy  
    1. Xmlbuilder.buildxml (new Servertwo ());

To see the number of players in the three-line game only call:

[Java] view plain copy  
    1. Xmlbuilder.buildxml (new Serverthree ());

Hey? You find that when you look at the number of players online, the return value is always-1, and it's normal to look at the second and the third suits.

You have to call up your leader: "I feel I write the code is not a problem, but query one serving the number of players online always return-1, why this?" ”

Oh "Your leader suddenly reminds me," This is my problem, before you explain clearly. Since one of our services has been open for some time, the ability to query the number of online players has long been available, using the Serverfirst class. At that time wrote Utility.getonlineplayercount () This method is mainly aimed at the new two and three services, did not put a service query function to do again. ”

Hearing your leader said so, you immediately relieved: "Then you modify the Utility.getonlineplayercount () is good, should not I what matter?" ”

"... It was supposed to be like this ... However, utility and Serverfirst these two classes have been hit into the jar bag, can not be modified ah ... "Your leader is a little awkward.

What This is not the pit father, do I have to change the interface? "You are already in tears."

"This is not necessary, in this case you can use the adapter mode, this mode is to solve the problem of incompatibility between the interface appears." ”

In fact, the use of the adapter mode is very simple, the core idea is as long as the two incompatible interface can be normal docking on the line. In the above code, Xmlbuilder uses the Playercount interface to assemble the XML, and Serverfirst does not implement the Playercount interface. At this point, an adapter class is needed to bridge the Xmlbuilder and Serverfirst, and there is no doubt that ServerOne will act as the role of the adapter class. Modify the code for the ServerOne as follows:

[Java] view plain copy  
  1. Public class ServerOne implements Playercount {
  2. private Serverfirst Mserverfirst;
  3. Public ServerOne () {
  4. Mserverfirst = new Serverfirst ();
  5. }
  6. @Override
  7. Public String getServerName () {
  8. return "one service";
  9. }
  10. @Override
  11. public int Getplayercount () {
  12. return Mserverfirst.getonlineplayercount ();
  13. }
  14. }

This through the serverone of the adaptation, Xmlbuilder and Serverfirst between the successful completion of docking! We don't even have to know the Serverfirst class when we use it, just create an instance of ServerOne as usual.

It is important to note that the adapter pattern is not the kind of pattern that makes the architecture more reasonable, but more often it acts as a firefighter and helps solve the problem of interface mismatches due to unreasonable design of the previous architecture. A better approach is to try to think about what might happen later in the design, and don't learn from your leader on this issue.

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

Java design mode-adapter (Adapter)

Related Article

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.