Ruby 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.online_player_count (Fixnum), the corresponding value of each suit can be used to get the corresponding number of online players, such as the introduction of 1, two to the incoming 2, the three are introduced into 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 a parent class Playercount for counting the number of online people, the following code:

Class Playercount  
      
    def server_name  
        raise "You should override this method in subclass."
    End
          
    def player_count  
        raise "Your should override this method in subclass."
    End End
      

Then define three statistical classes to inherit Playercount, corresponding to three different suits, as follows:

Class ServerOne < Playercount  
      
    def server_name  
        "one-suit" end
          
    def player_count  
        Utility.online_ Player_count (1)  
    end
      
Class Servertwo < Playercount  
      
    def server_name  
        "Two suits"
    end
          
    def player_count  
        Utility.online_ Player_count (2)  
    end
      
Class Serverthree < Playercount  
      
    def server_name  
        "three suits"
    end
          
    def player_count  
        Utility.online_player_count (3)  
    end
      

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

Class Xmlbuilder  
      
    def self.build_xml player  
        builder = ""
        builder << "<root>"
        Builder < < "<server>" << player.server_name << 

"</server>"
        builder << "<player_ Count> "<< player.player_count.to_s << 

" </player_count> "
        builder <<" </root > "End"
      

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.