Classes: identifying classes and candidates for Classes Attributes and Services

Comprehensive study notes, diagrams, and exam preparation for Classes: identifying classes and candidates for Classes Attributes and Services.

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, and Order.
  • 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, and Exam are likely candidates for classes.
  • However, words like name, address, or date are 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, and Payment are strong class candidates.
    • Quantity may be an attribute of OrderItem, 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:
    • Student may have attributes such as studentId, name, age, department, and email.
    • BankAccount may have attributes such as accountNumber, balance, and accountType.

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 include model, color, speed, fuelLevel, and registrationNumber.
  • For Book, attributes may include title, author, ISBN, publisher, and price.

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 Rectangle class:
    • length and width are good attributes.
    • area is 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 address containing street, city, and postal code
  • Derived attributes: computed from other attributes, such as age derived from dateOfBirth
  • Optional attributes: may or may not have a value, such as middleName
  • Example:
    • In a Customer class:
    • customerId may be a simple attribute
    • address may be composite
    • age may be derived
    • faxNumber may be optional

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:
    • BankAccount may have services such as deposit(), withdraw(), and checkBalance().
    • Student may have services such as enrollCourse(), updateProfile(), and calculateGPA().

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(), and reserveBook() are services.
    • In an e-commerce system:
    • addToCart(), removeFromCart(), placeOrder(), and processPayment() are services.

Types of services

  • Accessors: methods that retrieve information, such as getName() or getBalance()
  • Mutators: methods that change state, such as setAddress() or deposit()
  • Business services: methods that perform domain-specific operations, such as calculateDiscount() or issueFine()
  • Control services: methods that coordinate tasks, such as login(), submitForm(), or generateReport()
  • Example:
    • In a Employee class:
    • getSalary() is an accessor
    • setDepartment() is a mutator
    • calculateTax() is a business service

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:
    • BankAccount should not allow direct external modification of balance.
    • Instead, deposit() and withdraw() 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:
    • Address may 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