Ruby design Pattern Dialysis: Template approach (Template method)

Source: Internet
Author: User

Today you come to work as usual and start your programming work as always.

The project manager tells you that today you want to add a new feature to the server, and you want to write a method that handles the book object, wrapping all the fields of the book object in XML format so that you can easily interact with the client later. and print the log before and after the start of the package so that it is convenient to debug and problem-Orient.

No problem! You think this function is a piece of cake, very confidently began to write code.

The book object code is as follows:

Class book  
    Attr_accessor:book_name,:p ages,:p Rice,: Author, ISBN end

Then write a class that is designed to wrap the book object in XML format:

Class Formatter  
      
    def format_book (book)  
        puts ' format begins ' result
        = ' <book_name>#{book.book_name} </book_name>\n "Result
        + +" <pages>#{book.pages}</pages>\n "result
        + =" <price>#{ Book.price}</price>\n "Result
        + +" <author>#{book.author}</author>\n "result
        + +" < isbn>#{book.isbn}</isbn>\n "
        puts" format finished "result End"
      

The calling code is as follows:

Book = book.new
book.book_name = "Programming Ruby"
book.pages = 830 Book.price = \ book.author =
"Dave T Homas "
book.isbn =" 9787121038150 "
formatter =  formatter.new result
= Formatter.format_book (book)  
puts result

After you've written it, you can't wait to start running and the results are exactly what you expected.

The project manager after reading, is very satisfied with you, the boy efficiency is very high! You are also very proud.

However, two days later, the project manager found you, he said before the need to consider the interaction of the client also includes mobile devices, and mobile devices are compared to eat traffic, in XML format to transmit too much traffic, think the best to change to use the JSON format transmission. However, the previous XML format should also be preserved, preferably by the client specifying which format to use.

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.