Created
January 25, 2013 15:39
-
-
Save rewinfrey/4635346 to your computer and use it in GitHub Desktop.
Dependency Inversion Violation For Kelly
This file contains 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
class PlayerIO | |
attr_accessor :output, :input | |
def initialize(output, input) | |
@output = output | |
@input = input | |
end | |
def output(args) | |
@output.puts args | |
end | |
def input | |
@input.gets | |
end | |
end | |
# to prevent a dependency inversion violation, keep any hard dependencies at the very top | |
# this means inject dependencies into an object, don't bury them inside the object | |
player_io = PlayerIO.new($stdout, $stdin) | |
player_io = PlayerIO.new(outfile, infile) | |
player_com = PlayerCommunicator.new(:feedback => "some_feedback", | |
:total_turns => 9, | |
:player_io => player_io) | |
class PlayerCommunicator | |
attr_accessor :guess, :feedback, :turn_number, :player_io | |
def initialize(options) | |
@feedback = options.fetch(:feedback) | |
@turn_number = 1 | |
@total_turns = options.fetch(:total_turns) | |
@guess_history = [] | |
@player_io = options.fetch(:player_io) | |
end | |
def output(message) | |
@player_io.output(message) | |
end | |
def input | |
@player_io.input | |
end | |
end | |
# somewhere in my application... | |
player_com.output("Enter move:") | |
next_move = game.next_move |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment