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

- 在多个工具类中添加私有构造函数,防止实例化
-这种设计模式确保了工具类的静态方法和属性的正确使用
- 受影响的主要类包括:
  - AlgorithmHelper
  - AppStorageHelper
  - LogHelper
  - PreferencesHelper
  - SZYXLocalStorageHelper
  - SZYXLocalStorageKeys
  - TimeHelper
  - ToolsHelper  - ToolsHelperForTS - ValidatorHelper
  - WindowHelper
  - XWebHelper
这个提交包含在:
徐勤民 2024-10-31 12:28:44 +08:00
父节点 9a2f82c33c
当前提交 9c6141f7fa
共有 12 个文件被更改,包括 26 次插入2 次删除

查看文件

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

查看文件

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

查看文件

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

查看文件

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

查看文件

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

查看文件

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

查看文件

@ -14,6 +14,8 @@
*/ */
export class TimeHelper { export class TimeHelper {
private constructor() {
}
/** /**
* *
* @returns * @returns

查看文件

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

查看文件

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

查看文件

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

查看文件

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

查看文件

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