← 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

ProblemKey ClassesPatterns
Airline ManagementFlight, Seat, Passenger, Booking, PaymentFactory, Singleton
Restaurant SystemMenu, Order, Chef, Waiter, TableCommand, Observer
Car RentalVehicle, Customer, Reservation, InvoiceStrategy (pricing)
Chess GameBoard, Piece (subclasses), Player, MoveStrategy, State
TicketmasterEvent, Venue, Seat, Booking, PaymentFactory, Proxy
LinkedInProfile, Connection, Post, Message, JobObserver, MVC
FacebookUser, Post, Comment, Like, Friend, GroupObserver, Composite
✏️ Exercise: Design an ATM system with cash dispensing, deposit, and balance inquiry. Draw a class diagram showing relationships. Implement withdraw() in Python.