今天的重构我觉得没有太大用处
旧的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class AnimalFeedingService
{
private bool FoodBowlEmpty { get; set; }
public void Feed()
{
if (FoodBowlEmpty)
Feeder.ReplenishFood();
// more code to feed the animal
}
}
public static class Feeder
{
public static void ReplenishFood()
{
// fill up bowl
}
}
重构后的代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class AnimalFeedingService
{
public IFeederService FeederService { get; set; }
public AnimalFeedingService(IFeederService feederService)
{
FeederService = feederService;
}
private bool FoodBowlEmpty { get; set; }
public void Feed()
{
if (FoodBowlEmpty)
FeederService.ReplenishFood();
// more code to feed the animal
}
}
public interface IFeederService
{
void ReplenishFood();
}
public class FeederService : IFeederService
{
public void ReplenishFood()
{
Feeder.ReplenishFood();
}
}
public static class Feeder
{
public static void ReplenishFood()
{
// fill up bowl
}
}
该重构主要用来单元测试时模拟静态类