We will now apply our knowledge of interfaces to a little restructuring of the Acme case study. A major benefit of using interfaces is that they raise the level of abstraction somewhat, helping you to understand the system by way of the interface contracts, without worrying about how the system is implemented. | As usual, our case study code is in the CaseStudy directory for this chapter. | The Contracts There are two main sets of contracts in the Acme Travel Agency Case Study. The first specifies operations on customers, and the second, operations involving hotels. Customer Contract The ICustomer interface shown below specifies the methods to be used by clients in the Acme Travel Agency system. Public Interface ICustomer Function RegisterCustomer(_ ByVal firstName As String, _ ByVal lastName As String, _ ByVal emailAddress As String) As Integer Sub UnregisterCustomer(ByVal id As Integer) Function GetCustomer(_ ByVal id As Integer) As ArrayList Sub ChangeEmailAddress(_ ByVal id As Integer, _ ByVal emailAddress As String) End Interface The RegisterCustomer , UnregisterCustomer , and ChangeEmailAddress method definitions are exactly the same as the methods we implemented in the Customers class. The GetCustomer method is new. Previously, we had a ShowCustomers method, which displayed a list of customers to the console. This method was strictly temporary. For general use we want to return data and let the client decide what to do with it. The GetCustomer method returns information about one or all customers in an array list. If -1 is passed for the ID, the list will contain all the registered customers. Otherwise, the list will contain the customer information for the customer with the given ID. If no customer has that ID, the list will be empty. Hotel Contracts We next look at the functionality of the class HotelBroker . The methods divide fairly naturally into three groups. -
Hotel information, such as the cities where hotels are available and the hotels within a city -
Hotel administration, such as adding or deleting a hotel or changing the number of rooms and rate of a hotel -
Hotel reservations, such as booking or canceling a reservation or obtaining a list of reservations Accordingly, we create three interfaces for the HotelBroker . These interfaces are defined in AcmeDefinitions.vb . Public Interface IHotelInfo Function GetCities() As ArrayList Function GetHotels() As ArrayList Function GetHotels(_ ByVal city As String) As ArrayList End Interface Public Interface IHotelAdmin Function AddHotel(_ ByVal city As String, _ ByVal name As String, _ ByVal numberRooms As Integer, _ ByVal rate As Decimal) As String Function DeleteHotel(_ ByVal city As String, _ ByVal name As String) As String Function ChangeRooms(_ ByVal city As String, _ ByVal name As String, _ ByVal numberRooms As Integer, _ ByVal rate As Decimal) As String End Interface Public Interface IHotelReservation Function MakeReservation(_ ByVal customerId As Integer, _ ByVal city As String, _ ByVal hotel As String, _ ByVal checkinDate As DateTime, _ ByVal numberDays As Integer) _ As ReservationResult Sub CancelReservation(ByVal id As Integer) Function FindReservationsForCustomer(_ ByVal customerId As Integer) As ArrayList End Interface The Implementation We examined the Step 1 implementation of the hotel brokerage system in detail in Chapter 5. The Step 2 implementation uses collections in place of arrays, and it passes information to the client rather than display information directly. Structures One detail of our implementation concerns the data structures used to pass lists to the client. We use the ArrayList class. But what do we store in each array list? We could use Customer objects and Hotel objects. The problem here is that these classes have implementation-specific data in them that the client code does not need, as well as the information fields that the client code does care about. To obtain implementation-neutral representations, we introduce several structures. In Customers.vb we define the CustomerListItem structure for passing customer information. Public Structure CustomerListItem Public CustomerId As Integer Public FirstName As String Public LastName As String Public EmailAddress As String End Structure In AcmeDefinitions.vb we define structures for hotels, reservations, and reservation results. Public Structure HotelListItem Public City As String Public HotelName As String Public NumberRooms As Integer Public Rate As Decimal End Structure Public Structure ReservationListItem Public CustomerId As Integer Public ReservationId As Integer Public HotelName As String Public City As String Public ArrivalDate As DateTime Public DepartureDate As DateTime Public NumberDays As Integer End Structure Public Structure ReservationResult Public ReservationId As Integer Public ReservationCost As Decimal Public Rate As Decimal Public Comment As String End Structure The ReservationResult returns a ReservationId of -1 if there is a problem, along with an explanation of the problem in the Comment field. Otherwise, "OK" is returned in the Comment field. We invite you to examine the code in the CaseStudy folder and to build and run the program. |