首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Java 實體類重構:消除重複 set/get 代碼的三種模式

     編輯:程式設計綜合問答

痛點代碼

if ("1".equals(subjectCode)) {
    TestM m = new TestM();
    m.setTeachingCode(teachingVersionCode);
    m.setGradeCode(gradeCode);
    m.setTermCode(Integer.valueOf(termCode));
    m.setQuesTestTypeCode(Integer.valueOf(testTypeCode));
    List list = testService.getTestMList(m, pageNo);
    int count = testService.getPaperMCount(m);
}
if ("2".equals(subjectCode)) { ... TestE ... }
if ("3".equals(subjectCode)) { ... TestC ... }

每新增一科目就要複製一大塊,違反 DRY 原則,維護極其痛苦。

重構模式一:提取公共父類 + 模板方法

// 1. 抽象基類封裝共同屬性與構造
public abstract class BaseTest {
    protected String teachingCode;
    protected String gradeCode;
    protected Integer termCode;
    protected Integer testTypeCode;

    public BaseTest(String teachingCode, String gradeCode, String termCode, String testTypeCode) {
        this.teachingCode = teachingCode;
        this.gradeCode = gradeCode;
        this.termCode = Integer.valueOf(termCode);
        this.testTypeCode = Integer.valueOf(testTypeCode);
    }
    // 共同 getter/setter...
}

// 2. 子類只需聲明差異屬性
public class TestM extends BaseTest {
    private String mathSpecificField;
    public TestM(String tc, String gc, String term, String type) { super(tc, gc, term, type); }
}

// 3. Service 統一接口
public interface TestService {
    List getList(T param, int pageNo);
    int getCount(T param);
}

// 4. 策略工廠路由
@Component
public class TestFactory {
    private final Map> services = new HashMap<>();
    @Autowired public TestFactory(TestMService m, TestEService e, TestCService c) {
        services.put("1", m); services.put("2", e); services.put("3", c);
    }
    public TestService getService(String subjectCode) {
        return services.get(subjectCode);
    }
}

// 5. 調用端極簡
TestService svc = factory.getService(subjectCode);
BaseTest param = new TestM(tc, gc, term, type); // 或反射/構造器映射
List list = svc.getList(param, pageNo);
int count = svc.getCount(param);

重構模式二:JSON 反序列化自動映射(適合屬性名一致)

// 通用構建參數對象
JSONObject json = new JSONObject();
json.put("teachingCode", teachingVersionCode);
json.put("gradeCode", gradeCode);
json.put("termCode", termCode);
json.put("testTypeCode", testTypeCode);

// 根據 subjectCode 確定目標類
Class clazz = Map.of(
    "1", TestM.class, "2", TestE.class, "3", TestC.class
).get(subjectCode);

BaseTest param = JSONObject.toBean(json, clazz); // 自動 set 同名屬性

重構模式三:Builder 模式 + 函數式選項(現代風格)

public class TestParam {
    private String teachingCode, gradeCode;
    private Integer termCode, testTypeCode;
    // ... 差異字段用 Map 擴展
    private Map ext = new HashMap<>();

    private TestParam(Builder b) { this.teachingCode = b.teachingCode; ... this.ext = b.ext; }

    public static class Builder {
        // 必填
        private String teachingCode, gradeCode;
        // 選填
        private Integer termCode, testTypeCode;
        private Map ext = new HashMap<>();

        public Builder teachingCode(String v) { this.teachingCode = v; return this; }
        public Builder ext(String k, Object v) { ext.put(k, v); return this; }
        public TestParam build() { return new TestParam(this); }
    }
}

// 使用
TestParam param = new TestParam.Builder()
    .teachingCode(tc).gradeCode(gc)
    .termCode(Integer.valueOf(termCode))
    .testTypeCode(Integer.valueOf(testTypeCode))
    .ext("mathField", "xxx") // 科目特有
    .build();

選型建議

場景推薦模式
層次穩定、共同屬性多、行為多態模式一:父類 + 策略工廠
屬性名高度一致、臨時 DTO、無行為模式二:JSON 反序列化
構建參數複雜、可選項多、鏈式調用模式三:Builder + Map 擴展

總結

核心思想:**將「不變的共同部分」上移到父類/基類/通用構建器,「變化的差異部分」通過繼承、反射、Map 擴展隔離**。消除 if-else 鏈,新增科目只需新增實現類並註冊到工廠,符合開閉原則。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved