Java OOP多态性设计/问题



我正在创建一个非常基本的Cache对象。下面是我的代码:

Cache.java是一个被重写的抽象类。

public abstract class Cache {
    protected Date dateCreated;
    protected long expiration;
    private BuildStrategy strategy;
    protected Cache(long expiration, BuildStrategy strategy) {
        this.dateCreated = new Date();
        this.expiration = expiration;
        this.strategy = strategy;
        strategy.buildAndUpdate();
    }
    private final boolean isExpired() {
        long duration = new Date().getTime() - this.dateCreated.getTime();
        if (duration > expiration) {
            return true;
        }
        return false;
    }
    protected void build() {
        if (!isExpired())
            return;
        setDateCreated(new Date());
        buildAndUpdate();
    }
    protected abstract void buildAndUpdate();
    final Date getDateCreated() {
        return dateCreated;
    }
    final void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    final long getExpiration() {
        return expiration;
    }
    final void setExpiration(long expiration) {
        this.expiration = expiration;
    }
}

这是一个覆盖它的类的示例,ACache.java:

   public class ACache extends Cache {
    protected ACache(long expiration) {
        super(expiration);
    }
    private Object variableToBeUpdated;
    public Object getVariableToBeUpdated() {
        return variableToBeUpdated;
    }
    public void setVariableToBeUpdated(Object variableToBeUpdated) {
        this.variableToBeUpdated = variableToBeUpdated;
    }
    @Override
    protected void buildAndUpdate() {
        // ...connects to the database etc...
        // ...once database stuff is done, update variableToBeUpdated
        // NOTE: Other caches may implement buildAndUpdate() differently, that's
        // why it's abstract
    }
}

我这里的问题是,我想隐藏buildAndUpdate()方法,只是暴露Cachebuild()方法,因为为了更新Cache,我想先检查它是否过期。

由于buildAndUpdate()protected,该方法可以被类本身访问。我该如何继续做我想做的事?如何改进我的实现?

编辑1:接受了controlaldel和图灵85的建议,选择了IoC。我创建了一个名为BuildStrategy的接口,它有一个void buildAndUpdate()方法。这是正确的吗?

您可以采取的一种方法是完全摆脱这个方法,而不是在BuildAndUpdate类中创建,这将是构造函数中必需的参数。然后你可以继承你的缓存类,并在一个空的构造函数中,用BuildAndUpdate对象初始化超类。

有意义吗?

可以使用泛型。不确定为什么需要类是抽象的。需要特殊行为的人,可以扩展你的类。

import java.util.Date;
import java.util.Map;
public  class  Cache<K,V> {
private Map<K,V> map;
protected Date dateCreated;
protected long expiration;
protected Cache(long expiration) {
    this.dateCreated = new Date();
    this.expiration = expiration;
    buildAndUpdate();
}
private final boolean isExpired(){
    long duration = new Date().getTime() - this.dateCreated.getTime();
    if (duration > expiration){
        return true;
    }
    return false;
}
protected void build(){
    if (!isExpired()) return;
    setDateCreated(new Date());
    buildAndUpdate();
}
protected void buildAndUpdate(){
    //populate map here
}
final Date getDateCreated() {
    return dateCreated;
}
final void setDateCreated(Date dateCreated) {
    this.dateCreated = dateCreated;
}
final long getExpiration() {
    return expiration;
}
final void setExpiration(long expiration) {
    this.expiration = expiration;
}

我最终做的是将管理所有Cache对象的类移动到另一个包中。我喜欢控制反转的想法,使代码看起来更流畅和模块化-这就是为什么我把它标记为最佳答案。

最新更新