Kaynağa Gözat

refactor(utils): 为工具类添加私有构造函数

- 在多个工具类中添加私有构造函数,防止实例化
-这种设计模式确保了工具类的静态方法和属性的正确使用
- 受影响的主要类包括:
  - AlgorithmHelper
  - AppStorageHelper
  - LogHelper
  - PreferencesHelper
  - SZYXLocalStorageHelper
  - SZYXLocalStorageKeys
  - TimeHelper
  - ToolsHelper  - ToolsHelperForTS - ValidatorHelper
  - WindowHelper
  - XWebHelper
徐勤民 5 ay önce
ebeveyn
işleme
9c6141f7fa

+ 2 - 0
src/main/ets/utils/AlgorithmHelper.ets

@@ -2,6 +2,8 @@
  * 计算相关工具类
  */
 export class AlgorithmHelper {
+  private constructor() {
+  }
   /**
    * 计算两点间距离
    * @param x1

+ 2 - 0
src/main/ets/utils/AppStorageHelper.ets

@@ -2,6 +2,8 @@
  * 永久化存储,存储在本地文件
  */
 export class AppStorageHelper {
+  private constructor() {
+  }
   /**
    * 缓存
    * @param key

+ 2 - 2
src/main/ets/utils/LogHelper.ets

@@ -5,6 +5,8 @@ const LOGGER_DOMAIN: number = 0x0000
 const LOGGER_TAG: string = '=====》'
 
 export class LogHelper {
+  private constructor() {
+  }
   private static domain: number = LOGGER_DOMAIN
   private static tag: string = LOGGER_TAG //日志Tag
   private static format: string = '%{public}s'
@@ -130,5 +132,3 @@ export class LogHelper {
     }
   }
 }
-
-// export default new LogHelper() //单例(在ES6模块中,当你使用 import 导入一个模块时,实际上是在导入该模块的值的一个引用。这意味着在另一个模块中修改该值会影响原始模块中的值。)

+ 4 - 0
src/main/ets/utils/PreferencesHelper.ets

@@ -1,6 +1,9 @@
 import preferences from '@ohos.data.preferences';
 
 export class PreferencesHelper {
+  private constructor() {
+  }
+
   public static async put(key: string, value: preferences.ValueType | undefined | null) {
 
     const pref = await preferences.getPreferences(getContext(), 'PreferencesHelper')
@@ -24,6 +27,7 @@ export class PreferencesHelper {
     })
 
   }
+
   public static async delete(key: string): Promise<void> {
     return new Promise(async (resolve, reject) => {
 

+ 2 - 0
src/main/ets/utils/SZYXLocalStorageHelper.ts

@@ -1,3 +1,5 @@
 export class SZYXLocalStorageHelper{
+  private constructor() {
+  }
   public static storage: LocalStorage = new LocalStorage();
 }

+ 2 - 0
src/main/ets/utils/SZYXLocalStorageKeys.ts

@@ -1,5 +1,7 @@
 
 export class SZYXLocalStorageKeys{
+  private constructor() {
+  }
   public static HttpHandlerList: string = 'httpHandlerList';
   public static HttpHandlerListLength: string = 'HttpHandlerListLength';
   public static XWebViewCLose: string = 'SZYX_XWebViewCLose';

+ 2 - 0
src/main/ets/utils/TimeHelper.ts

@@ -14,6 +14,8 @@
  */
 
 export class TimeHelper {
+  private constructor() {
+  }
   /**
    * 获取当前时间戳
    * @returns

+ 2 - 0
src/main/ets/utils/ToolsHelper.ets

@@ -100,6 +100,8 @@ interface ThrottleInterface {
  * 常用方法
  */
 export class ToolsHelper {
+  private constructor() {
+  }
   /**
    * 弹出Toast
    * @param msg

+ 2 - 0
src/main/ets/utils/ToolsHelperForTS.ts

@@ -4,6 +4,8 @@ import { HashMap } from '@kit.ArkTS';
  * 常用方法,部分方法,在ets里面不能用
  */
 export class ToolsHelperForTS {
+  private constructor() {
+  }
   // Map转为Record
   static mapToRecord(myMap: HashMap<string, string>): Record<string, string> {
     return Object.fromEntries(myMap.entries()) as Record<string, string>;

+ 2 - 0
src/main/ets/utils/ValidatorHelper.ets

@@ -2,6 +2,8 @@
  * 常用正则验证
  */
 export class ValidatorHelper {
+  private constructor() {
+  }
   /**
    * 是否为手机号
    * @param phone

+ 2 - 0
src/main/ets/utils/WindowHelper.ets

@@ -3,6 +3,8 @@ import { common } from '@kit.AbilityKit';
 
 
 export class WindowHelper {
+  private constructor() {
+  }
   /**
    * 缓存窗体,关闭时需要
    * 同时只能出现一个窗口,所以只做一个缓存就可以

+ 2 - 0
src/main/ets/utils/XWebHelper.ets

@@ -14,6 +14,8 @@ export interface XWebParams {
 }
 
 export class XWebHelper {
+  private constructor() {
+  }
   /**
    * 打开web页面,加载h5
    * @param params