انتقل إلى وضع عدم الاتصال باستخدام تطبيق Player FM !
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.
Gearexpects a class namedWheelto exist
- The name of a message that it intends to send to someone other than
self.Gearexpects aWheelinstance to respond todiameter
- The arguments that a message requires.
Gearknows thatWheel.newrequires arimand atire
- The order of those arguments.
Gearknows the first argument toWheel.newshould berimand 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
Wheelclass changes, thegear_inchesmethod must also change - The bigger problem is that
gear_inchesis explicitly saying that it is only willing to calculate gear inches for instances ofWheel Gearwill 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.
Gearneeds 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
Wheelinstance 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.newfromgear_inchesand intoGear'sinitializemethod - The next alternative isolates the creation of a
Wheelinto its ownwheelmethod
Isolate Vulnerable External Messages
gear_inchesdepends onGearresponding towheelandwheelresponding todiameter- by creating a different
diametermethod 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
initializemethod must be passed in the correct order. we can pass an object instead to remove this dependency
Explicitly Define Defaults
- we can use the
fetchmethod to set defaults when using hashes in ourinitializemethod fetchexpects the key you're fetching to be in the hash and supplies several options for handling missing keysfetchwill only set the default if the key is not found in the hash
Managing Dependency Direction
- All examples thus far have shown
Geardepending onWheelordiameter- but the code could have easily been written so thatWheeldepends onGearorratio
Choosing Dependency Direction
Depend on things that change less often
John’s pick - Pick Krisp -https://krisp.ai
78 حلقات
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.
Gearexpects a class namedWheelto exist
- The name of a message that it intends to send to someone other than
self.Gearexpects aWheelinstance to respond todiameter
- The arguments that a message requires.
Gearknows thatWheel.newrequires arimand atire
- The order of those arguments.
Gearknows the first argument toWheel.newshould berimand 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
Wheelclass changes, thegear_inchesmethod must also change - The bigger problem is that
gear_inchesis explicitly saying that it is only willing to calculate gear inches for instances ofWheel Gearwill 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.
Gearneeds 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
Wheelinstance 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.newfromgear_inchesand intoGear'sinitializemethod - The next alternative isolates the creation of a
Wheelinto its ownwheelmethod
Isolate Vulnerable External Messages
gear_inchesdepends onGearresponding towheelandwheelresponding todiameter- by creating a different
diametermethod 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
initializemethod must be passed in the correct order. we can pass an object instead to remove this dependency
Explicitly Define Defaults
- we can use the
fetchmethod to set defaults when using hashes in ourinitializemethod fetchexpects the key you're fetching to be in the hash and supplies several options for handling missing keysfetchwill only set the default if the key is not found in the hash
Managing Dependency Direction
- All examples thus far have shown
Geardepending onWheelordiameter- but the code could have easily been written so thatWheeldepends onGearorratio
Choosing Dependency Direction
Depend on things that change less often
John’s pick - Pick Krisp -https://krisp.ai
78 حلقات
ทุกตอน
×مرحبًا بك في مشغل أف ام!
يقوم برنامج مشغل أف أم بمسح الويب للحصول على بودكاست عالية الجودة لتستمتع بها الآن. إنه أفضل تطبيق بودكاست ويعمل على أجهزة اندرويد والأيفون والويب. قم بالتسجيل لمزامنة الاشتراكات عبر الأجهزة.