Go offline with the Player FM app!
Managing Dependencies
Manage episode 224613088 series 1900125
Chapter 3: Managing Dependencies
To collaborate, an object must know something about others. Knowing creates a dependency. If not managed carefully, these dependencies will strangle your application
Recognizing Dependencies
An object has a dependency when it knows:
- The name of another class.
Gear
expects a class namedWheel
to exist
- The name of a message that it intends to send to someone other than
self
.Gear
expects aWheel
instance to respond todiameter
- The arguments that a message requires.
Gear
knows thatWheel.new
requires arim
and atire
- The order of those arguments.
Gear
knows the first argument toWheel.new
should berim
and the second should betire
Writing Loosely Coupled Code
Inject Dependencies
see 1_inject_dependencies.rb
- Referring to a class by its name inside of another class is bad.
- If the name of
Wheel
class changes, thegear_inches
method must also change - The bigger problem is that
gear_inches
is explicitly saying that it is only willing to calculate gear inches for instances ofWheel
Gear
will only collaborate with any other kind of object even if that object has a diameter and uses gears!
It's is not the class of the object that's important, it's the message you plan to send to it.
Gear
needs access to an object that can respond todiameter
- a duck type- We can use a technique called dependency injection to move the creation of a new
Wheel
instance outside of the class
Isolate Dependencies
see 2_isolate_dependencies.rb
Isolate Instance Creation
- Sometimes you can't break all unnecessary dependencies, but you can isolate them
- The first technique moves
Wheel.new
fromgear_inches
and intoGear
'sinitialize
method - The next alternative isolates the creation of a
Wheel
into its ownwheel
method
Isolate Vulnerable External Messages
gear_inches
depends onGear
responding towheel
andwheel
responding todiameter
- by creating a different
diameter
method to holdwheel.diameter
, we remove the dependency withingear_inches
Remove Argument-Order Dependencies
see 3_remove_arg_orer_dependencies.rb
Use Hashes for Initialization Arguments
- arguments of our
initialize
method must be passed in the correct order. we can pass an object instead to remove this dependency
Explicitly Define Defaults
- we can use the
fetch
method to set defaults when using hashes in ourinitialize
method fetch
expects the key you're fetching to be in the hash and supplies several options for handling missing keysfetch
will only set the default if the key is not found in the hash
Managing Dependency Direction
- All examples thus far have shown
Gear
depending onWheel
ordiameter
- but the code could have easily been written so thatWheel
depends onGear
orratio
Choosing Dependency Direction
Depend on things that change less often
John’s pick - Pick Krisp -https://krisp.ai
78 episodes
Manage episode 224613088 series 1900125
Chapter 3: Managing Dependencies
To collaborate, an object must know something about others. Knowing creates a dependency. If not managed carefully, these dependencies will strangle your application
Recognizing Dependencies
An object has a dependency when it knows:
- The name of another class.
Gear
expects a class namedWheel
to exist
- The name of a message that it intends to send to someone other than
self
.Gear
expects aWheel
instance to respond todiameter
- The arguments that a message requires.
Gear
knows thatWheel.new
requires arim
and atire
- The order of those arguments.
Gear
knows the first argument toWheel.new
should berim
and the second should betire
Writing Loosely Coupled Code
Inject Dependencies
see 1_inject_dependencies.rb
- Referring to a class by its name inside of another class is bad.
- If the name of
Wheel
class changes, thegear_inches
method must also change - The bigger problem is that
gear_inches
is explicitly saying that it is only willing to calculate gear inches for instances ofWheel
Gear
will only collaborate with any other kind of object even if that object has a diameter and uses gears!
It's is not the class of the object that's important, it's the message you plan to send to it.
Gear
needs access to an object that can respond todiameter
- a duck type- We can use a technique called dependency injection to move the creation of a new
Wheel
instance outside of the class
Isolate Dependencies
see 2_isolate_dependencies.rb
Isolate Instance Creation
- Sometimes you can't break all unnecessary dependencies, but you can isolate them
- The first technique moves
Wheel.new
fromgear_inches
and intoGear
'sinitialize
method - The next alternative isolates the creation of a
Wheel
into its ownwheel
method
Isolate Vulnerable External Messages
gear_inches
depends onGear
responding towheel
andwheel
responding todiameter
- by creating a different
diameter
method to holdwheel.diameter
, we remove the dependency withingear_inches
Remove Argument-Order Dependencies
see 3_remove_arg_orer_dependencies.rb
Use Hashes for Initialization Arguments
- arguments of our
initialize
method must be passed in the correct order. we can pass an object instead to remove this dependency
Explicitly Define Defaults
- we can use the
fetch
method to set defaults when using hashes in ourinitialize
method fetch
expects the key you're fetching to be in the hash and supplies several options for handling missing keysfetch
will only set the default if the key is not found in the hash
Managing Dependency Direction
- All examples thus far have shown
Gear
depending onWheel
ordiameter
- but the code could have easily been written so thatWheel
depends onGear
orratio
Choosing Dependency Direction
Depend on things that change less often
John’s pick - Pick Krisp -https://krisp.ai
78 episodes
All episodes
×Welcome to Player FM!
Player FM is scanning the web for high-quality podcasts for you to enjoy right now. It's the best podcast app and works on Android, iPhone, and the web. Signup to sync subscriptions across devices.