Extract Subclass · Los Techies

发布时间:2025-08-19 02:27

20 August, 2009. It was a Thursday.

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

</div> </div>

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

</div> </div>

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

网址:Extract Subclass · Los Techies https://mxgxt.com/news/view/1693086

相关内容

Remove Middle Man · Los Techies
Replace conditional with Polymorphism · Los Techies
“詹姆斯”通常指的是美国职业篮球运动员勒布朗·詹姆斯(LeBron James),他目前效力于美国职业篮球联赛(NBA)洛杉矶湖人队(Los Angeles Lakers)。
袁高峰副教授
洛杉矶好莱坞明星豪宅发现之旅一日游(英文团)
JSON Overview
纪梵希明星柔雾粉底液
旧金山南湾 洛斯阿图斯Los Altos 大占地海景豪华双层别墅 4卧6浴 豪宅区 获奖园景 收藏级酒窖 便利地理位置
大牌明星不断光顾,这里竟是马德里一年中“最热闹”的地方?!
娱乐圈出轨事件不断,“戴绿帽”用西班牙语怎么说?

随便看看