Lesson 4 | Classes as data types (Choosing Class Attributes) |
Objective | Describe how to Choose the Data a Class holds |
Describe how to Choose Data a Class holds
Operations
Depending on the programming languages you've used in the past, you may have met "clumpy" data types like this before.
C has structures, and
other languages have records or mixed arrays. If you've worked with such types, you know how convenient it can be to gather related
information together like this. But objects do more than that.
Objects, as we have said before, have both attributes and operations (also called functions or methods or behavior).
Operations are covered in a future lesson.
Choosing the attributes of your classes
The best place to start when listing attributes is your list of responsibilities. Look for words like:
- Knows
- Tracks
- Records
- Maintains
These words generally show you attributes. For example, in the video store, the Customer
class has a responsibility that reads "Tracks name, address, and phone number."
That shows you three attributes of the Customer
class. It is pretty straightforward to decide that they are best kept as string attributes of the class.
Properties
Properties represent structural features of a class. As a first approximation, you can think of properties as corresponding to fields in a class.
The reality is rather involved, as we shall see, but that is a reasonable place to start.
Properties are a single concept, but they appear in two quite distinct notations: attributes and associations.
Although they look quite different on a diagram, they are really the same thing.
Create Class Diagram - Exercise