Object Oriented Design done right.
Ruby and Javascript instructor at Tuts+ for 1.5 years
Code Retreats em Portugal
class User
def buy(product)
# code that makes the user buy a product
end
end
class User
def buy(product)
# code that makes the user buy a product
end
end
Create a new class with the specific
responsibility of handling a purchase.
class User
def buy(product)
Purchase.new(user, product).execute
end
end
class Purchase
def initialize user, product
@user = user
@product = product
end
def execute
update_stock
charge_user
deliver_receipt
engage_shipping
end
end
It breaks the code.
Create a class that handles multiple products.
class PurchaseMany
def initialize user, products
@user = user
@product = products
end
def execute
@products.each do |product|
Purchase.new(user, product).execute
end
endend