class Dependency
def initialize(foo, bar, baz)
@foo = foo
@bar = bar
@baz = baz
end
def data
{ :foo => @foo, :bar => @bar, :baz => @baz }
end
end
class Foo
def initialize(Dep)
@data = Dep.new(:a, :b, :c).data
# Foo knows too much about its dependency
# Foo not only relies on the public interface,
# but Foo also knows how to construct a new instance of the dependency;
# that's too much knowledge (context)
# So even though we're injecting our dependency,
# we're now tightly coupling Foo to the dependency
end
end
foo = Foo.new(Dependency)
2. less context.rb
class Dependency
def initialize(foo, bar, baz)
@foo = foo
@bar = bar
@baz = baz
end
def data
{ :foo => @foo, :bar => @bar, :baz => @baz }
end
end
class Foo
def initialize(Dep)
@data = Dep.data # Foo now only relies on an interface
# e.g. relies on being able to safely send a `data` message to the dependency
end
end
foo = Foo.new(Dependency.new(:a, :b, :c))