Skip to content

Instantly share code, notes, and snippets.

@elliot42
Created February 23, 2011 22:53
Show Gist options
  • Save elliot42/841379 to your computer and use it in GitHub Desktop.
Save elliot42/841379 to your computer and use it in GitHub Desktop.

Q. Why do CorporateDatabase and Store both have borrowRentable() and returnRentable()?

A. Store::borrowRentable() only deals with updating the store's own inventory.

CustomerDatabase::borrowRentable() deals with parsing streams, calling the store to update the store's inventory, as well as creating a new Transaction object and calling the Customer to update itself. You can see the differences below.

When CorporateDatabase is loading transactions, it will look something like this:

switch (first_char_we_found_on_the_stream) {
    case 'S': default_store->displayCategoryInventory(); break;
    case 'H': history(rest_of_stream); break;
    case 'B': borrowRentable(rest_of_stream); break;
    case 'R': returnRentable(rest_of_stream); break;
}

The point is: during design review we decided that CorporateDatabase would have methods to wrap all of the different behaviors it would do during loading transactions. It would not put those behaviors directly into the switch statement except for extremely simple cases.

The stuff that happens in CorporateDatabase::borrowRentable handles all sorts of parsing and dealing with Transactions and Customers.

You'll see below the CorporateDatabase::borrowRentable() uses Store::borrowRentable() as a subcomponent. But Store::borrowRentable() has a narrower scope of responsibility.

bool CorporateDatabase::borrowRentable(istream &stream) {
    int userid;     
    char rentableTypeIdentifier; // look for 'C', 'D' or 'F'

    stream >> userid;
    stream >> rentableTypeIdentifier; // 'C', 'D', 'F' or an invalid code

    // the following line of code is PSEUDO
    Rentable* rentable_used_for_parsing = \
        get_correct_type_of_rentable_out_of_hashfactory(rentableTypeIdentifer)

    Rentable* rentableKey = rentable_used_for_parsing->parseKey(stream);
    Customer* customer = customerList->lookup(c);

    // do error checking here.

    // We now know our Rentable, our Customer, and that we want to Borrow.

    // tell the store to check out the movie.
    // this makes the store do all the work to find the movie
    // and update its own inventory etc.
    Rentable* rentable_we_actually_borrowed = \
        default_store->borrowRentable(rentableKey);
   
    Transaction* t = new BorrowTransaction(customer, 
                                           rentable_we_actually_borrowed);

    // tell the customer she owns the movie.
    customer->borrowRentable(t);

    return true;
}

Does that make sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment