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:
- Top: Class Name.
- Middle: Attributes (Fields).
- 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
Teacherteaches aStudent.
B. Aggregation (Weak Has-A)
A “part-of” relationship where the “part” can exist independently of the “whole”.
- Example: A
LibraryhasBooks. 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
HousehasRooms. 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
Dogis anAnimal. - 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.*or0..*: Many.1..*: One or many.
4. OOD Best Practices
- Favor Composition over Inheritance: This promotes flexibility by allowing behavior to be changed at runtime.
- Program to an Interface, not an Implementation: Decouple your code from specific classes to make it easier to swap components.
- 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.