2015-05-27 62 views
0

我正在创建一个非常基本的Cache对象。这里是我的代码:Java OOP多态设计/问题

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:采取ControlAltDel和Turing85的建议,并与IoC一起去。我创建了一个名为BuildStrategy的接口,它有一个void buildAndUpdate()方法。它是否正确?

+2

'Cache'类中的'buildAndUpdate'是抽象的,所以不能从'ACache'中调用,如果这是你所担心的。 – GriffeyDog

+0

什么?它完全可以在'ACache'中调用。 'ACache'覆盖'buildAndUpdate()'方法,并且因为它是'Cache'中的'protected abstract',这意味着它在'ACache'中被覆盖时将具有'protected'修饰符。这就是问题所在。 – mpmp

+0

暴露给谁? – biziclop

回答

2

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

有意义吗?

+0

你的意思是[控制反转](http://en.wikipedia.org/wiki/Inversion_of_control)? – Turing85

+0

嗯......我迷失在'......这将是构造函数中必需的参数。“我不明白你的意思。 – mpmp

+0

@MiguelPortugal看看我的链接。基本上,您需要构建一个管理对象,管理一个特定“Cache”的'buildAndUpdate'进程并将该管理对象传递到'Cache'类(应将其存储为属性以供将来使用)。 – Turing85

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

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