Java design pattern Dialysis: combination (composite)

Source: Internet
Author: User

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 Getallnovels method can obtain all the novel names, and then the novel name into the Getbrowsecount method can get the volume of the book, the novel into the Getsalecount method can get the book sales. You currently have only a few known APIs to use, so start doing it!

public int Getnovelsbrowsecount () {  
    int browsecount = 0;  
    list<string> allnovels = Getallnovels ();  
    for (String novel:allnovels) {  
        Browsecount + = Getbrowsecount (novel);  
    }  
    return browsecount;  
}  
      
public int Getnovelssalecount () {  
    int salecount = 0;  
    list<string> allnovels = Getallnovels ();  
    for (String novel:allnovels) {  
        Salecount + = Getsalecount (novel);  
    }  
    return salecount;  
}

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:

public int 

Getcomputerbooksbrowsecount () {  
    int browsecount = 0;  
    list<string> allcomputerbooks = Getallcomputerbooks ();  
    for (String computerbook:allcomputerbooks) {  
        Browsecount + = Getbrowsecount (Computerbook);  
    }  
    return browsecount;  
}  
      
public int Getcomputerbookssalecount () {  
    int salecount = 0;  
    list<string> allcomputerbooks = Getallcomputerbooks ();  
    for (String computerbook:allcomputerbooks) {  
        Salecount + = Getsalecount (Computerbook);  
    }  
    return salecount;  
}

In addition to using the Getallcomputerbooks 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 interface, which has two methods to be implemented:

Public interface Statistics {  
      
    int getbrowsecount ();  
          
    int Getsalescount ();  
      
}

Then define a Novelstatistics class for statistical fiction books that implement the two methods defined in the interface:

public class Novelstatistics implements Statistics {  
      
    @Override public
    int Getbrowsecount () {  
        int Browsecount = 0;  
        list<string> allnovels = Getallnovels ();  
        for (String novel:allnovels) {  
            Browsecount + = Getbrowsecount (novel);  
        }  
        return browsecount;  
    }  
      
    @Override public
    int Getsalescount () {  
        int salecount = 0;  
        list<string> allnovels = Getallnovels ();  
        for (String novel:allnovels) {  
            Salecount + = Getsalecount (novel);  
        }  
        Return Salecount  
    }  
      
}

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.