Created
May 8, 2012 18:18
-
-
Save cbandy/2638188 to your computer and use it in GitHub Desktop.
What DCI gets us: from Procedure to Context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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