Object-Oriented Design for a Library App
Model books, borrowers, and loans using classes, responsibilities, and simple object collaboration.
Explanation
Object-oriented design represents a system through objects that combine data and behavior. Good class design begins with responsibilities rather than creating a class for every noun.
Students model a small library application, identify useful relationships, and implement borrowing rules. They then discuss encapsulation and how clear interfaces reduce accidental misuse.
Learning activities
- Identify candidate objects and their responsibilities.
- Draw a simple class relationship diagram.
- Implement and test one borrowing rule.
Code example
python
class Book:
def __init__(self, title):
self.title = title
self.is_available = True
def borrow(self):
if not self.is_available:
return False
self.is_available = False
return TrueKnowledge check
1. What should guide the creation of a class?
- Its responsibility
- The length of its name
- The screen color
Show answer
Its responsibility