Skip to content

Instantly share code, notes, and snippets.

@cbandy
Created May 8, 2012 18:18
Show Gist options
  • Select an option

  • Save cbandy/2638188 to your computer and use it in GitHub Desktop.

Select an option

Save cbandy/2638188 to your computer and use it in GitHub Desktop.
What DCI gets us: from Procedure to Context
# Ruby-esque pseudocode
def transfer_money_between_accounts(acct1, acct2, amount)
puts "Source balance is: #{acct1.balance}"
puts "Destination balance is: #{acct2.balance}"
acct1.balance -= amount
acct2.balance += amount
puts "Source balance is now: #{acct1.balance}"
puts "Destination balance is now: #{acct2.balance}"
end
transfer_money_between_accounts(Account.new(1000), Account.new(0), 245)
# Better signature and variable names
def transfer_money(Account source, Account destination, amount)
puts "Source balance is: #{source.balance}"
puts "Destination balance is: #{destination.balance}"
source.balance -= amount
destination.balance += amount
puts "Source balance is now: #{source.balance}"
puts "Destination balance is now: #{destination.balance}"
end
transfer_money(Account.new(1000), Account.new(0), 245)
# Use domain language do describe the actions
def transfer_money(Account source, Account destination, amount)
puts "Source balance is: #{source.balance}"
puts "Destination balance is: #{destination.balance}"
# Withdraw from source account
source.balance -= amount
# Deposit into destination account
destination.balance += amount
puts "Source balance is now: #{source.balance}"
puts "Destination balance is now: #{destination.balance}"
end
transfer_money(Account.new(1000), Account.new(0), 245)
# Move actions onto respective objects
class TransferMoney
module Source
def withdraw(amount)
self.balance -= amount
end
end
module Destination
def deposit(amount)
self.balance += amount
end
end
def execute(Account source, Account destination, amount)
source.extend Source
destination.extend Destination
puts "Source balance is: #{source.balance}"
puts "Destination balance is: #{destination.balance}"
source.withdraw(amount)
destination.deposit(amount)
puts "Source balance is now: #{source.balance}"
puts "Destination balance is now: #{destination.balance}"
end
end
TransferMoney.execute(Account.new(1000), Account.new(0), 245)
# Prepare for other entry points
class TransferMoney
module Source
def withdraw(amount)
self.balance -= amount
end
end
module Destination
def deposit(amount)
self.balance += amount
end
end
def initialize(Account source, Account destination, amount)
@source = source
@source.extend Source
@destination = destination
@destination.extend Destination
@amount = amount
end
def execute
puts "Source balance is: #{@source.balance}"
puts "Destination balance is: #{@destination.balance}"
@source.withdraw(@amount)
@destination.deposit(@amount)
puts "Source balance is now: #{@source.balance}"
puts "Destination balance is now: #{@destination.balance}"
end
end
TransferMoney.new(Account.new(1000), Account.new(0), 245).execute
# Other entry points
class TransferMoney
module Source
def withdraw(amount)
self.balance -= amount
end
end
module Destination
def deposit(amount)
self.balance += amount
end
end
def initialize(Account source, Account destination, amount)
@source = source
@source.extend Source
@destination = destination
@destination.extend Destination
@amount = amount
end
def transfer
@source.withdraw(@amount)
@destination.deposit(@amount)
end
def transfer_verbosely
puts "Source balance is: #{@source.balance}"
puts "Destination balance is: #{@destination.balance}"
transfer
puts "Source balance is now: #{@source.balance}"
puts "Destination balance is now: #{@destination.balance}"
end
end
TransferMoney.new(Account.new(1000), Account.new(0), 245).transfer_verbosely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment