高階基礎 常見常量類的設計方式 入門篇

2021-10-16 13:13:17 字數 3568 閱讀 7235

一、介紹

傳統開我們只是將常量類用於作為字典功能儲存一些不容易改變的狀態等資訊,而實際上來自於日常開發的設計,常量類的設計方式主要有四種

1、超級列舉

2、註冊單例

3、字典常量

4、策略工廠

二、步驟

《超級列舉》

大部分人使用常量類的時候均用於不超過兩個常量屬性的儲存,而實際上常量類可以理解為乙個被static final修改的類,既然作為類,那麼其本身是允許具有多個成員變數和行為方法的。我可以這樣使用:

// 身份證對映碼

@getter

public enum medicalinsurancecerttype , "身份證","10"),

medical_insurance_cert_type_passport(new string , "護照", "20"),

medical_insurance_cert_type_official(new string , "軍官證", "30"),

medical_insurance_cert_type_mainland(new string , "港澳台", "40"),

medical_insurance_cert_type_workcard(new string , "工作簽證", "50"),

medical_insurance_cert_type_permits(new string , "居留證", "60"),

medical_insurance_cert_type_livepermits(new string , "長期居住證", "70"),

medical_insurance_cert_type_others(new string , "其他", "100");

private string certtype;

private string certtypemsg;

private string certtypebody;

medicalinsurancecerttype(string certtype, string certtypemsg, string certtypebody)

}

說明:在列舉類中使用多屬性將不同型別的資料進行對映歸併,比如圖中每乙個列舉物件都對應著三個屬性

《註冊單例》

在日常中,個人使用單例的地方並不是特別多,而實際上我們的單例物件是交給了ioc容器進行管理,ioc使用了容器進行管理。在物件被建立後儲存起來,通過固定的key值進行獲取,從而避免被反射暴力破壞。ioc容器中如果使用相同的class資訊建立物件會獲取到相同的key值,從而避免了出現多個相同例項。在我們日常的開發中,可以使用列舉來實現該模式:

class<?> clazz = ***xx.class;

//通過反射回去私有構造方法

constructor constructor = clazz.getdeclaredconstructor(null);

//強制訪問

constructor.setaccessible(true);

//暴力初始化,建立兩個例項

object o1 = constructor.newinstance();

object o2 = constructor.newinstance();

註冊單例 (**來自網路)

這是乙個列舉

public enum enumsingleton

public object getinstance()

}這個測試**

public static void main(string args)

});}

executorservice.shutdown();

}

《字典常量》

用於儲存字典和匹配資訊

// 身份證對映碼

@getter

public enum medicalinsurancecerttype , "身份證","10"),

medical_insurance_cert_type_passport(new string , "護照", "20"),

medical_insurance_cert_type_official(new string , "軍官證", "30"),

medical_insurance_cert_type_mainland(new string , "港澳台", "40"),

medical_insurance_cert_type_workcard(new string , "工作簽證", "50"),

medical_insurance_cert_type_permits(new string , "居留證", "60"),

medical_insurance_cert_type_livepermits(new string , "長期居住證", "70"),

medical_insurance_cert_type_others(new string , "其他", "100");

private string certtype;

private string certtypemsg;

private string certtypebody;

medicalinsurancecerttype(string certtype, string certtypemsg, string certtypebody)

public static string getcerttypebody(string certtypebody)

}return "100";

}}

說明:通過靜態化放進行匹配從而獲取到對應對映碼,這是在做第三方介面經常用到的方式 

《策略工廠》

對於乙個業務的某個功能,具有多種可選擇的方法和型別(使用者可自選),這種情況下通過列舉工廠的形式將多個型別給統一管理起來,通過將工廠開放出去從而統一對外的管理。(**來自網路)

public class paystrategyfactory 

/*** 獲取支付方式類

** @param paytype 前端傳入支付方式

* @return

*/public static payment getpayment(string paytype)

return payment;

}}

訂單這一工具天然具備被支付的行為,可以將支付的型別裹挾在訂單裡面

public class neworder 

/*** 訂單支付方法

** @return

*/public boolean pay(string paytype)

return paysuccess;

}}

測試案例

public static void main(string args)
三、總結

在開發中,為了方便經常是會將字典常量和超級列舉和策略工廠的方式結合在一起使用。但具體應用還是好區分不同的場景,不能一次性全部用上,也沒意義。

有問題歡迎上報和分享!

C 程式設計基礎 類中的常量

首先看看常量定義規則 規則1 需要對外公開的常量放在標頭檔案中,不需要對外公開的常量放在定義檔案的頭部。為了便於管理,可以把不同模組的常量集中放在乙個公共的標頭檔案中。例如 const float radius 100 const float dlameter radius 2 類中的常量 有時候我...

基礎題 類 類的設計思路

include include using namespace std class student int main student student int 變數a void student set 變數a int 變數a int student get 變數a 成員變數無參建構函式 有參建構函式 ...

融合CURD的類的設計方式

在學習.的早期,設計類時習慣把crud的方法也寫到類中,後來考慮到這樣的寫法不方便擴充套件,特別當資料庫型別移植時要做很多的工作,但是還是把他寫出來。using system using system.data using system.data.oracleclient using fjeptri...