Understanding a Problem Domain

In our book example, we wrote the code based on what we wanted to do with books. The idea of a Book type comes from the problem domain: keeping track of books in a bookstore. We thought about this problem domain and figured out what features of a book we cared about.

We might have decided to keep track of the number of pages, the date it was published, and much more; what you decide to keep track of depends exactly on what your program is supposed to do.

It’s common to define multiple related types. For example, if this code was part of an online store, we might also have an Inventory type, perhaps a ShoppingCart type, and much more.

Object-oriented programming revolves around defining and using new types. As you learned in Modules, Classes, and Methods, a class is how Python represents a type. Object-oriented programming involves at least these phases:

  1. Understanding the problem domain. This step is crucial: you need to know what your customer wants (your boss, perhaps a friend or business contact, perhaps yourself) before you can write a program that does what the customer wants.

  2. Figuring out what type(s) you might want. A good starting point is to read the description of the problem domain and look for the main nouns and noun phrases.

  3. Figuring out what features you want your type to have. Here you should write some code that uses the type you’re thinking about, much like we did with the Book code at the beginning of this chapter. This is a lot like the Examples step in the function design recipe, where you decide what the code that you’re about to write should do.

  4. Writing a class that represents this type. You now need to tell Python about your type. To do this, you will write a class, including a set of methods inside that class. (You will use the function design recipe as you design and implement each of your methods.)

  5. Testing your code. Your methods will have been tested separately as you followed the function design recipe, but it’s important to think about how the various methods will interact.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset