优化:基于阿里巴巴 Java 开发手册(黄山版)优化常量及包命名

1.编程规约>常量定义>第4条:
【推荐】不要使用一个常量类维护所有常量,要按常量功能进行归类,分开维护。
说明:大而全的常量类,杂乱无章,使用查找功能才能定位到要修改的常量,不利于理解,也不利于维护。
正例:缓存相关常量放在类 CacheConsts 下;系统配置相关常量放在类 SystemConfigConsts 下。
2.编程规约>常量定义>第5条:
【推荐】常量的复用层次有五层:跨应用共享常量、应用内共享常量、子工程内共享常量、包内共享常
量、类内共享常量。
  1)跨应用共享常量:放置在二方库中,通常是 client.jar 中的 constant 目录下。
  2)应用内共享常量:放置在一方库中,通常是子模块中的 constant 目录下。
  反例:易懂常量也要统一定义成应用内共享常量,两个程序员在两个类中分别定义了表示“是”的常量:
  类 A 中:public static final String YES = "yes";
  类 B 中:public static final String YES = "y";
  A.YES.equals(B.YES),预期是 true,但实际返回为 false,导致线上问题。
  3)子工程内部共享常量:即在当前子工程的 constant 目录下。
  4)包内共享常量:即在当前包下单独的 constant 目录下。
  5)类内共享常量:直接在类内部 private static final 定义。
This commit is contained in:
2023-03-03 22:34:19 +08:00
parent 94be1f9553
commit 1257a4bc35
19 changed files with 61 additions and 61 deletions

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.constant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* 缓存相关常量
*
* @author Charles7c
* @since 2022/12/22 19:30
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CacheConsts {
/**
* 登录用户缓存键
*/
public static final String LOGIN_USER_CACHE_KEY = "LOGIN_USER";
/**
* 验证码缓存键
*/
public static final String CAPTCHA_CACHE_KEY = "CAPTCHA";
/**
* 限流缓存键
*/
public static final String LIMIT_CACHE_KEY = "LIMIT";
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.constant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import cn.hutool.core.text.CharPool;
/**
* 字符相关常量
*
* @author Charles7c
* @since 2023/2/10 20:14
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CharConsts implements CharPool {
/**
* 分号
*/
public static final String SEMICOLON = ";";
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.constant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* 文件相关常量
*
* @author Charles7c
* @since 2023/1/2 21:19
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FileConsts {
/**
* 头像支持的图片类型
*/
public static final String[] AVATAR_SUPPORTED_IMG_TYPES = {"jpg", "png", "gif", "jpeg"};
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.constant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* 正则相关常量
*
* @author Charles7c
* @since 2023/1/10 20:06
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RegExpConsts {
/**
* 密码正则必须包含字母和数字的组合可以使用特殊字符长度在6-32之间
*/
public static final String PASSWORD = "^(?=.*\\d)(?=.*[a-z]).{6,32}$";
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.constant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* 系统相关常量
*
* @author Charles7c
* @since 2023/2/9 22:11
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SysConsts {
/**
* 超级管理员角色编码
*/
public static final String SUPER_ADMIN = "admin";
/**
* 全部权限标识
*/
public static final String ALL_PERMISSION = "*";
/**
* 默认密码
*/
public static final String DEFAULT_PASSWORD = "123456";
}