Refactoring Day 19 : Extract Factory Class

Refactoring Day 19 : Extract Factory Class 
 
Todays refactoring was first coined by the GangOfFour and has many resources on the web that have different usages of this pattern. Two different aims of the factory pattern can be found on the GoF website here and here. 
Often in code some involved setup of objects is required in order to get them into a state where we can begin working with them. Uusally this setup is nothing more than creating a new instance of the object and working with it in whatever manner we need. Sometimes however the creation requirements of this object may grow and clouds the original code that was used to create the object. This is where a Factory class comes into play. For a full description of the factory pattern you can read more here. On the complex end of the factory pattern is for creating families of objects using Abstract Factory. Our usage is on the basic end where we have one factory class creating one specific instance for us. Take a look at the code before: 
   1: public class PoliceCarController    2: {    3:     public PoliceCar New(int mileage, bool serviceRequired)    4:     {    5:         PoliceCar policeCar = new PoliceCar();    6:         policeCar.ServiceRequired = serviceRequired;    7:         policeCar.Mileage = mileage;    8:      9:         return policeCar;   10:     }   11: } 
  
As we can see, the new action is responsible for creating a PoliceCar and then setting some initial properties on the police car depending on some external input. This works fine for simple setup, but over time this can grow and the burden of creating the police car is put on the controller which isn’t really something that the controller should be tasked with. In this instance we can extract our creation code and place in a Factory class that has the distinct responsibility of create instances of PoliceCar’s 
   1: public interface IPoliceCarFactory    2: {    3:     PoliceCar Create(int mileage, bool serviceRequired);    4: }    5:      6: public class PoliceCarFactory : IPoliceCarFactory    7: {    8:     public PoliceCar Create(int mileage, bool serviceRequired)    9:     {   10:         PoliceCar policeCar = new PoliceCar();   11:         policeCar.ReadForService = serviceRequired;   12:         policeCar.Mileage = mileage;   13:         return policeCar;   14:     }   15: }   16:     17: public class PoliceCarController   18: {   19:     public IPoliceCarFactory PoliceCarFactory { get; set; }   20:     21:     public PoliceCarController(IPoliceCarFactory policeCarFactory)   22:     {   23:         PoliceCarFactory = policeCarFactory; 
31 Days of Refactoring  Sean Chambers  32 
  24:     }   25:     26:     public PoliceCar New(int mileage, bool serviceRequired)   27:     {   28:         return PoliceCarFactory.Create(mileage, serviceRequired);   29:     }   30: } 
  
Now that we have the creation logic put off to a factory, we can add to that one class that is tasked with creating instances for us without the worry of missing something during setup or duplicating code. 
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值