- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个供外部用户使用的库,但我坚持一些基本的设计决策。
我正在编写一个简单的 POJO 类,它将保存一些有关 OAuth2 token 的信息。
该类需要满足以下条件:
这是我到目前为止所拥有的:
public TokenInformation(String token, Date expirationDate, String refreshToken) {
Objects.requireNonNull(token, "token parameter cannot be null.");
this.token = token;
if ((expirationDate != null) && (refreshToken != null)) {
this.expiresIn = expiresIn;
this.refreshToken = refreshToken;
isPermanentToken = false;
} else if ((expirationDate == null) && (refreshToken == null)) {
this.expiresIn = null;
this.refreshToken = null;
isPermanentToken = true;
} else {
// throw some exception here
}
}
说实话,我对代码的外观并不完全满意。
但我确实有以下想法:
我认为第二个想法可能是最好的方法。例如,我想不出任何 Java API 需要我们传递 null,这可能暗示我粘贴的代码是一个坏主意。此外,Java 大量使用工厂方法,因此对于我的库的用户来说这不会是一个陌生的模式。
不过我想听听其他人的意见。因此,如果您需要任何其他信息,请告诉我。
最佳答案
我更愿意将永久和临时 token 行为封装到各自的域中,以便您库的任何用户都清楚地知道正在实例化哪种 token
我认为建议的类(class):
/**
* The base class encapsulates the behavior of generic token
*/
public class AbstractToken {
protected String token;
// other properties that exist very closely with token
public String getToken() {
return token;
}
}
永久 token 域名
/**
* The domain encapsulates the behaviour of Permanent token - token that never expires
*/
public class PermanentToken extends AbstractToken {
// more attributes that makes general token as Parmament token
/**
* Instantiates a new Permanent token - this token never expires
*
* @param token the token
*/
public PermanentToken(String token) {
this.token = token;
}
}
临时 token 的域:
/**
* The domain for Temporary token.
*/
public class TemporaryToken extends AbstractToken {
private Date expirationDate;
private String refreshToken;
// more attributes that makes general token as temporary token
/**
* Instantiates a new Temporary token with token expiry date and refresh token
*
* @param token the token
* @param expirationDate the expiration date
* @param refreshToken the refresh token
*/
public TemporaryToken(String token, Date expirationDate, String refreshToken) {
this.token = token;
this.expirationDate = expirationDate;
this.refreshToken = refreshToken;
}
}
现在,您的库的用户清楚地知道他/她想要实例化和使用哪种 token 。
P.S. - I guess you would be able to keep better names for your domains and you as deep into the business for your library.
关于java - 作为库作者,期望用户根据情况将 null 传递给方法参数是不是很糟糕的设计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188133/
我想在这里说的是我在从之前离开的相同状态重新启动我的应用程序时遇到的问题。我在这方面做了很多研发,并且已经解决了 stackoverflow 中发布的问题。所以请不要说它是重复的。 我试过设置这些选项
当我在我的类中实现 __cmp__ 函数时,python 是否会在内部重载“==”,我们在 C++ 中是如何做到的? 只是好奇。我是 python 的新手。 :) 最佳答案 ==的含义当您定义 __c
我在 Raspberry Pi2 上安装了 Gitlab,几个月来它运行良好。但自从关闭了RPi的电源后,它就不再起作用了。网页返回502错误。 502 Whoops, GitLab is takin
有人知道用户登陆带有Webfonts的页面时为什么Google Chrome浏览器崩溃吗 它并不会一直发生,而是经常发生 我刚得到一个蓝屏页面,却不知道为什么:该页面不是来自重定向时就很好了。 这是我
当我登录时,Skype始终会给出此错误。 糟糕,Skype存在问题。尝试注销然后重新登录。 STARTUP_LOAD_ERROR MACBOOK 最佳答案 Macbook 用户 退出Skype 回家
我正在尝试从 flutter 开始,首先我在 cmd 上运行 flutter doctor 它有效。在我安装了 ANDROID SDK 之后,同样的命令 flutter doctor 给了我异常:
从 android studio 终端运行 flutter attach 不工作。显示错误flutter 意外退出。 终端输出: flutter attach Checking for adverti
当使用 TinyMCE 4 测试所有浏览器时,Chrome 非常慢。 (我尝试从 TinyMCE 中删除所有插件,但没有任何区别。) Chrome 需要大约 20-25 秒在 TinyMCE 中呈现一
我试图让下面的脚本工作,以便从远程服务器(服务器 1)读取特定目录中的 CSV 文件列表,并将数据移植到另一台服务器的 PostgreSQL 数据库中。 我已经创建了一个 rsa SSH key 并将
在嵌入式 linux 环境中(在 PowerPC 上定制的 2.4.25)几个小时后我得到以下内核 panic : Oops: kernel access of bad area, sig: 11 N
在将现有 Node.js (Hapi.js) + RethinkDB 从 OVH VPS(最小 vps)迁移到 AWS Lambda( Node )+ DynamoDB 的过程中,我最近遇到了一个非常
我是一名优秀的程序员,十分优秀!