“引入参数对象”重构来自于Martin Folwers的重构目录,你可以在这里找到原始的描述。

有时当一个方法所需要的参数大于5个后,读懂该方法的签名就会变得比较困难,就像下面的代码:

   1: public class Registration
   2: {
   3:     public void Create(decimal amount, Student student, IEnumerable<Course> courses,
   4:         decimal credits)
   5:     {
   6:         // do work
   7:     }
   8: }
 
在这种情形下创建一个用户传递参数的类是很有帮助的,这会使得代码更容易明白也更灵活,因为当你需要增加参数时,只需要给参数类添加一个属性即可。
请注意只有当你发现方法的参数比较多时才应该应用该重构,如果方法的参数比较少,就没有必要应用此重构,因为该重构会增加系统中类的数量。
 
   1: public class RegistrationContext
   2: {
   3:     public decimal Amount { get; set; }
   4:     public Student Student { get; set; }
   5:     public IEnumerable<Course> Courses { get; set; }
   6:     public decimal Credits { get; set; }
   7: }
   8:  
   9: public class Registration
  10: {
  11:     public void Create(RegistrationContext registrationContext)
  12:     {
  13:         // do work
  14:     }
  15: }
 
原文链接:http://www.lostechies.com/blogs/sean_chambers/archive/2009/08/23/refactoring-day-23-introduce-parameter-object.aspx

相关文章:

  • 2021-11-04
  • 2021-07-28
  • 2022-01-29
  • 2022-01-04
  • 2021-07-26
  • 2021-05-24
猜你喜欢
  • 2021-10-18
  • 2021-06-23
  • 2021-08-17
  • 2021-11-17
  • 2021-08-28
  • 2021-07-13
  • 2021-06-02
相关资源
相似解决方案