CSCI 370 - Lecture 7: UML Class Diagrams and SOLID Principles


UML Class Diagrams

What is a UML Class Diagram?

A UML (Unified Modeling Language) class diagram provides a visual representation of the structure of a software system by showing its classes, attributes, methods, and relationships among objects. It is a low-level diagram compared to higher-level diagrams like context diagrams.

Key Components of Class Diagrams

A UML Class Diagram typically shows five main types of information:

  1. Classes (Objects): The diagram identifies the different object types or classes (e.g., Animal, Duck, Fish, Zebra).

  2. Attributes and Methods:

    • Attributes (e.g., age) represent the properties of a class.
    • Methods (e.g., quack()) represent the behaviors.
    • Visibility Modifiers: ‘+’ denotes public, ‘-‘ denotes private.
  3. Relationships Between Classes:

    • Inheritance: Represented with a triangle; shows one class inheriting from another.
    • Aggregation: Represented with a hollow diamond; shows whole-part relationships where parts can exist independently.
    • Composition: Represented with a filled diamond; shows whole-part relationships where parts cannot exist independently.
    • Dependency: Dashed arrow line; shows one class uses another temporarily.
    • Association: Simple lines; shows a general relationship without strong ownership.
  4. Multiplicity:

    • Describes the number of instances of one class related to another.
    • Examples:
      • 0..* means zero or many.
      • 1 means exactly one.
      • 1..* means one or more.
  5. Generalization:

    • Another term for inheritance, indicating that one class (child) generalizes or extends another (parent).

Class Diagram Example: Online Shopping System


Object-Oriented Principles

Review of Core OO Principles

  1. Encapsulation: Hides internal state and only exposes necessary components.
  2. Abstraction: Hides complexity and shows only essential features.
  3. Inheritance: One class inherits properties and methods from another.
  4. Polymorphism: A single function or method can work in different ways depending on input (e.g., println() in Java).

Additional OO Principles


SOLID Principles

S - Single Responsibility Principle (SRP)

Each class should have only one responsibility or reason to change. Benefits:

O - Open-Closed Principle

A class should be open for extension but closed for modification. This avoids breaking existing code and promotes extensibility.

L - Liskov Substitution Principle

Subtypes must be substitutable for their base types without altering the correctness of the program.

Example:

I - Interface Segregation Principle

(Not covered in this lecture.)

D - Dependency Inversion Principle

High-level modules should not depend on low-level modules. Instead, both should depend on abstractions (e.g., interfaces). Also attributed to Robert C. Martin (Uncle Bob).


Final Notes


Tip: Try reviewing different UML diagrams online and practice identifying the components discussed. Also, test your understanding by applying SOLID principles in your own code.