← Back to Tutorials
12. Object-Oriented Design
OOD Design Template
// Step 1: Identify key entities (nouns)
// Step 2: Define relationships (inheritance, composition)
// Step 3: List behaviors (verbs → methods)
// Step 4: Apply design patterns (Singleton, Factory, Strategy, Observer)
// Step 5: Discuss trade-offs (coupling vs cohesion, extensibility)
ATM Machine
# Key classes
class Card:
def __init__(self, number, pin, account):
self.number = number; self.pin = pin
self.account = account
class Account:
def __init__(self, acct_no, balance=0):
self.acct_no = acct_no; self.balance = balance
def withdraw(self, amount):
if amount > self.balance: raise InsufficientFunds()
self.balance -= amount
return amount
class ATM:
def __init__(self, cash_available):
self.cash_available = cash_available
def authenticate(self, card, pin):
return card.pin == pin
def withdraw(self, card, amount):
if amount > self.cash_available: raise ATMOutOfCash()
cash = card.account.withdraw(amount)
self.cash_available -= amount
return cash
Key OOD Problems
Problem Key Classes Patterns
Airline Management Flight, Seat, Passenger, Booking, Payment Factory, Singleton
Restaurant System Menu, Order, Chef, Waiter, Table Command, Observer
Car Rental Vehicle, Customer, Reservation, Invoice Strategy (pricing)
Chess Game Board, Piece (subclasses), Player, Move Strategy, State
Ticketmaster Event, Venue, Seat, Booking, Payment Factory, Proxy
LinkedIn Profile, Connection, Post, Message, Job Observer, MVC
Facebook User, Post, Comment, Like, Friend, Group Observer, Composite
✏️ Exercise: Design an ATM system with cash dispensing, deposit, and balance inquiry. Draw a class diagram showing relationships. Implement withdraw() in Python.
← Previous Back to Index Next: Behavioral →