简体   繁体   English

Java OOP多态性设计/问题

[英]Java OOP polymorphism design/issue

I am creating a very basic Cache object. 我正在创建一个非常基本的Cache对象。 Here is my code: 这是我的代码:

Cache.java is an abstract class meant to be overriden. 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;
    }
}

This is a sample of a class that overrides it, ACache.java : 这是一个覆盖它的类的示例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
    }
}

My problem here is that I want to hide the buildAndUpdate() method and just expose the build() method of Cache because in order for the Cache to be updated, I would want to check if it's expired first. 我的问题是我想隐藏buildAndUpdate()方法,而只公开Cachebuild()方法,因为为了更新Cache ,我想先检查一下它是否已过期。

Since buildAndUpdate() is protected , the method can be accessed by the class itself. 由于buildAndUpdate() protected ,因此类本身可以访问该方法。 How do I proceed with what I want to do? 我该如何继续做我想做的事情? How can you improve my implementation? 您如何改善我的实施?

EDIT 1: Took ControlAltDel and Turing85's advice and went with IoC. 编辑1:接受了ControlAltDel和Turing85的建议,并接受了IoC。 I created an interface called BuildStrategy that has a void buildAndUpdate() method. 我创建了一个名为BuildStrategy的接口,该接口具有一个void buildAndUpdate()方法。 Is this correct? 这个对吗?

One way you could go would be to get rid of this method entirely, and instead create at BuildAndUpdate class, which would be a required parameter in the constructor. 一种可行的方法是完全摆脱此方法,而在BuildAndUpdate类中创建,这将是构造函数中的必需参数。 You could then subclass your Cache class, and in an empty constructor, initialize the superclass with a BuildAndUpdate object. 然后,可以子类化Cache类,并在一个空的构造函数中,使用BuildAndUpdate对象初始化超类。

Make sense? 说得通?

you can use generics. 您可以使用泛型。 Not sure why you need class to be abstract. 不知道为什么需要类是抽象的。 People who require special behaviour, they can extend your class. 需要特殊行为的人可以扩展您的课程。

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

What I ended up doing is I moved the class that managed all the Cache objects in another package. 我最终要做的是,将管理所有Cache对象的类移到了另一个包中。 I liked the idea of Inversion of Control though, makes the code looks smoother and modular - which is why I marked it as the best answer. 我喜欢控制反转的思想,它使代码看起来更加平滑和模块化-这就是为什么我将其标记为最佳答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM