????????????????δ?????????????????????????????????????е?K-V????????С?????????????????1??С????????????????????1??С?????????????????????System.currentTimeMillis() ??JDK???????????????????????????????????????????????????????ж??????1??С??????????????????????????????????????????????????????????μ?????
// ????????
public interface SystemClock {
public long getCurrentTime();
}
// ???????е????
public class SystemClockImpl implements SystemClock {
@Override
public long getCurrentTime() {
return System.currentTimeMillis();
}
}
// ?????? ShortLifeMemoCache<T>
public class ShortLifeMemoCache<T> implements CacheService<T> {
private static final long DEFAULT_TTL = 1L*60*60*1000;
/**
* ???????λ:????
*/
private SystemClock systemClock;
/**
* ???????(Time to live)????λ:????
*/
private long ttl = DEFAULT_TTL;
private final Map<String?? Value<T>> cache =  new HashMap<String?? Value<T>>();
@Override
public void put(String key?? T t) {
long life = systemClock.getCurrentTime() + ttl;
cache.put(key?? new Value<T>(t?? life));
}
@Override
public T get(String key) {
Value<T> v = cache.get(key);
if (v != null){
if(v.getLife() > systemClock.getCurrentTime()) {
return v.getValue();
} else {
cache.remove(key);
}
}
return null;
}
}
???????????????????????????????????????????????????????д???????????????????????????????
// ??????????????ttl=10???????FakeSystemClock????
public void testValueTimeout() {
long timeToLive = 10;
FakeSystemClock systemClock = new FakeSystemClock();
systemClock.setCurTime(0);
ShortLifeMemoCache<String> cacheService = new ShortLifeMemoCache<String>();
cacheService.setSystemClock(systemClock);
cacheService.setTtl(timeToLive);
cacheService.put("key1"?? "value1");
// after 10 seconds
systemClock.setCurTime(10);
assertNull(cacheService.get("key1"));
}
// Mock ?????
static class FakeSystemClock implements SystemClock {
private long curTime;
@Override
public long getCurrentTime() {
return curTime;
}
public long getCurTime() {
return curTime;
}
public void setCurTime(long curTime) {
this.curTime = curTime;
}
}
???????????????????????? - ??????????????????????????????????????????????????????????????????????????????????У???????????????????????????????????????????????????????ЩAndroid????????????????MVP??????? - ??????????MVP???????????View??Presenter??????????????????????MVP??????????????ù?????MVP????90?????????????10??????????????????鼮???棬???????????????MVP?????????????????????????MVP???????PassiveView-MVP????????????MVP???????????UI???С?3????
????????????????????б?????????y????????????????????????Щ???????????????????Mock?Щ????????API??????????????ЩMock????????? EasyMock??mockito ?????????Mock???????????????????jUnit??UI??????????? Robotium ??.
????????Android??UI?????????????????????????????????UI????/Ч????UI/????????????????????2????????С?????????????????????????UI???????????????????Model???棨Model???????????ModelingTheWorld????Model?????????????????????
????PS?????????
???????????????????????????? getter ?? setter ??????????????????????е???????????????????????“???????”???????????????????????????δ???
public class Foo {
private String message = "Hello World";
private String View view = new View();
public void print() {
System.out.println(message);
}
public void showMessage() {
view.display(message);
}
}
??????δ????? message ?? view ??????Foo??????????????????????? - ???????? ?????????? ?????????????к????????磺
//???????????ε??????
publicFoo(Stringmessage??Viewview){
this.message=message;
this.view=view;
}
//??????setter????
publicvoidsetMessage(Stringmessage){
this.message=message;
}
publicvoidsetView(Viewview){
this.view=view;
}
?????????????????????????????????????????????????????д?????????£???????????к??????????????Ioc?????????????Spring????????????????????????Android???????е?Dagger????