やるしかない | コード00のブログ

やるしかない

7日(土) 会社にてバッチ計測

      パターン指向リファクタリング 100ページ 読破

今後の予定

家に帰って冒険王ビィトを見ながら模様替えする


思ったこと

・職場での協力という概念

  自分の中には競争という概念しかなかったことに気づいた

  変わる必要がある


パターン指向リファクタリングを読んで


・スーパークラスの抽出

 似通った特性を持つ2つ以上のクラスがあれば、共通の特性を持つスーパークラスに移動すればよい


・コンストラクタの連鎖

 複数のコンストラクタに同じコードが重複している

     ↓

 コンストラクタを連鎖させて、コードの重複を減らす


例 

 public class Load{

publc car(float size, string color, double price ){

this.strategy = new Car

this.size = size

this.color = color

this.price = price

}

public car(float size, string color, double price, int type){

this.strategy = new CarType

this.size = size

this.color = color

this.price = price

this.type = type

}

public car(Capitalstrategy strategy, float size, string color, double price, int type, date date){

this.strategy = strategy

this.size = size

this.color = color

this.price = price

this.type = type

this.date = date

}


この場合、1番目と2番目のコンストラクタ内で3番目のコンストラクタを呼び出すように変更することにより重複をなくす


修正後

  public class Load{

publc car(float size, string color, double price ){

//コンストラクタを呼び出す

this(new Car, size, color, price, null, null)

}

public car(float size, string color, double price, int type){

//コンストラクタを呼び出す

this(new CarType, size, color, price, type, null)

}

public car(float size, string color, double price, int type, date date){

this.size = size

this.color = color

this.price = price

this.type = type

this.date = date

}


・インタフェースの統合

  スーパークラスや上位インターフェースに、サブクラスと同じインターフェースを持たせる必要がある

  

  サブクラスのパブリックメソッドの中で、スーパークラスや上位インターフェースに含まれていないものをすべて探す。それらのメソッドをスーパークラスにコピーし、それぞれが空の振る舞いをするように変更する


・パラメータの抽出

  メソッドやコンストラクタで、ローカルでインスタンス化した値をフィールドに代入している

  代入文の右辺をパラメータとして抽出し、クライアントから渡された引数をフィールドに代入する