再包装-味6可変データ
9408 ワード
に入る
このヒントは、インフラストラクチャの白起善の「改造する」を学んで整理したものです.
におい可変データ
Mutable Data
Mutable Data
変数の分割
Split Variable
ループ
各
サンプルコード
public class Order {
public double discount(double inputValue, int quantity) {
if (inputValue > 50) inputValue = inputValue - 2;
if (quantity > 100) inputValue = inputValue - 1;
return inputValue;
}
}
におい
パラメータinputValueの値の使用
解決する
変数を分けましょう.
改造後
public class Order {
public double discount(double inputValue, int quantity) {
double result = inputValue;
if (inputValue > 50) result -= 2;
if (quantity > 100) result -= 1;
return result;
}
}
説明:
変数としてinputValueを使用します.
クエリー関数と変更関数の分離
Separate Query from Modifier
移動障害物も便利です.
値を返す関数には、サイドシフト係数がない必要があります.
キャッシュが重要なオブジェクト状態の変化ではないと仮定します.したがって、キャッシュデータを
サンプルコード
public class Billing {
private Customer customer;
private EmailGateway emailGateway;
public Billing(Customer customer, EmailGateway emailGateway) {
this.customer = customer;
this.emailGateway = emailGateway;
}
public double getTotalOutstandingAndSendBill() {
double result = customer.getInvoices().stream()
.map(Invoice::getAmount)
.reduce((double) 0, Double::sum);
sendBill();
return result;
}
private void sendBill() {
emailGateway.send(formatBill(customer));
}
private String formatBill(Customer customer) {
return "sending bill for " + customer.getName();
}
}
におい
getTotalOutstandingAndSendBill関数を呼び出すとsendBill()不要な関数が呼び出されます.
解決する
クエリー関数と名廉関数を分けて!
改造後
public class Billing {
private Customer customer;
private EmailGateway emailGateway;
public Billing(Customer customer, EmailGateway emailGateway) {
this.customer = customer;
this.emailGateway = emailGateway;
}
public double getTotalOutstanding() {
return customer.getInvoices().stream()
.map(Invoice::getAmount)
.reduce((double) 0, Double::sum);
}
public void sendBill() {
emailGateway.send(formatBill(customer));
}
private String formatBill(Customer customer) {
return "sending bill for " + customer.getName();
}
}
説明:
getTotalOutState()関数はクエリーにのみ使用され、senBill()関数はコマンドとクエリーを個別に呼び出すことで分離します.
セットの削除
Remove Setting Method
•テナントの提供は、このフィールドが変更される可能性があることを意味します.
•オブジェクトの作成時に最初に設定した値を変更する必要がない場合は、その値を設定できるジェネレータを作成し、変更の可能性を排除するためにセッションを削除する必要があります.
サンプルコード
public class Person {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
におい
メンバーを識別するフィールドidは変更できませんが、setterを使用します.
解決する
setterメソッドを除去します.
派生変数をクエリー関数に置換
Replace Derived Variable with Query
public class Person {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Replace Derived Variable with Query
サンプルコード
public class ProductionPlan {
private double production;
private List<Double> adjustments = new ArrayList<>();
public void applyAdjustment(double adjustment) {
this.adjustments.add(adjustment);
this.production += adjustment;
}
public double getProduction() {
return this.production;
}
}
におい
applyadjustment()関数はパラメータの値をlistに挿入し、profusionフィールドに合計を保存します.これはlistで合計が分かる不要な変数です.
解決する
本番フィールドを削除するとlistに格納されている合計が返されます.
改造後
public class ProductionPlan {
private List<Double> adjustments = new ArrayList<>();
public void applyAdjustment(double adjustment) {
this.adjustments.add(adjustment);
}
public double getProduction() {
return adjustments.stream().mapToDouble(Double::valueOf).sum();
}
}
説明:
不要な生産フィールドを削除した後、getProductionで要約を計算します.
複数の関数を変換関数に結合
Combine Functions into Transform
参照を値に置換
-Change Reference to Value
サンプルコード
private TelephoneNumber officeTelephoneNumber;
public String getOfficeAreaCode() {
return this.officeTelephoneNumber.getAreaCode();
}
public void setOfficeAreaCode(String areaCode) {
this.officeTelephoneNumber.setAreaCode(areaCode);
}
public String getOfficeNumber() {
return this.officeTelephoneNumber.getNumber();
}
public void setOfficeNumber(String number) {
this.officeTelephoneNumber.setNumber(number);
}
におい
TelphoneNumberオブジェクトを参照オブジェクトではなく値オブジェクトとして使用する場合の設定
解決する
変更せずにsetterで新しい値オブジェクトを作成します.
改造後
Person
public class Person {
private TelephoneNumber officeTelephoneNumber;
public String getOfficeAreaCode() {
return this.officeTelephoneNumber.getAreaCode();
}
public String getOfficeNumber() {
return this.officeTelephoneNumber.getNumber();
}
public void setOfficeAreaCode(String areaCode) {
this.officeTelephoneNumber = new TelephoneNumber(areaCode, this.getOfficeNumber());
}
public void setOfficeNumber(String number) {
this.officeTelephoneNumber = new TelephoneNumber(this.getOfficeAreaCode(), number);
}
}
TelephoneNumber
public class TelephoneNumber {
private final String areaCode;
private final String number;
public TelephoneNumber(String areaCode, String number) {
this.areaCode = areaCode;
this.number = number;
}
public String getAreaCode() {
return areaCode;
}
public String getNumber() {
return number;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TelephoneNumber that = (TelephoneNumber) o;
return Objects.equals(areaCode, that.areaCode) && Objects.equals(number, that.number);
}
@Override
public int hashCode() {
return Objects.hash(areaCode, number);
}
}
値オブジェクトであるためsetterを削除しequals,hashCodeメソッドを実装する.Reference
この問題について(再包装-味6可変データ), 我々は、より多くの情報をここで見つけました https://velog.io/@coconenne/리팩토링-냄새-6.-가변-데이터テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol