Extract Subclass

Refactoring Day 20 : Extract Subclass 
 
Todays refactoring comes from Martin Fowlers catalog of patterns. You can find this refactoring in his catalog here 
This refactoring is useful when you have methods on a base class that are not shared amongst all classes and needs to be pushed down into it’s own class. The example I’m using here is pretty straightforward. We start out with a single class called Registration. This class handles all information related to a student registering for a course. 
   1: public class Registration    2: {    3:     public NonRegistrationAction Action { get; set; }    4:     public decimal RegistrationTotal { get; set; }    5:     public string Notes { get; set; }    6:     public string Description { get; set; }    7:     public DateTime RegistrationDate { get; set; }    8: } 
 
There is something that we’ve realized after working with this class. We are using it in two different contexts. The properties NonRegistrationAction and Notes are only ever used when dealing with a NonRegistration which is used to track a portion of the system that is slightly different than a normal registration. Noticing this, we can extract a subclass and move those properties down into the NonRegistration class where they more appropriately fit. 
   1: public class Registration    2: {    3:     public decimal RegistrationTotal { get; set; }    4:     public string Description { get; set; }    5:     public DateTime RegistrationDate { get; set; }    6: }    7:      8: public class NonRegistration : Registration    9: {   10:     public NonRegistrationAction Action { get; set; }   11:     public string Notes { get; set; }   12: }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值