这个问题也可以在以下链接中找到 Question at this link 我能够清除 3 个测试用例中的 2 个,但无法清除 1 个测试用例。我还将在这里上传我的代码。
●创建一个新的包本地接口(interface),名为AlertDAO,其中包含与MapAlertDAO相同的方法。
●MapAlertDAO 应实现 AlertDAO 接口(interface)。
●AlertService 应该有一个接受 AlertDAO 的构造函数。
●raiseAlert 和 getAlertTime 方法应使用通过构造函数传递的对象
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
interface AlertDAO
{
public UUID addAlert(Date time);
public Date getAlert(UUID id);
}
class AlertService
{
private AlertDAO objAlertDAO;
private final MapAlertDAO storage = new MapAlertDAO();
public AlertService(AlertDAO objAlertDAO)
{
this.objAlertDAO=objAlertDAO;
}
public UUID raiseAlert()
{
return this.storage.addAlert(new Date());
}
public Date getAlertTime(UUID id)
{
return this.storage.getAlert(id);
}
}
class MapAlertDAO implements AlertDAO
{
private final Map<UUID, Date> alerts = new HashMap<UUID, Date>();
public UUID addAlert(Date time)
{
UUID id = UUID.randomUUID();
this.alerts.put(id, time);
return id;
}
public Date getAlert(UUID id)
{
return this.alerts.get(id);
}
public static void main(String args[])
{
AlertService obj =new AlertService(new MapAlertDAO());
}
}
最佳答案
传递代码
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
class AlertService {
private final AlertDAO storage;
public AlertService(AlertDAO storage) {
this.storage = storage;
}
public UUID raiseAlert() {
return this.storage.addAlert(new Date());
}
public Date getAlertTime(UUID id) {
return this.storage.getAlert(id);
}
}
interface AlertDAO {
UUID addAlert(Date time);
Date getAlert(UUID id);
}
class MapAlertDAO implements AlertDAO {
private final Map<UUID, Date> alerts = new HashMap<UUID, Date>();
@Override
public UUID addAlert(Date time) {
UUID id = UUID.randomUUID();
this.alerts.put(id, time);
return id;
}
@Override
public Date getAlert(UUID id) {
return this.alerts.get(id);
}
}
关于java - 警报服务(重构AlertService和MapAlertDAO类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52886604/