Los Techies : Blogs about software and anything tech!

Refactoring Day 21 : Collapse Hierarchy


Todays refactoring comes from Martin Fowlers catalog of patterns. You can find this refactoring in his catalog here

Yesterday we looked at extracting a subclass for moving responsibilities down if they are not needed across the board. A Collapse Hierarchy refactoring would be applied when you realize you no longer need a subclass. When this happens it doesn’t really make sense to keep your subclass around if it’s properties can be merged into the base class and used strictly from there.

   1: public class Website
   2: {
   3:     public string Title { get; set; }
   4:     public string Description { get; set; }
   5:     public IEnumerable<Webpage> Pages { get; set; }
   6: }
   7:  
   8: public class StudentWebsite : Website
   9: {
  10:     public bool IsActive { get; set; }
  11: }

Here we have a subclass that isn’t doing too much. It just has one property to denote if the site is active or not. At this point maybe we realize that determing if a site is active is something we can use across the board so we can collapse the hierarchy back into only a Website and eliminate the StudentWebsite type.

   1: public class Website
   2: {
   3:     public string Title { get; set; }
   4:     public string Description { get; set; }
   5:     public IEnumerable<Webpage> Pages { get; set; }
   6:     public bool IsActive { get; set; }
   7: }

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

Kick It on DotNetKicks.com
Posted Aug 21 2009, 08:23 AM by schambers

Comments

on 08-25-2009 5:54 PM

Agile/ALT.NET James Shore provides a Introduction to Agile for QA People This is late but Davy Brion has started a series on Build Your Own Data Access Layer Series . Some posts include Out of the Box CRUD Functionality , Mapping Classes to Tables , and

张荣华 wrote 31天重构指南之二十一:合并继承
on 10-15-2009 2:45 AM

今天要说的重构来自于Martin Fowlers的模式目录,你可以在这里找到它。 昨天我们说的“提取子类”重构是指当基类中的一个责任不被所有的子类所需要时,将这些责任提取到合适的子类中。而我们今天所说...

31 Days of Refactoring « Vincent Leung's .NET Tech Clips wrote 31 Days of Refactoring &laquo; Vincent Leung&#039;s .NET Tech Clips
on 10-28-2009 9:28 AM

Pingback from  31 Days of Refactoring « Vincent Leung's .NET Tech Clips

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:02 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:03 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems