
# Adaptee
class StringList
def initialize
@rows = []
end
# SpecificRequest()
def get_string
return @rows.join("\n")
end
def add(value)
@rows << value
end
end
# Adapter
class TextAdapter < StringList
# Request()
def get_text
return get_string()
end
end
# Client
adapter = TextAdapter.new
adapter.add("line 1")
adapter.add("line 2")
text = adapter.get_text()
# text: line 1
# line 2
print(text)
| Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. |