31日間の再構築ガイドの9:インタフェースの抽出


次に、よく無視される再構築、インタフェースの抽出を見てみましょう.しかし、1つ以上のクラスがクラス内のメソッドのサブセットを使用する場合、それらの依存性を遮断し、消費者(consumers)にインタフェースを使用させるべきであることに気づきました.これは非常に容易ですが、コードの結合性を低下させます.
   1: public class ClassRegistration
   2: {
   3:     public void Create()
   4:     {
   5:         // create registration code
   6:     }
   7:  
   8:     public void Transfer()
   9:     {
  10:         // class transfer code
  11:     }
  12:  
  13:     public decimal Total { get; private set; }
  14: }
  15:  
  16: public class RegistrationProcessor
  17: {
  18:     public decimal ProcessRegistration(ClassRegistration registration)
  19:     {
  20:         registration.Create();
  21:         return registration.Total;
  22:     }
  23: }
              ,   ClassRegistration   Total  、Create        ,  ClassRegistration          ClassRegistration     ,
           。         :
   1: public interface IClassRegistration
   2: {
   3:     void Create();
   4:     decimal Total { get; }
   5: }
   6:  
   7: public class ClassRegistration : IClassRegistration
   8: {
   9:     public void Create()
  10:     {
  11:         // create registration code
  12:     }
  13:  
  14:     public void Transfer()
  15:     {
  16:         // class transfer code
  17:     }
  18:  
  19:     public decimal Total { get; private set; }
  20: }
  21:  
  22: public class RegistrationProcessor
  23: {
  24:     public decimal ProcessRegistration(IClassRegistration registration)
  25:     {
  26:         registration.Create();
  27:         return registration.Total;
  28:     }
  29: }
       Martin Fowler  ,   ここ
http://www.lostechies.com/blogs/sean_chambers/archive/2009/08/07/refactoring-day-9-extract-interface.aspx

This refactoring was first published by Martin Fowler and can be found in his list of refactorings here .