Chapter 3: Manging Dependencies Flashcards
When does an object depend on another object?
When one object changes, the other might be forced to change in turn.
When does an object have a dependency?
Following this code: https://github.com/efrapp/design-principles/blob/ch-3/gear.rb
An object has a dependency when it knows:
- The name of another class. Gear expects a class named Wheel to exist.
- The name of a message that it intends to send to someone other than self. Gear expects a Wheel instance to respond to diameter.
- The arguments that a message requires. Gear knows that Wheel.new requires a rim and a tire.
- The order of those arguments. Gear knows that Wheel takes positional arguments and the first should be rim, the second, tire.
How do we solve the “explicit reference to another class” problem?
Instead of hard-code a reference to a class we can inject that reference as a dependency.
What technique do we use to solve the “explicit reference to another class” problem?
Dependency Injection.
What are the consequences to use an “explicit reference to another class”?
- If the name of the referenced class change we need to change any code where were we are referencing that class
- We are declaring that we can collaborate ONLY with the class we are explicitly referring to. If in the future we need to collaborate with another kind of object, we can’t, even if that object has the qualities needed to do it.
What can we do when we can’t remove a dependency from our code?
We can isolate it.
What are the techniques to isolate a dependency?
- Isolate Instance Creation
- Isolate Vulnerable External Messages
How does the “Isolate Instance Creation” work?
When we can’t inject a dependency due to constrains in the app we can explicitly expose the dependency. For this we can use:
- move the dependency to the initialization method in the class we’re referecing it.
- move the dependency in its own explicitly defined method. This new method lazily creates a new instance of the dependency using the Ruby’s ||= operator.
How does the “Isolate Vulnerable External Messages” work?
This technique deals with the "messages that are sent to someone other than self" problem. For this, we can remove the external dependency and encapsulate it in a method of its own. This technique becomes necessary when a class contains embedded references to a message that is likely to change.
What are positional arguments?
They are arguments with an specific, fixed position in the definition of a function/method.
Why do positional arguments are bad?
Because, if for some reason, the order of them changes in the definition of the method, all the senders of it will be forced to change in turn.
How do we solve the positional arguments dependency?
If we have control of the definition of the method, we can use keyword arguments.
How do we deal with positional arguments in an external interface?
We don’t always have the opportunity to control the signature of the method, like external interfaces. For this, we can wrap the external method with one controlled by us and supply keyword arguments.
What is a factory?
An object whose purpose is to create other objects.
What is the dependency direction?
Dependencies always have a direction. The direction means who depends on who.