Introduce Parameter Object · Los Techies

发布时间:2025-08-28 15:38

Refactoring Day 23 : Introduce Parameter Object

23 August, 2009. It was a Sunday.

This refactoring comes from Fowler’s refactoring catalog and can be found here

Sometimes when working with a method that needs several parameters it becomes difficult to read the method signature because of five or more parameters being passed to the method like so:

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: }

</div> </div>

In this instances it’s useful to create a class who’s only responsibility is to carry parameters into the method. This helps make the code more flexible because to add more parameters, you need only to add another field to the parameter object. Be careful to only use this refactoring when you find that you have a large number of parameters to pass to the method however as it does add several more classes to your codebase and should be kept to a minimum.

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: }

</div> </div>

This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

网址:Introduce Parameter Object · Los Techies https://m.mxgxt.com/news/view/1724060

相关内容

Rename (method, class, parameter) · Los Techies
Break Dependencies · Los Techies
Remove Middle Man · Los Techies
Replace conditional with Polymorphism · Los Techies
QUnit.config
The parameters dictionary contains a null entry for parameter 'id' of non
The parameters dictionary contains a null entry for parameter 'id' of non
Extract Subclass · Los Techies
Component Object Model (COM) 是什么?
Pull Up Method · Los Techies

随便看看