Commit 8988f895 authored by Robert Chatley's avatar Robert Chatley
Browse files

remove duplication using Strategy Pattern

parent 8f39871a
class ReverseEncoder
class LineEncoder
#constructor
def initialize(encrypt_func)
@encrypt_func = encrypt_func
end
def encode(str)
words = str.split(" ")
result = []
for word in words
result << encode_word(word)
result << @encrypt_func.encode_word(word)
end
result.join(" ")
end
......@@ -15,18 +20,14 @@ class ReverseEncoder
end
class DoublingEncoder
def encode(str)
words = str.split(" ")
result = []
for word in words
result << encode_word(word)
end
result.join(" ")
class Doubler
def encode_word(word)
word * 2
end
end
class Reverser
def encode_word(word)
word * 2
word.reverse
end
end
\ No newline at end of file
require './encoder'
describe ReverseEncoder do
describe Reverser do
encoder = ReverseEncoder.new
encoder = LineEncoder.new(Reverser.new)
it "can reverse a single word" do
expect(encoder.encode("abcd")).to eq("dcba")
......@@ -14,9 +14,9 @@ describe ReverseEncoder do
end
describe DoublingEncoder do
describe Doubler do
encoder = DoublingEncoder.new
encoder = LineEncoder.new(Doubler.new)
it "can double a single word" do
expect(encoder.encode("abcd")).to eq("abcdabcd")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment