我怎样才能减少模型的圈复杂度

How can i reduce cyclomatic complexity from a model

我需要降低这个模型的圈复杂度,因为它有 26 个,这是正常的 class setter 和 getter

public class DetailRecord {

    private int lengthReference1;
    private int lengthReference2;
    private int lengthPayersNit;
    private int lengthTransactionAmount;
    private String recordType;
    private String payersNit;
    private String payersName;
    private String payersAccountBank;
    private String accountNumberToBeDebited;
    private String transactionType;
    private String transactionAmount;
    private String referenceOne;
    private String referenceTwo;
    private String expirationDateOrApplicationDate;
    private String billedPeriods;
    private String cycle;
    private String reserved;
    private String validationNit;
    private String encabezadoTotal;

    public DetailRecord() {
        lengthReference1 = 30;
        lengthReference2 = 30;
        lengthPayersNit = 13;
        lengthTransactionAmount = 17;
        recordType = "6";
        payersName = "                    ";
    }

    public int getLengthReference1() {
        return lengthReference1;
    }

    public int getLengthReference2() {
        return lengthReference2;
    }

    public int getLengthPayersNit() {
        return lengthPayersNit;
    }

    public int getLengthTransactionAmount() {
        return lengthTransactionAmount;
    }

    public String getRecordType() {
        return recordType;
    }

    public String getPayersName() {
        return payersName;
    }

    public String getPayersAccountBank() {
        return payersAccountBank;
    }
}

如何降低圈复杂度?也许使用构建器或者我还能做什么?抽象 class 或者创建一个接口?

您可以使用组合,将此 class 分解为多个 class 并分离不相关的事物。这样,您将拥有多个 classes,使其更易于维护和扩展,并降低圈复杂度。

我已经使用工厂方法降低了 ciclomatyc 的复杂性,

public class DetailRecord extends StringManager {
private final String RECORD_TYPE = "06";
private String userMainReference;
private String secondaryReferenceOfTheUser;
private static final String BILLER_PERIODS = "00";
private String cycle;
private String mainServiceValue;
private static final String BILLED_SERVICE_CODE_BY_ADDITIONAL_COMPANY = "             ";
private static final String ADDITIONAL_SERVICE_VALUE = "              ";
private String expirationDate;
private String collectingBankId;
private String targetAccountNumber;
private String billerAccountType;
private String noBillerId;
private String billerName;
private static final String SOURCE_BANK_CODE = "   ";
private static final String RESERVED = "                        ";
private static final int lengthOfUserMainReference = 48;
private static final int lengthOfTheSecondaryReferenceOfTheUser = 30;
private static final int lengthOfTheCycle = 3;
private static final int lengthOfTheMainServiceValue = 14;
private static final int lengthOfTheCollectingBankId = 8;
private static final int lengthOfTargetAccountNumber = 17;
private static final int lengthOfNoBillerId = 10;
private static final int lengthOfBillerName = 22;


public DetailRecord(String userMainReference, String secondaryReferenceOfTheUser, String cycle, String mainServiceValue, String expirationDate, String collectingBankId, String targetAccountNumber, String billerAccountType, String noBillerId, String billerName) {
    this.userMainReference = userMainReference;
    this.secondaryReferenceOfTheUser = secondaryReferenceOfTheUser;
    this.cycle = cycle;
    this.mainServiceValue = mainServiceValue;
    this.expirationDate = expirationDate;
    this.collectingBankId = collectingBankId;
    this.targetAccountNumber = targetAccountNumber;
    this.billerAccountType = billerAccountType;
    this.noBillerId = noBillerId;
    this.billerName = billerName;
}

public static DetailRecord createWith(String userMainReference, String secondaryReferenceOfTheUser, String cycle, String mainServiceValue, String expirationDate, String collectingBankId, String targetAccountNumber, String billerAccountType, String noBillerId, String billerName) {
    return new DetailRecord(userMainReference, secondaryReferenceOfTheUser, cycle, mainServiceValue, expirationDate, collectingBankId, targetAccountNumber, billerAccountType, noBillerId, billerName);
}

@Override
public String toString() {
    return new StringBuilder()
            .append(RECORD_TYPE)
            .append(fillInWithZerosLeftPad(userMainReference, lengthOfUserMainReference))
            .append(fillInWithBlanksLeftPad(secondaryReferenceOfTheUser, lengthOfTheSecondaryReferenceOfTheUser))
            .append(BILLER_PERIODS)
            .append(fillInWithBlanksLeftPad(cycle, lengthOfTheCycle))
            .append(fillInWithZerosLeftPad(mainServiceValue, lengthOfTheMainServiceValue))
            .append(BILLED_SERVICE_CODE_BY_ADDITIONAL_COMPANY)
            .append(ADDITIONAL_SERVICE_VALUE)
            .append(expirationDate)
            .append(fillInWithZerosLeftPad(collectingBankId, lengthOfTheCollectingBankId))
            .append(fillInWithBlanksLeftPad(targetAccountNumber, lengthOfTargetAccountNumber))
            .append(billerAccountType)
            .append(fillInWithBlanksLeftPad(noBillerId, lengthOfNoBillerId))
            .append(fillInWithBlanksLeftPad(billerName, lengthOfBillerName))
            .append(SOURCE_BANK_CODE)
            .append(RESERVED).toString();
}

}