Search Knowledge

© 2026 LIBREUNI PROJECT

Software Engineering & OOAD / Analysis & Design

OO Design: Class Modeling & UML

Object-Oriented Design: Class Modeling & UML

While Analysis focuses on the what, Object-Oriented Design (OOD) focuses on the how. It involves defining the software objects and how they collaborate to fulfill the requirements identified during analysis.

1. Static Modeling: The Class Diagram

The Class Diagram is the heart of OOD. It describes the structure of a system by showing its classes, their attributes, operations, and the relationships among objects.

Class Structure

A class is represented by a rectangle divided into three parts:

  1. Top: Class Name.
  2. Middle: Attributes (Fields).
  3. Bottom: Methods (Operations).

Relationships between Classes

Understanding relationships is critical for creating a modular system:

A. Association

A generic relationship where one class “uses” or “knows about” another.

  • Example: A Teacher teaches a Student.

B. Aggregation (Weak Has-A)

A “part-of” relationship where the “part” can exist independently of the “whole”.

  • Example: A Library has Books. If the library closes, the books still exist.
  • UML: Empty diamond arrow.

C. Composition (Strong Has-A)

A “part-of” relationship where the “part” cannot exist without the “whole”.

  • Example: A House has Rooms. If the house is destroyed, the rooms are too.
  • UML: Filled diamond arrow.

D. Generalization (Is-A)

Inheritance between a superclass and a subclass.

  • Example: A Dog is an Animal.
  • UML: Hollow triangle arrow.

UML Class Diagram Example (PlantUML)

@startuml
class Car {
  - String model
  - Engine engine
  + void start()
}

class Engine {
  - int horsepower
  + void ignite()
}

class Wheel {
  - double pressure
}

Car *-- Engine : composition
Car o-- Wheel : aggregation
@enduml

2. Dynamic Modeling: Sequence Diagrams

While class diagrams show static structure, Sequence Diagrams show how objects interact over time to perform a specific task (usually a use case).

Key Elements:

  • Lifelines: Vertical dashed lines representing an object’s existence over time.
  • Messages: Horizontal arrows showing data/calls between objects.
  • Activation bars: Thin rectangles on lifelines showing when an object is “active” or processing.

Sequence Diagram Example

@startuml
actor User
participant "LoginUI" as UI
participant "AuthService" as Auth
database "UserDB" as DB

User -> UI: Enter Credentials
UI -> Auth: login(username, pass)
Auth -> DB: fetchUser(username)
DB --> Auth: userObject
Auth -> Auth: validatePassword()
Auth --> UI: success
UI --> User: Show Dashboard
@enduml

3. Multiplicity in Design

Multiplicity defines how many instances of one class can be associated with one instance of another.

  • 1: Exactly one.
  • 0..1: Zero or one.
  • * or 0..*: Many.
  • 1..*: One or many.

4. OOD Best Practices

  1. Favor Composition over Inheritance: This promotes flexibility by allowing behavior to be changed at runtime.
  2. Program to an Interface, not an Implementation: Decouple your code from specific classes to make it easier to swap components.
  3. Keep Classes Small: A class should have one reason to change (Single Responsibility Principle).

5. Code Example: Translating UML to Java

Here is how a composition relationship looks in code:

// Engine class
class Engine {
    private int horsepower;
    public Engine(int hp) { this.horsepower = hp; }
    public void start() { System.out.println("Vroom!"); }
}

// Car class demonstrating Composition
public class Car {
    private final Engine engine; // Final implies the car 'owns' the engine's lifecycle

    public Car() {
        this.engine = new Engine(200); // Created when Car is created
    }

    public void drive() {
        engine.start();
        System.out.println("Car is moving");
    }
}

Key Takeaways

  • Class Diagrams represent the architectural blueprint.
  • Sequence Diagrams represent the execution flow.
  • A good design is decoupled, cohesive, and extensible.