Last active
July 21, 2017 07:33
-
-
Save towry/9671a46012d95fc0311467b73fb1c172 to your computer and use it in GitHub Desktop.
describe the DCI(Data, context, interaction) architecture in Ruby.
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
| require_relative "customer" | |
| require_relative "user" | |
| # context bind role to object. | |
| # by using the extend method in ruby. | |
| # now the data object has some behaviors that get from the role class(Customer). | |
| class Context | |
| def initialize(user, book) | |
| @customer, @book = user, book | |
| @customer.extend(Customer) | |
| end | |
| def call | |
| @customer.buy(@book) | |
| end | |
| end | |
| user = User.new | |
| ctx = Context.new(user, "a book") | |
| ctx.call |
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
| # interaction, the role. | |
| module Customer | |
| def buy(book) | |
| puts @name | |
| puts "bought a book" | |
| # or do other thing about the | |
| end | |
| end |
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
| # The data, doesn't change offen. | |
| class User | |
| def initialize | |
| @name = "towry" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment