review confident ruby
再读confident ruby(performing work部分)
再读confident ruby,记下以下要点。
每个函数应包含4部分
- collecting input
- performing work
- delivering result
- handling failures
performing work的模式
这个部分只有一个pattern
sending a strong message
要发消息给trused object,需要3个条件
- 识别要完成工作所需要发送的消息
- 识别这些消息对应的roles
- 确保method能担任这些roles
一个从文件中读取用户信息,并发邮件的例子
需要做以下步骤
- Parse the purchase records from the CSV contained in a provided IO object.
- For each purchase record, use the record’s email address to get the associated customer record, or, if the email hasn’t been seen before, create a new customer record in our system.
- Use the legacy record’s product ID to find or create a product record in our system.
- Add the product to the customer record’s list of purchases.
- Notify the customer of the new location where they can download their files and update their account info.
- Log the successful import of the purchase record.
识别message
- #parse_legacy_purchase_records.
- For #each purchase record, use the record’s #email_address to #get_customer.
- Use the record’s #product_id to #get_product.
- #add_purchased_product to the customer record.
- #notify_of_files_available for the purchased product.
- #log_successful_import of the product record.
识别roles
Message | Receiver Role |
---|---|
#parse_legacy_purchase_records | legacy_data_parser |
#each | purchase_list |
#email_address,#product_id | purchase_record |
#get_customer | customer_list |
#get_product | product_inventory |
#add_purchased_product | customer |
#notify_of_files_available | customer |
#log_successful_import | data_importer |
关于duck type
不要在method中再根据type写分支逻辑,而是首先找出我们需要什么样的message和role,然后确保我们在程序里只允许使用准备好的duck