當函式有不理想的依賴時,適合採取這手法。
「將東西都轉為參數」與「共享大量範圍」是兩極端,我們能藉知識增長而調整出好的平衡。

Replace Query with Parameter
替換查詢式為參數,就是請呼叫方自己提供此值,這容易違反介面應方便被用的原則。
如何在程式安排責任是個關鍵,這不容易也非不可變,這也是我們得熟悉它與替換參數為查詢程式的原因。
# Before
class Customer:
def __init__(self, name, location):
self.name = name
self.location = location
def get_location(self):
return self.location
def calculate_shipping():
customer = Customer("Alice", "Taipei")
if customer.get_location() == "Taipei":
return 10
else:
return 20
shipping_cost = calculate_shipping()
# After
def calculate_shipping(location):
if location == "Taipei":
return 10
else:
return 20
customer = Customer("Alice", "Taipei")
shipping_cost = calculate_shipping(customer.location)