Chapter 3: Manging Dependencies Flashcards

1
Q

When does an object depend on another object?

A

When one object changes, the other might be forced to change in turn.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When does an object have a dependency?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we solve the “explicit reference to another class” problem?

A

Instead of hard-code a reference to a class we can inject that reference as a dependency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What technique do we use to solve the “explicit reference to another class” problem?

A

Dependency Injection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the consequences to use an “explicit reference to another class”?

A
  1. If the name of the referenced class change we need to change any code where were we are referencing that class
  2. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What can we do when we can’t remove a dependency from our code?

A

We can isolate it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the techniques to isolate a dependency?

A
  • Isolate Instance Creation

- Isolate Vulnerable External Messages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does the “Isolate Instance Creation” work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the “Isolate Vulnerable External Messages” work?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are positional arguments?

A

They are arguments with an specific, fixed position in the definition of a function/method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why do positional arguments are bad?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we solve the positional arguments dependency?

A

If we have control of the definition of the method, we can use keyword arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do we deal with positional arguments in an external interface?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a factory?

A

An object whose purpose is to create other objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the dependency direction?

A

Dependencies always have a direction. The direction means who depends on who.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the three truths about code related to changes?

A
  • Some classes are more likely than others to have changes in requirements
  • Concrete classes are more likely to change than abstract classes
  • Changing a class that has many dependents will result in widespread consequences.
17
Q

How the likelihood of change affect the direction of the dependency?

A

Every class in an application has a rank of how likely it is to undergo a change compared with others, this can be a class written by you, a base class of the language, or a class in a framework. This rank is a key point to consider when choosing the direction of dependency.

18
Q

Why does an abstraction is more reliable than a concrete class?

A

Abstraction is always safer than depending on a concretion because by its very nature, the abstraction is more stable.

19
Q

What are the zones derived from the intersection of Dependents and Likelihood of change thrusts?

A
  • Zone A (Abstract zone): classes that have little likelihood of change but contain many dependents. Abstract classes ought to be here.
  • Zone B (Neutral zone): classes rarely change and have few dependents.
  • Zone C (Neutral zone): opposite to zone A, these classes tend to be more concrete, which make them more likely to change but few classes depend on them.
  • Zone D (Denger zone): it is the danger zone because changes are costly. They have many dependents and more likelihood of change.