Chapter 1: Object Orientation Simplified Notes (by Grok 3)

Fundamental Principles of Object-Oriented (O-O) Design

  • Abstraction (抽象化):
    • Isolates a system’s use from its implementation, allowing users to interact without knowing internal details.
    • Example: A driver uses a car’s interface (pedals, steering) without understanding engine mechanics.
  • Encapsulation (封裝):
    • Combines data (attributes) and behaviors (methods) into a class, hiding implementation details from clients.
    • Clients rely on the class’s interface, enhancing security and maintainability.
  • Modularity (模組化):
    • Breaks complex systems into smaller, manageable modules to simplify understanding and development.
    • Example: A software system splits into user interface, database, and logic modules.
  • Hierarchy (層次結構):
    • Organizes elements taxonomically (分類學) into levels, such as class inheritance or system components.
    • Enables structured relationships and reuse of code.

Key Concepts of Object Orientation

  • Object (物件):
    • Represents an entity in the problem domain with:
      • State: Current condition defined by attributes (e.g., a student’s enrollment status).
      • Behavior: Actions an object can perform, represented by methods (e.g., adding a fitness class).
      • Identity: Unique identifier (e.g., object name with a unique key).
    • Example (Fitness System):
      • Student Object: Attributes (student number, name, address); Methods (add fitness class, update record).
      • Instructor Object: Attributes (name, availability); Methods (teach class, change fee).
      • Registration Record Object: Attributes (student number, class number); Methods (add/drop student, notify instructor).
  • Class (類別):
    • Acts as a template for objects, defining common attributes and operations.
    • Represented in UML with a rectangle containing:
      • Class name.
      • Attributes (structure).
      • Operations (behavior).
    • Example (Course Class):
      • Attributes: Name, location, credit hours, start/end time.
      • Operations: Add/delete student, get roster, check if full.
  • Generalization (概括化):
    • Establishes a taxonomic relationship where a general class (superclass) abstracts common features, and specific classes (subclasses) add unique characteristics.
    • Example: A “Person” class generalizes “Employee,” “Customer,” and “Supplier”; an “Employee” generalizes “Hourly Employee” (e.g., driver, cleaner).
  • Inheritance (繼承):
    • Allows a subclass to inherit attributes, operations, and relationships from a superclass, supporting code reuse.
    • Types:
      • Single Inheritance: One superclass (e.g., “Account” to “Savings Account”).
      • Multiple Inheritance: Multiple superclasses (use cautiously due to complexity).
    • Subclasses can add or redefine inherited features.
    • Example: “Person” (superclass) with “Patient” and “Doctor” (subclasses); “Doctor” inherits “Person” attributes (e.g., name) and adds specific ones (e.g., specialty).
  • Message Passing (訊息傳遞):
    • Enables objects to communicate by sending messages to invoke methods.
    • Example: In a sales system, a “Sales Transaction” object sends a message to a “Customer” object to update details.
  • Interface (Polymorphism, 多型):
    • Allows objects of different classes to respond to the same message in unique ways, hiding implementation details behind a single interface.
    • Supports “plug-and-play” architectures for flexible system design.
    • Example: A “calculatePay” method behaves differently for “Full-Time Employee,” “Part-Time Employee,” and “Contract Employee.”
  • Package (套件):
    • Organizes model elements (e.g., classes) into groups for better management and configuration.
    • Example: “University Artifacts” package contains “Course,” “Schedule,” “Student,” and “Professor” classes.

UML Notation and Diagrams

  • Class Diagram:
    • Represents classes, their attributes, operations, and relationships (e.g., generalization, inheritance).
    • Example (Hotel System for Q1):
      • Superclass: “Person” with attributes (name, ID, contact).
      • Subclasses: “Employee” (adds role, salary) and “Customer” (adds booking history).
      • Generalization: “Employee” and “Customer” inherit from “Person.”
  • Object Diagram:
    • Shows specific instances of classes and their relationships at a moment in time.
    • Example (Employee Object for Q2):
      • Instance: “JohnDoe:Employee” with values (name=“John Doe,” ID=“E123,” role=“Manager”).

Relationships Between Classes and Objects

  • A class defines the abstract structure and behavior for objects.
  • Objects are instances of a class, grouped by shared attributes and operations.
  • Example: “Course” class defines properties (name, location); objects like “BACS20250” (credit=3, semester=2) are instances.

Practical Applications

  • Abstraction: Simplifies interaction by focusing on essential features (e.g., using a fitness app without knowing its code).
  • Encapsulation: Protects data integrity (e.g., restricting direct access to a student’s record).
  • Inheritance: Reduces redundancy (e.g., reusing “Person” attributes in “Employee”).
  • Polymorphism: Enhances flexibility (e.g., a single “draw” method for different shapes like “Cube” or “Tube”).

Questions for Review

  • Q1: Draw a UML class diagram for a hotel system showing generalization/inheritance (e.g., “Person” to “Employee” and “Customer”).
  • Q2: Construct an object diagram for an employee object (e.g., “JohnDoe:Employee” with specific attribute values).