Refactoring Day 5 : Pull Up Field
Today we look at a refactoring that is similar to the Pull Up method. Instead of a method, it is obviously
done with a field instead!
1: public abstract class Account
2: {
3: }
4:
5: public class CheckingAccount : Account
6: {
7: private decimal _minimumCheckingBalance = 5m;
8: }
9:
10: public class SavingsAccount : Account
11: {
12: private decimal _minimumSavingsBalance = 5m;
13: }
In this example, we have a constant value that is duplicated between two derived classes. To promote
reuse we can pull up the field into the base class and rename it for brevity.
1: public abstract class Account
2: {
3: protected decimal _minimumBalance = 5m;
4: }
5:
6: public class CheckingAccount : Account
7: {
8: }
9:
10: public class SavingsAccount : Account
11: {
12: }
done with a field instead!
1: public abstract class Account
2: {
3: }
4:
5: public class CheckingAccount : Account
6: {
7: private decimal _minimumCheckingBalance = 5m;
8: }
9:
10: public class SavingsAccount : Account
11: {
12: private decimal _minimumSavingsBalance = 5m;
13: }
In this example, we have a constant value that is duplicated between two derived classes. To promote
reuse we can pull up the field into the base class and rename it for brevity.
1: public abstract class Account
2: {
3: protected decimal _minimumBalance = 5m;
4: }
5:
6: public class CheckingAccount : Account
7: {
8: }
9:
10: public class SavingsAccount : Account
11: {
12: }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.