Overview
Many people have difficulties when they try to think about a problem in an object oriented way. I understand that. It can be quite difficult. In an attempt to help, I am going to try to talk my way through a simple problem, a magazine subscription service. I am going to focus mostly on the domain objects. Domain objects are those that relate directly to the "domain" of the problem. They are often called the business objects and they reflect the things core to the problem.
Other kinds of objects are data objects which handle transferring data to and from the main business objects from databases or files, display objects which create the forms and reports that are used to access the domain objects and helper objects that perform various necessary tasks.
The Problem
First you need to define the problem as clearly as possible.
The program is for a magazine subscription service. A customer can subscribe to various magazines for physical or digital delivery or both. Subscriptions can be for different lengths of time and vary in price. Magazines can be weekly, monthly or quarterly. A customer can cancel a subscription at any time. A notice is sent to customers one month before the subscription ends letting them know it is ending and encouraging them to renew.
Here are some use cases for the problem. Some activities such as subscribing, unsubscribing, or renewing require logins. I should also write out the logic of each use case and do activity diagrams to clarify the processes, but I am focusing on the objects for this blog. Let me just say, use cases and activity diagrams can be critical to getting the clear understanding of the problem that is essential in defining the objects.


Thinking About the Objects
First, think about the nouns involved in the problem. There are magazine, customer, subscription, notice, and a login.Each of these will be an object. That is where you start. For now we will leave the login to be dealt with later.

Determining the Attributes
Attributes or fields are things that describe the class. A customer, for instance, will have a customer number, a name, an address, an email. A customer will also have a list of subscriptions. A list is an object in Java or python that acts like an open ended array and can store objects such as subscriptions.
I have added fields to each of the objects. The minus sign - in front of the fields means they are private to the class. It is a rule of objects, part of the principle of encapsulation, that all class members should be private. They are exposed to other classes through get and set methods when necessary.
Look at the attributes or fields that I have added to each object. It is quite possible there are more that should be added. I am trying to keep the example fairly simple, but in real life, leaving things out can lead to difficulties.

A couple of additional notes:
Things should only be in one place. If you find yourself repeating the same attributes in multiple classes, you should extract those attributes into their own classes.
You might also notice, that I gave every field a data type. this is not absolutely necessary, and the types can vary with different languages, but it does help when translating the classes into code.
Methods
Methods are things a class can do. A customer for instance can subscribe to a magazine and unsubscribe. There are also special methods. A constructor with initializes the class and a string method which will output some element or description of a class as a string. (By default it usually outputs the class name.). There are also the gets and sets that allow an outside class to access or change a class' attribute values.
To know what methods to add requires that you think through the processes involved. This is where the activity diagrams would be helpful. What is involved in a customer subscribing to a magazine? What is involved in a customer unsubscribing?
For our purposes a customer subscribes by adding a subscription object to his or her list of subscriptions and removes one by deleting it from the list. We will assume an renewal is an editing of an existing subscription.
I am going use python conventions when doing these diagrams because that is what most of you have experience with, though I will say up front, python is not the best object oriented language. Java is far better.
Following python, the __init__() will be for the constructor method and __str__() for the to string method.
Here is the customer class. Note I have methods to add, remove and edit the subscription list, plus methods to see name and id. I added a set for address so that the customer can change addresses if needed.

Here are the other three classes. Note they really don't do much. They are there mostly to store data. It is much more convenient to have an object store multiple fields than to try to pass around all those fields individually. You can just pass a magazine object rather than having to pass name and price and type. I put gets for most of the fields, but only a very few sets. Most of the values are passed in with the constructor.

Refining the Model
Our classes and methods so far reveal a problem. Our magazine class represents a single magazine. We need something that show all the magazine objects. Also when are Notices sent and how?
The first thing that I will do is add a Product class, That will store a list of all the magazines. It will have methods to get the whole list, get one magazine by id, remove a magazine by id and edit a magazine.

On the same note, our customer class only represents a single customer. We need a class to control all our customers.

Finally, I have added a class to monitor the subscripions for expiration dates.

There a several other classes that would be involved here. Classes to write and retrieve all the data from the database. Classes to capture data from forms to pass to the data classes and classes to display the data.
The Next blog will talk about how these classes relate to each other.
No comments:
Post a Comment