I heard that your company recently launched a new E-book reading application, the market reaction is very good, the application of the book Mall, users can choose to buy their favorite books. Your company also attaches great importance to this project, increased the input, decided to add some additional function to this application.
Well, you know you're not going to get away with this, and it didn't take long for your leader to find you. He tells you that the current application counts the volume and volume of each book, but now you want to add the ability to count the amount of browsing and sales and the total volume and volume of all books, and hopefully you can do it.
The work of the leadership arrangement of course is to evade, you can only bite the bullet, but fortunately this function does not look very complicated.
You like to read novels, then start with the statistical function of the novel. First, through the Get_all_novels method can obtain all the novel names, and then the novel name into the Get_browse_count method can get the volume of the book, the novel into the Get_sale_count method can get the book sales. You currently have only a few known APIs to use, so start doing it!
def
get_novels_browse_count
browse_count = 0
all_novels = get_all_novels ()
All_novels.each do |novel |
Browse_count + = Get_browse_count (novel) End
Browse_count end
def get_novels_sale_count
Sale_ Count = 0
all_novels = get_all_novels ()
All_novels.each do |novel|
Sale_count + = Get_browse_count (novel) End
Sale_count End
Soon you will write down the above two methods, both by acquiring all the novel names and then calculating the volume and volume of each novel, and then adding the total to the results.
The statistics of the novel are complete, and then you start to do the statistical functions of computer books, the code looks like this:
def get_computer_books_browse_count
browse_count = 0
all_computer_books = get_all_computer_books ()
All_ Computer_books.each do |computer_book|
Browse_count + = Get_browse_count (computer_book) End
Browse_count end
def Get_computer_books_sale_ Count
sale_count = 0
all_computer_books = get_all_computer_books ()
All_computer_books.each do | computer_book|
Sale_count + = Get_browse_count (computer_book) End
Sale_count End
In addition to using the Get_all_computer_books method to get all the computer titles, the other code basics are the same as in the novel statistics.
Now you have completed the statistical functions of the two categories of books, there are medical, natural, historical, legal, political, philosophical, tourism, food and so on and so on books. You suddenly realize the seriousness of some problems, the amount of work is not much, but then write down, your method will explode, so many ways to look at it, not to mention how to use.
At this time you had to ask your leader for help, and he explained your confusion. See your leader thinking for a moment, and then confidently tell you that using combination mode can not only easily eliminate your confusion, but also the ability to complete the function excellently.
He immediately showed you the code operation, first defines a statistics class, which has two methods:
Class
Statistics
def get_browse_count
raise "You should override this method in subclass."
End
def get_sale_count
raise "Your should override this method in subclass."
End End
Both methods simply throw an exception because the two methods need to be overridden in the subclass.
Then define a Novelstatistics class for statistical fiction books, inherit the statistics class that you just defined, and rewrite the two methods in statistics:
Class Novelstatistics < Statistics
def get_browse_count
browse_count = 0
all_novels = get_all_novels () C4/>all_novels.each do |novel|
Browse_count + = Get_browse_count (novel) End
Browse_count end
def get_sale_count
sale_count = 0
all_novels = get_all_novels ()
All_novels.each do |novel|
Sale_count + = Get_browse_count (novel) End
Sale_count end