Overview
This lecture introduces UML class diagrams, covering their basic structure, types of relationships, and real-world examples to illustrate their use in system modeling.
Basic Structure of UML Class Diagrams
- A class represents an entity in the system, depicted as a rectangle divided into three sections.
- The top section shows the class name; an instance is a specific example of that class.
- The middle section lists attributes (fields/properties) that describe each instance, formatted as visibility name: data type.
- Example attributes for an Animal class: name: String, id: int, age: int.
- The bottom section contains methods (operations/functions) showing possible behaviors of the class, also with visibility and parentheses.
- Methods allow modification or actions, e.g., setName or eat() in the Animal class.
Visibility in UML
- Visibility symbols specify attribute/method accessibility: minus (-) for private, plus (+) for public, hash (#) for protected, tilde (~) for package/default.
- Private members are accessible only within the class, public by all, protected by the class and its subclasses, and package by classes in the same package.
- Typically, attributes are private/protected; methods are public.
Class Diagram Example: Employee
- Employee class attributes: name, employee ID, phone number, department (all private).
- Method example: updatePhoneNumber() set to public.
Relationships in UML Class Diagrams
- Inheritance: Child classes inherit attributes and methods from a parent class; depicted by an open arrow.
- Example: Tortoise, Otter, Slow Loris classes inherit from the abstract Animal class (shown in italics).
- Abstraction: Abstract classes are not instantiated directly but serve as templates for subclasses.
- Association: Simple relationship between classes, represented by a line (e.g., Otter eats Sea Urchin).
- Aggregation: "Whole-part" relationship where parts can exist independently; shown with an open diamond.
- Composition: "Whole-part" relationship where parts cannot exist without the whole; shown with a closed diamond.
Multiplicity
- Multiplicity defines numerical constraints on relationships, such as 1, 0..1, 1..*, or a specific range.
- Example: One lobby per visitor center (1), one or more bathrooms per visitor center (1..*).
Real-World Example: Online Shopping Cart
- User class: attributes like userID, password, loginStatus (private); method verifyLogin() (public).
- Customer and Administrator inherit from User, with their own specific attributes/methods.
- Composition relationships: A Customer's cart, orders, shipping info, and order details cannot exist without the Customer.
- Multiplicity: A customer can have zero or many orders; each order belongs to only one customer; one-to-one relationship between order and order details.
Key Terms & Definitions
- Class β Blueprint for objects; represents entities in the system.
- Attribute β Property or field describing an instance of a class.
- Method β Behavior or function that a class can perform.
- Visibility β Access level for attributes/methods: private, public, protected, package.
- Inheritance β Relationship where child classes inherit from a parent class.
- Abstraction β Modeling concept by abstract classes not instantiated directly.
- Association β Basic relationship between two classes.
- Aggregation β Whole-part relationship; parts can exist independently.
- Composition β Whole-part relationship; parts cannot exist without the whole.
- Multiplicity β Specifies how many objects participate in a relationship.
Action Items / Next Steps
- Practice drawing basic UML class diagrams, including attributes, methods, and visibility.
- Create diagrams illustrating inheritance, association, aggregation, and composition.
- Review an online shopping cart UML diagram for further understanding.