简体   繁体   English

抽象 class 构造函数参数与最终数据的抽象方法

[英]Abstract class constructor param vs. abstract method for final data

What are the pros/cons of using the abstract class constructor vs. an abstract method for passing final data to an abstract class?使用抽象 class 构造函数与将最终数据传递给抽象 class 的抽象方法的优缺点是什么?

Pass via constructor:通过构造函数传递:

public abstract class MyAbstractClass<T> {
  private final String type;
  private final Function<String, T> factoryFn;

  protected MyAbstractClass(String type, Function<String, T> factoryFn) {
    this.type = type;
    this.factoryFn = factoryFn;
  }

  public T doSomething(String value) { ... }
}

Pass via abstract method:通过抽象方法传递:

public abstract class MyAbstractClass<T> {
  abstract String getType();

  abstract T getFactoryFn(String value);

  public T doSomething(String value) { ... }
}

I'm aware that the abstract methods can potentially be misused, because it doesn't enforce to always return the same value.我知道抽象方法可能会被滥用,因为它并不强制总是返回相同的值。

But apart from that, is it just a matter of personal preference, or are there any real (dis)advantages for using one over the other?但除此之外,这只是个人喜好问题,还是使用一个比另一个有任何真正的(缺点)优势?

I hope I am understanding your question correctly..我希望我能正确理解你的问题..

Usually, when a property of a class is always held in a field, it is more concise to use an abstract constructor.通常,当 class 的属性始终保存在字段中时,使用抽象构造函数会更简洁。 For example, consider the two following scenarios....例如,考虑以下两种情况......

// Scenario 1:
abstract class AClass {
    final int field;
    public AClass(int f) {
        field = f;
    }

    public int getField() {
        return field;
    }
}

class Class1 extends AClass {
    public Class1(int f) {
        super(f);
    }

    // Class Unique Code...
}

class Class2 extends AClass {
    public Class2(int f) {
        super(f);
    }

    // Class Unique Code...
}


// Scenario 2:
abstract class AClass {
    public abstract int getField();
}

class Class1 extends AClass {
    final int field;

    public Class1(int f) {
        field = f;
    }


    @Override
    public int getField() {
        return field;
    }

    // Class Unique Code...
}


class Class2 extends AClass {
    final int field;

    public Class2(int f) {
        field = f;
    }


    @Override
    public int getField() {
        return field;
    }

    // Class Unique Code...
}

Scenario 1 is shorter since the getter logic for field only needs to be specified once.场景 1 较短,因为field的 getter 逻辑只需要指定一次。 Whereas in scenario 2, the getter logic must be overridden by both subclasses.而在场景 2 中,getter 逻辑必须被两个子类覆盖。 I find scenario 2 to be redundant... why write the same code twice when you can use java inheritance to your advantage.我发现场景 2 是多余的......当您可以使用 java inheritance 对您有利时,为什么要编写两次相同的代码。

As a final note, I usually don't hold functions in fields unless totally necessary.最后一点,除非完全必要,否则我通常不会在字段中保存函数。 Whenever you have a function in a field, it's usually a sign that an abstract function can be applied.每当您在字段中有 function 时,通常表明可以应用抽象 function。

Here is your original code with my advice applied...这是您应用了我的建议的原始代码...

public abstract class MyAbstractClass<T> {
    private final String type;

    protected MyAbstractClass(String t) {
        type = t;
    }

    protected abstract T applyFactoryFunction(String value);

    public T doSomething(String value) { ... }
}

Hope this helped!希望这有帮助!

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

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