- study of composition
- a collection of objects
- functions between those objects
- the means to compose functions
- etc
- a object holding a reference to one or more other objects that is collaborates with to fulfill its responsibilities.
- has-a relationship (as opposed to inheritance's is-a)
- composition over inheritance
- f . g = f(g(x))
- Objects should have a defined collection of fields. An object's state should be expressible entirely in terms of those fields.
- Fields should be set at initialization and then frozen. "Update" are performed by creating a new object.
- Objects should respond to call to perform their (primary) responsibility.
our objects:
- have a clearly defined responsibility
- have a predictable interface
- are immutable
- have injectable dependencies
- are easy to configure and re-configure
- are easy to test
- are easy to mock in tests
def call(sms)
compose(
:parse_message,
:build_order,
:validate_order,
:record,
:send_response
).call(sms)
end
def compose(*methods)
->(val) do
result = val
methods.each do |name|
result = method(name).call(result)
end
result
end
end