Skip to Main Content
Prefactoring
book

Prefactoring

by Ken Pugh
September 2005
Intermediate to advanced content levelIntermediate to advanced
240 pages
6h 28m
English
O'Reilly Media, Inc.
Content preview from Prefactoring

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Understanding Unittest.Mock

Understanding Unittest.Mock

Mario Corchero
Java™ Performance

Java™ Performance

Charlie Hunt, Binu John

Publisher Resources

ISBN: 0596008740Supplemental ContentCatalog PageErrata