1.2 Designing Classes

Introduction

In order for our classes and objects to be of use, and to work together in a logically sound manner, we need to ensure that we design them correctly. Remember that objects are a collection of data and functions. Also remember that objects are meant to model real world objects and concepts. It is important to keep this in mind, otherwise our class definitions become blurred, confusing, and ultimately ends up with bad code being written.


Attributes and Functions

Attributes

The data component of objects is handled by the attributes. The attributes are the collection of variables which store the values which make the specific object unique from the other objects of the same class. Consider the following example: If we have a class Contact with the following attributes:

CONTACT
---------------
first_name
last_name
email

Then suppose we create three objects from the Contact class. Here are the three objects:

contact_1.first_name = "Luke"
contact_1.last_name = "Skywalker"
contact_1.email = "luke.s@theforce.com"

contact_2.first_name = "Darth"
contact_2.last_name = "Vader"
contact_2.email = "boss@deathstar.com"

contact_3.first_name = "Obi-Wan"
contact_3.last_name = "Kenobi"
contact_3.email = "obiwan.k@theforce.com"

Here, we see that the values of the attributes first_name, last_name, and email are what make each object unique from each other, not the actual attributes themselves.

When deciding on the attributes for a class, try to limit the attributes to the following:

  1. Variables which describes the "thing" the class is modeling.
  2. Variables which make the "thing" unique.
  3. Variables which describe what the "thing" is made up of.

Example 1: You should do this

SUPPLIER
------------------
supplier_id
company_name
phone
fax
email
address
website

Example 2: Do NOT do this

SUPPLIER
-------------------
director_name
director_email
product_1_name
product_1_price
product_2_name
product_2_price
num_sales

In the above examples, hopefully it should be obvious to everyone why the first example is shows attributes of a well designed class, whereas the second example is poorly designed. In the first example, each attribute is a characteristic of a supplier company. However, in example 2, the class has incorporated characteristics of other objects, such as an employee and product, as well as including an attribute, num_sales which is not really a characteristic, but a value which is computed from other data.

Functions

So far, we have just been describing classes as a label and set of attributes. This would be equivalent to describing a table or spreadsheet. What makes an object special, is the fact that the functionality and data co-exist. So, in addition to the attributes of a class, we also define the functions for that class. For example:

CLIENT
-------------------
client_id
first_name
last_name
email
phone
-------------------
get_name()
send_email()
show_details()

In the example above, we have defined some functions we might want our object to perform, which operate on the values stored in the attributes for each object of this class. Every time we create an object of this class, it will possess, not only the attributes of the class, but also the functions of the class.

As with the attributes, it is prudent to choose the functions for inclusion with care, so that the class definition can remain logically sound. Thus, only functions which either directly relate to the attributes of the class, and the functions which are innate to the "thing" modeled by the class should be included.


Exercises

Define the attributes and functions for the following classes of objects:

  1. House
  2. Pet
  3. Television
  4. Piano

Continue to the next section


Comments

comments powered by Disqus