Object Orientation Study Notes
Overview
Object-Oriented Analysis and Design (OOAD) is a software development approach that models real-world problems using objects and their interactions. It’s built on four fundamental principles and seven core concepts.
1. Basic Principles of Object Orientation
1.1 Abstraction (抽象)
Definition: Isolates use from implementation - you can use something without knowing how it works internally.
Key Points:
- Separates what an object does from how it does it
- Users interact with objects through interfaces without needing implementation details
- Example: Using a remote control (interface) without understanding TV circuitry (implementation)
1.2 Encapsulation (封装)
Definition: A development technique that creates new data types (classes) by combining information and behaviors while restricting access to implementation details.
Core Features:
- Hides implementation from clients
- Clients depend only on the interface
- Combines data (attributes) and behavior (methods) in a single unit
- Controls access to internal object details
Example: Professor object encapsulates personal data and teaching methods - other objects can only interact through defined operations like setMaxLoad() or takeSabbatical()
1.3 Modularity (模块化)
Definition: Breaks complex systems into manageable, smaller parts.
Benefits:
- Makes large systems easier to understand
- Enables separate development of different modules
- Improves maintainability and reusability
Example: University system broken into modules:
- Billing Subsystem
- Course Catalog Subsystem
- Student Management Subsystem
- Course Registration System
1.4 Hierarchy (层次结构)
Definition: Taxonomic organization where elements at the same level share similar characteristics.
Key Principle: Elements at the same hierarchy level should be at the same level of abstraction
Example: Asset hierarchy:
- Asset (top level)
- Bank Account, Security, Properties (middle level)
- Savings, Fixed Deposit (under Bank Account)
- Stock, Bond (under Security)
- Bank Account, Security, Properties (middle level)
2. Basic Concepts of Object Orientation
2.1 Object (对象)
Definition
Object: “An abstraction of something in a problem domain, reflecting the capabilities of the system to keep information about it, interact with it, or both.” - Coad and Yourdon (1990)
Object Characteristics (Booch 1994)
Objects have three essential properties:
- State (状态): Current condition affecting how it behaves
- Represented by attributes and relationships
- Behavior (行为): What the object can do and how it responds to events
- Represented by operations, methods, and state machines
- Identity (标识): Each object is unique
- Has unique object name with a unique key
UML Object Notation
objectName:className
attributeName=value
Example:
OOAD:Subject
courseID="BACS2053"
courseName="Object Oriented Analysis and Design"
2.2 Class (类)
Definition
Class: A description of a group of objects with common properties (attributes), behavior (operations), and relationships.
Key Relationships:
- An object is an instance of a class
- A class serves as a template for creating objects
- Objects are grouped into classes
UML Class Notation
Classes are represented with 3 compartments:
Class Name
-----------
Attributes
-----------
Operations()
Example:
Professor
-----------
- name
- employeeID : UniqueId
- hireDate
- status
- discipline
- maxLoad
-----------
+ submitFinalGrade()
+ acceptCourseOffering()
+ setMaxLoad()
+ takeSabbatical()
+ teachClass()
Attributes vs Operations
Attributes (属性):
- Data that describes the object
- Format:
attributeName : dataType
Operations (操作):
- Functions the object can perform
- Format:
operationName(parameter_list) : return_type
2.3 Generalization (泛化)
Definition
Generalization: A taxonomic relationship between a more general element and a more specific element.
Key Characteristics:
- More general descriptions are abstracted out from specialized classes
- Every subclass contains at least one unique characteristic
- Creates an “is-a-kind-of” relationship
Example Hierarchy:
Person (most general)
├── Employee
│ ├── Monthly Paid
│ ├── Weekly Paid
│ └── Hourly Paid
│ ├── Driver
│ ├── Cleaner
│ └── Sales Assistant
├── Customer
└── Supplier
2.4 Inheritance (继承)
Definition
Inheritance: A mechanism where more specific classes (subclasses) incorporate structure and behavior from more general classes (superclasses).
Key Features
- Supplements generalization relationships
- Whole description of superclass applies to all subclasses
- Enables code reuse from previous projects
- Creates hierarchy of abstractions
Types of Inheritance
Single Inheritance:
- One class inherits from one other class
- Most common and safer approach
Multiple Inheritance:
- One class inherits from several other classes
- More complex, use with caution
What Gets Inherited?
Subclasses inherit their parent’s:
- Attributes
- Operations
- Relationships
Subclasses can:
- Add additional attributes, operations, relationships
- Redefine inherited operations (use caution!)
Inheritance vs Generalization
- Generalization: Conceptual relationship between classes
- Inheritance: Language-specific mechanism to implement generalization
2.5 Message Passing (消息传递)
Definition
Message Passing: The mechanism by which objects communicate and collaborate to fulfill system functions.
How It Works
Message Components:
- Sender object: Initiates the message
- Receiver object: Receives and processes the message
- Message content:
[receiver, operation, parameters] - Return message:
[sender, return_value(s)]
Encapsulation Connection
- Objects cannot know how other objects operate internally
- Objects only know the interface of other objects
- Supports the “layers of an onion” model:
- Outer layer: Operation signatures (interface)
- Middle layer: Operations that access data
- Inner core: Hidden data (encapsulated)
2.6 Polymorphism (多态性)
Definition
Polymorphism: Allows one message to be sent to objects of different classes, with each object responding appropriately.
Key Features
- Same message name in different classes
- Each class performs/reacts differently
- Sending object doesn’t need to know what type of object will receive the message
- Hides different implementations behind a single interface
Polymorphism Example
Message: calculatePay()
Different Implementations:
- FullTimeEmployee: Fixed monthly amount based on grade
- PartTimeEmployee: Variable amount based on rate and hours worked
- ContractEmployee: Fixed amount based on contract, no pension deductions
Interface (接口)
Interfaces formalize polymorphism and support “plug-and-play” architectures.
UML Interface Representations:
- Canonical (Class/Stereotype):
<<interface>>
Shape
-----------
Draw()
Move()
Scale()
Rotate()
- Elided/Iconic (“Lollipop”):
Shape ○————— Tube
○————— Pyramid
○————— Cube
2.7 Package (包)
Definition
Package: A general-purpose mechanism for organizing elements into groups.
Uses
- Organize models under development
- Unit of configuration management
- Contain other model elements (classes, interfaces, other packages)
Example:
University Artifacts
├── Student Artifacts
│ ├── Student
│ └── Course
├── Professor
├── Schedule
└── CourseOffering
3. Key Relationships and Differences
3.1 Class vs Object
| Class | Object |
|---|---|
| Abstract definition/template | Concrete instance |
| Describes structure & behavior | Has actual state & identity |
Example: Course class | Example: OOAD:Subject object |
3.2 Generalization vs Inheritance
| Generalization | Inheritance |
|---|---|
| Conceptual relationship | Implementation mechanism |
| Design/modeling concept | Programming language feature |
| ”Is-a-kind-of” relationship | Code reuse mechanism |
4. Important Exam Points
4.1 Fill-in-the-Blank Answers
- Modularity: Breaks up something complex into manageable parts
- Encapsulation: Creating new data types by combining information and behaviors, restricting access to implementation details
- Abstraction: Isolates use from implementation
- Hierarchy: Taxonomic organization of elements
4.2 Diagram Recognition
- Class Diagram: Rectangle with 3 compartments (name, attributes, operations)
- Object Diagram: Rectangle with 2 compartments (objectName:className, attribute=value)
- Package Diagram: Folder-like symbol containing other elements
- Interface Diagram: Either
<<interface>>stereotype or “lollipop” notation - Message Passing: Two objects with arrows showing message exchange
4.3 Key Principles to Remember
- Encapsulation = Data + Behavior + Information Hiding
- Inheritance enables code reuse and supports generalization
- Polymorphism = Same interface, different implementations
- Abstraction separates what from how
- Objects have state, behavior, and identity
- Classes are templates; objects are instances