Java OOP多态设计/问题

时间:2015-05-27 19:40:36

标签: java oop polymorphism

我正在创建一个非常基本的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()方法,只展示build() Cache方法,因为为了更新Cache,我我想先检查它是否已过期。

由于buildAndUpdate()protected,因此类本身可以访问该方法。我该如何处理我想做的事情?你怎么能改进我的实施?

编辑1:采取ControlAltDel和Turing85的建议并采用IoC。我创建了一个名为BuildStrategy的接口,它具有void buildAndUpdate()方法。这是对的吗?

3 个答案:

答案 0 :(得分:2)

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

有意义吗?

答案 1 :(得分:1)

你可以使用泛型。不确定为什么你需要课程来抽象。需要特殊行为的人,他们可以扩展你的课程。

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;
}

答案 2 :(得分:0)

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