Classes: Identifying Classes and Candidates for Classes, Attributes, and Services
Definition
A class is a user-defined data type that represents a group of objects sharing common structure and behavior.
Attributes are the properties or data fields that describe the state of a class.
Services are the actions or behaviors that a class provides, usually implemented as methods.
Identifying classes means analyzing a problem domain to determine which entities should be modeled as classes, which properties should become attributes, and which responsibilities should become services.
Main Content
1. Identifying Classes
What a class represents
- A class usually represents a real-world object, person, place, event, role, or conceptual entity.
- Examples include
Student,BankAccount,Car,Invoice,Library, andOrder. - A class should have a clear purpose in the system and a well-defined responsibility.
- A good class is usually cohesive, meaning its attributes and services all relate to one central idea.
How to identify candidate classes
- A common technique is to read the problem statement carefully and look for nouns and noun phrases.
- Nouns may indicate possible classes, but not every noun should be turned into a class.
- Ask questions such as:
- Does this noun represent a meaningful entity in the problem domain?
- Does it have its own identity?
- Does it need its own data and behavior?
- Will it be useful in the software model?
- Example: In a school management system, words like
Student,Teacher,Course,Classroom, andExamare likely candidates for classes. - However, words like
name,address, ordateare usually attributes, not classes.
Choosing the best candidate classes
- Candidate classes should be:
- Relevant to the domain
- Stable over time
- Distinct in responsibility
- Useful for abstraction
- Avoid creating classes for:
- Purely descriptive words
- Temporary values
- Actions that should be services
- Overly detailed internal implementation objects unless necessary
- For example, in an online shopping system:
Customer,Product,Cart,Order, andPaymentare strong class candidates.Quantitymay be an attribute ofOrderItem, not a standalone class in many cases.
2. Identifying Attributes
What attributes are
- Attributes are the data members or properties that store the state of a class.
- They describe what an object is like rather than what it does.
- Example:
Studentmay have attributes such asstudentId,name,age,department, andemail.BankAccountmay have attributes such asaccountNumber,balance, andaccountType.
How to identify candidate attributes
- Look for descriptive words in the problem statement, often adjectives or data values associated with a class.
- Ask:
- Does this property describe the object?
- Should this value be stored inside the object?
- Is it essential to the object’s identity or state?
- Example: For
Car, attributes may includemodel,color,speed,fuelLevel, andregistrationNumber. - For
Book, attributes may includetitle,author,ISBN,publisher, andprice.
Characteristics of good attributes
- Attributes should be:
- Meaningful
- Required for the class’s purpose
- Specific to the class
- Not redundant
- Good design avoids storing derived data unnecessarily.
- For example, in a
Rectangleclass:lengthandwidthare good attributes.areais usually better as a computed property or service result, not stored permanently, because it can be calculated from length and width.
- This reduces inconsistency and improves maintainability.
Common attribute types
- Simple attributes: single values such as
name,age,salary - Composite attributes: made of smaller parts, such as
addresscontaining street, city, and postal code - Derived attributes: computed from other attributes, such as
agederived fromdateOfBirth - Optional attributes: may or may not have a value, such as
middleName - Example:
- In a
Customerclass: customerIdmay be a simple attributeaddressmay be compositeagemay be derivedfaxNumbermay be optional
- In a
3. Identifying Services
What services are
- Services are the actions, operations, or behaviors that a class can perform.
- They represent what an object does.
- Services are usually implemented as methods.
- Example:
BankAccountmay have services such asdeposit(),withdraw(), andcheckBalance().Studentmay have services such asenrollCourse(),updateProfile(), andcalculateGPA().
How to identify candidate services
- Look for verbs and verb phrases in the problem statement.
- Ask:
- What actions are required?
- Which class should be responsible for each action?
- Does the action belong to the object itself?
- Example:
- In a library system:
borrowBook(),returnBook(), andreserveBook()are services.- In an e-commerce system:
addToCart(),removeFromCart(),placeOrder(), andprocessPayment()are services.
Types of services
- Accessors: methods that retrieve information, such as
getName()orgetBalance() - Mutators: methods that change state, such as
setAddress()ordeposit() - Business services: methods that perform domain-specific operations, such as
calculateDiscount()orissueFine() - Control services: methods that coordinate tasks, such as
login(),submitForm(), orgenerateReport() - Example:
- In a
Employeeclass: getSalary()is an accessorsetDepartment()is a mutatorcalculateTax()is a business service
- In a
Relationship between attributes and services
- Services often operate on attributes.
- A class should protect its attributes by allowing controlled access through services.
- This is a key idea in encapsulation.
- Example:
BankAccountshould not allow direct external modification ofbalance.- Instead,
deposit()andwithdraw()should change the balance under controlled rules.
- This ensures valid state and prevents errors such as negative balances or invalid updates.
Working / Process
1. Study the problem statement carefully
- Read the domain description and identify the important nouns, noun phrases, and verbs.
- Understand the real-world scenario before designing classes.
- Separate meaningful domain concepts from irrelevant words.
2. List candidate classes, attributes, and services
- Mark nouns as possible classes or attributes.
- Mark verbs as possible services.
- Organize the extracted items into groups.
- Example:
- Problem: “A student enrolls in a course and pays fees.”
- Possible classes:
Student,Course,Fee - Possible attributes:
studentId,courseCode,amount - Possible services:
enroll(),payFees()
3. Evaluate and refine the model
- Check whether each class has a clear responsibility.
- Remove unnecessary or duplicate candidates.
- Decide whether a noun is better as a class, attribute, or service.
- Make sure the final design supports encapsulation and abstraction.
- Example:
Addressmay be a class in a shipping system, but only an attribute in a simple contact system.
- Finalize the class diagram or object model with the selected classes, attributes, and services.
Advantages / Applications
- Helps in building software that reflects the real-world domain accurately
- Improves encapsulation by hiding internal data and exposing controlled services
- Makes systems easier to understand, maintain, test, and extend
- Supports reuse by creating well-defined classes that can be used in multiple parts of a program
- Widely applied in system analysis, object-oriented programming, UML class diagram design, and software architecture
Summary
- Classes are identified by analyzing important entities in the problem domain.
- Attributes describe the state of a class, and services describe its behavior.
- Careful selection of classes, attributes, and services leads to clean and effective object-oriented design.
- Important terms to remember: class, object, attribute, service, candidate class, encapsulation, abstraction