[Original] Java design pattern-adapter)

Source: Internet
Author: User


Reprinted please indicate the source: http://blog.csdn.net/guolin_blog/article/details/9400141

Early in the morning, your leader rushed to find you: "Fast, fast, urgent task! Chinajoy is about to begin recently. The boss asked for an intuitive way to view the number of online users of each server in our new game ."

You looked at the date, right! This is where we are about to start. It is clear that we have already started! How can this be done?

"It doesn't matter ." Your leader comforted you: "The function is actually very simple. All interfaces are provided. You just need to call it ."

Well, you accept it for a while. You have long been used to this sudden new demand.

Your leader gave you a detailed description of the requirements. Your game currently has three servers, one server has been open for a while, and the other two and the third servers are all new servers. The designed interface is very lightweight. You only need to call utility. getonlineplayercount (INT): the number of online players in the corresponding server can be obtained by passing in the corresponding value of each server. For example, if the server is 1, the server is 2, and the server is 3, the server is 3. If you input a server that does not exist,-1 is returned. Then you just need to assemble the data into XML, and the specific display function is completed by your leader.

Well, it sounds like the feature is not very complicated. If it seems that it is still time to start the work now, you will immediately start typing the code.

First, define an interface playercount for counting the number of online users. The Code is as follows:

public interface PlayerCount {String getServerName();int getPlayerCount();}

The three statistical classes are defined to implement the playercount interface, which corresponds to three different servers, as shown below:

Public class serverone implements playercount {@ overridepublic string getservername () {return "" ;}@ overridepublic int getplayercount () {return utility. getonlineplayercount (1 );}}
Public class servertwo implements playercount {@ overridepublic string getservername () {return "" ;}@ overridepublic int getplayercount () {return utility. getonlineplayercount (2 );}}
Public class serverthree implements playercount {@ overridepublic string getservername () {return "three servers" ;}@ overridepublic int getplayercount () {return utility. getonlineplayercount (3 );}}

Define an xmlbuilder class to encapsulate data of different servers into XML format. The Code is as follows:

public class XMLBuilder {public static String buildXML(PlayerCount player) {StringBuilder builder = new StringBuilder();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();}}

In this way, all the code is complete. To view the number of online players in a server, you only need to call:

XMLBuilder.buildXML(new ServerOne());

To view the number of online gamers in Server 2, you only need to call:

XMLBuilder.buildXML(new ServerTwo());

To view the number of online players in three servers, you only need to call:

XMLBuilder.buildXML(new ServerThree());

Why? When you view the number of online players in a server, the returned value is always-1. It is normal to view both Server 2 and Server 3.

You have to call your leader: "I feel that the code I wrote is okay, but the number of online players in a server always returns-1. Why ?"

"Oh !" Your leader suddenly remembered, "This is my problem, and I have not explained it clearly before. Since our server has been open for a while, the function of querying the number of online players has long been available, using the serverfirst class. At that time, the utility. getonlineplayercount () method was mainly used to repeat the query function of a server for the newly opened Server 2 and Server 3 ."

When you heard your leader say this, you suddenly breathed a sigh of relief: "You just need to modify utility. getonlineplayercount (). Should it be okay ?"

"Success... It should have been like this... However, the utility and serverfirst classes have been pushed to the jar package and cannot be modified ..." Your leader is a bit difficult.

"What? Isn't it a pitfall? Should I change the interface ?" You have burst into tears.

"This is not necessary. In this case, the adapter mode can be used. This mode is used to solve the problem of incompatibility between interfaces ."

In fact, the use of the adapter mode is very simple, the core idea is that as long as two incompatible interfaces can be normally connected. In the code above, xmlbuilder uses the playercount interface to assemble XML, while serverfirst does not implement the playercount interface, at this time, an adapter class is required to build a bridge between xmlbuilder and serverfirst. serverone will undoubtedly act as the adapter class. Modify the serverone Code as follows:

Public class serverone implements playercount {private serverfirst mserverfirst; Public serverone () {mserverfirst = new serverfirst () ;}@ overridepublic string getservername () {return "";} @ overridepublic int getplayercount () {return mserverfirst. getonlineplayercount ();}}

Through serverone adaptation, the interaction between xmlbuilder and serverfirst is successful! We don't even need to know the serverfirst class when using it. We just need to create a serverone instance.

It is worth noting that the adapter mode is not the one that makes the architecture more reasonable. More often, it is only used as a fireman, it helps solve interface mismatch problems caused by unreasonable preliminary architecture design. A better way is to try to consider the possible situations in the future during the design. Do not learn from your leader on this issue.

Adapter: 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 interface incompatibility to work together.

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.