Elegant way to implement abstract class

userit1985

I have an abstract class with a single abstract method; and a number of implementing classes (about 6).

The method returns an object that "needs" two parameters.

However, in some cases, only one of the two parameters is required.

Is there an elegant way to implement this case? (instead of return this parameter as empty)

public class NormResult {
    protected List<String> normWords;
    protected  List<String> unNormWords;

    public NormResult(List<String> normWords,List<String> unNormWords) {
        this.normWords = normWords;
        this.unNormWords = unNormWords;
    }

    public NormResult(List<String> normWords) {
        this.normWords = normWords;
        this.unNormWords =  Collections.emptyList();
    }
}

public abstract class AbstractNormalizer {
    protected abstract List<NormResult> doNorm();
}

public class FirstNormImpl extends AbstractNormalizer {
    protected List<NormResult> doNorm() {
        List<String> normWords = new ArrayList<>(5);
        List<String> unNormWords = new ArrayList<>(7);

        NormResult result = new NormResult(normWords, unNormWords);
        return result;      
    }
}

public class SecondNormImpl extends AbstractNormalizer {
    protected List<NormResult> doNorm() {
        List<String> normWords = new ArrayList<>(8);    
        NormResult result = new NormResult(normWords);
        return result;
    }
}
ΦXocę 웃 Пepeúpa ツ

if you do this to members final:

 protected final List<String> normWords;
 protected final List<String> unNormWords;

then in the constructor you have to initialize them both... then you can set to an empty collection or a null reference the one you dont have/need

and your overloaded constructor can look like:

public NormResult(List<String> normWords, List<String> unNormWords) {
    this.normWords = normWords;
    this.unNormWords = unNormWords;
}

public NormResult(List<String> normWords) {
    this(normWords, Collections.emptyList());
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implement IDisposable in abstract class

From Dev

Nested abstract class in an abstract class and how to implement it

From Dev

Is there a way to force a derived class to implement an abstract class or an interface nested in base class?

From Dev

Class must be declared as abstract or implement abstract method

From Dev

Elegant way to implement template method pattern in Golang

From Dev

How to implement an abstract class in Go?

From Dev

implement abstract adapter class in MainActivity

From Dev

Is there a way to specify or assert that the child class must re-implement a specific non-abstract virtual method?

From Dev

How to create Mock to a class that implement an abstract class

From Dev

Elegant way to create protected/abstract/etc methods in Objective-c

From Dev

Is there an elegant and pythonic way to require inputs for a class constructor?

From Dev

More elegant way to toggle a CSS class with jquery

From Dev

Matlab defining class functions in an elegant way for OOP

From Dev

elegant way to add css class in rails partials

From Dev

Abstract class implement a generic Interface Type

From Dev

How to implement generic fields from abstract class?

From Dev

implement nested abstract class with getter/setter

From Dev

parse anonymous class does not implement abstract method

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

Scala akka : implement abstract class with subtype parameter

From Dev

Java how to implement and design an abstract class

From Dev

How to implement methods of an abstract class? (Java)

From Dev

How to Implement Extension Method in Abstract Class

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

How to implement Abstract class in laravel 5.3

From Dev

How to implement operator+ in abstract base class

From Dev

Implement abstract method inside of scalatest class

From Dev

Class must either be declared abstract or implement abstract method

From Dev

Class must either be declared abstract or implement abstract method?

Related Related

  1. 1

    Implement IDisposable in abstract class

  2. 2

    Nested abstract class in an abstract class and how to implement it

  3. 3

    Is there a way to force a derived class to implement an abstract class or an interface nested in base class?

  4. 4

    Class must be declared as abstract or implement abstract method

  5. 5

    Elegant way to implement template method pattern in Golang

  6. 6

    How to implement an abstract class in Go?

  7. 7

    implement abstract adapter class in MainActivity

  8. 8

    Is there a way to specify or assert that the child class must re-implement a specific non-abstract virtual method?

  9. 9

    How to create Mock to a class that implement an abstract class

  10. 10

    Elegant way to create protected/abstract/etc methods in Objective-c

  11. 11

    Is there an elegant and pythonic way to require inputs for a class constructor?

  12. 12

    More elegant way to toggle a CSS class with jquery

  13. 13

    Matlab defining class functions in an elegant way for OOP

  14. 14

    elegant way to add css class in rails partials

  15. 15

    Abstract class implement a generic Interface Type

  16. 16

    How to implement generic fields from abstract class?

  17. 17

    implement nested abstract class with getter/setter

  18. 18

    parse anonymous class does not implement abstract method

  19. 19

    How to implement Interface to an abstract class which is constraint?

  20. 20

    Scala akka : implement abstract class with subtype parameter

  21. 21

    Java how to implement and design an abstract class

  22. 22

    How to implement methods of an abstract class? (Java)

  23. 23

    How to Implement Extension Method in Abstract Class

  24. 24

    How to implement Interface to an abstract class which is constraint?

  25. 25

    How to implement Abstract class in laravel 5.3

  26. 26

    How to implement operator+ in abstract base class

  27. 27

    Implement abstract method inside of scalatest class

  28. 28

    Class must either be declared abstract or implement abstract method

  29. 29

    Class must either be declared abstract or implement abstract method?

HotTag

Archive