2

I am creating an object oriented design for a cab-calling app like Uber. I have some of the classes listed. I am having trouble in designing behavior between the classes. For example, I have these two classes - Customer and Driver

A customer can call drivers and a driver can accept a ride request from customers.

So my thought is Customer class will have a function contactNearByDrivers(). And Driver class will have a function like getRideRequest()

I have two questions here -

  1. Where should the actual implementation of contacting nearby drivers and confirming driver's response should be? My thought is that there can a utility class, lets say CustomerDriverInteraction. This class will actually have functions for contacting near by drivers and recording their responses. contactNearByDrivers() function in Customer class would instantiate CustomerDriverInteraction and call its functions . Is this a good way of going about it?

  2. Once a driver accepts a request, the customer and driver need to be linked together for a ride. Should there be another class called Ride? Ride class will have a Customer and Driver objects as their members, among other fields. Is this approach correct? What are are the better ways of designing it?

3

Your first problem can be solved by adding another abstraction for an "operational headquarter", lets call the class Headquarter. This one can take the responsibility of contacting the drivers, but it would not be an "utility class", and it would not be instantiated by contactNearByDrivers. Instead, it would be instantiated just once, it would be kept informed about the location of all cars, and each customer can hold a reference to the headquarter to ask there for getting a ride.

To your second question: this depends on the use cases you want to support with your model. If your only use case is "find a free car for a customer which is located nearby", then there is no need for a Ride class. But if your intent is to write a program which can actually calculate the bill for a ride, or does some bookkeeping about the rides which where performed in the past, then you probably need one.

I would heavily recommend to do this incrementally. Don't model too many of these classes "in advance". Try to actually implement a first use case like "driver registers / unregisters at the headquearter" - you will only need two of the classes for it. In the next iteration, implement the use case "customer asks headquearter for a ride" - and find out, which classes you need for implementing this. The lackmus test if you modeled the right classes, and if you modeled them correctly, is if they help you to implement your program, and if the model finally does not contain anything your program actually does not need.

  • 1
    We often forget there is a third-party involved in many interactions. – JeffO Jan 11 '17 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.