9.2. Who's in Charge?

Are CDDiscs rented to customers or do customers rent CDDiscs? That might seem like a redundant question, but the decision of who is in charge underlies many design issues.

9.2.1. One Class in Charge

In the first version of the system, CDDisc had the start_rental and end_rental operations. When start_rental was invoked, it created a Rental. If we want to know what CDDiscs a particular customer is renting at the current time, we have to ask every CDDisc if it is rented to that Customer. CDDisc would need some additional methods:

    class CDDisc
        {
        start_rental(Customer a_customer);
            // Begins a rental for particular customer
        end_rental( );  // Ends the rental for that customer
        Customer retrieve_renting_customer( );
        Customer [] retrieve_all_customers_who_rented( );
        };

Should Customer keep track of what CDDiscs are rented to it? If so, at the time of the rental, we would call a method in the Customer class to record the rental and a corresponding method at the end of the rental:

    class Customer
        {
        begin_rental(CDDisc a_cd_disc);
        end_rental(CDDisc a_cd_disc);
        Rentals [] retrieve_current_rentals( );
        };

In Sam's current system, we have three classes: CDDisc, Rental, and Customer. A Rental object is currently a member of the CDDisc class. But Rental has a reference to the Customer class. So CDDisc is coupled to Rental and Rental is coupled to Customer. This organization worked OK when we had a one-to-one relationship between the classes. In essence, a CDDisc was tied ...

Get Prefactoring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.