Ruby on Rails Wednesday, March 26, 2014

Matthew Riley wrote in post #1141041:
> Hi Rails developers,
>
> I would appreciate some assistance. Please study the following code and
> provide your reasoning for whether you agree or disagree with the
> following
> statements.
>
> I understand the code is not specific to Rails, however I am
> specifically
> interested in Rails developer's perspective. Thanks!
>
> car.rb
>
> class Car
> def initialise(engine)
> @engine = engine
> end
>
> def start
> @engine.start
> end
> end
>
>
> engine.rb
>
> class Engine
>
> def start
>
> puts 'Engine started'
>
> end
>
> end
>
> - Car is not dependent on, and therefore not coupled to Engine
>
> - Car is dependent on an object that responds to "start"
>
> - Car may or may not be initialised with an Engine object
>
> - Car uses constructor-based dependency injection

The example is a little too simple to comment but i can agree with your
statements.

> - Car is dependent on an object that responds to "start".

This i think is the trickiest one. If only the engine class has a method
called 'start' then Car is effectively coupled to Engine. Also if
additional public interface methods get added to Engine, and Car starts
to call those:
engine.gear_up
engine.stop
then Car again becomes more and more coupled to the operations of
Engine.

Also if Car starts to manage other components, and calling their public
interfaces, thus having high fan out, then you also get tight coupling.

So again the point is, this example is still too simple to judge, and
the design pattern that will make the most sense for it is dependent on
how the rest of the application will flow.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/82991442089fd4c91e18f1124a1f940e%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment