Chthonic Project
Chthonic Project

Reputation: 8346

Using interface with enums

I am coding a mini-project of sorts about medical information, and in it, I am reading text from several data sources. All these text sources have the same type of information, but under slightly different labels. For example, sourceA has a section titled "adverse effects" while sourceB calls it "side effects".

I have an interface called Reader, and several classes (AReader, BReader, etc.) implementing this interface. Also, I have enum for each class for the section titles. For example:

enum ASections {
    SIDE_EFFECTS ("side effects"),
    DOSAGE       ("dosage");
    private String sectionTitle;
    private ASections(String s) { this.sectionTitle = s; }
}

enum BSections {
    SIDE_EFFECTS ("adverse effects"),
    DOSAGE       ("dosage and usage");
    private String sectionTitle;
    private BSections(String s) { this.sectionTitle = s; }
}

At the core of my project lies an Orchestrator class, which uses a Reader (the actual source A, B, etc. are specified by command line options). So far so good.

However, I want all the classes implementing Reader to also implement a method getSectionText, where the argument should be ASections or BSections or ...

How do I specify such a method at the interface level?

This was my first (obviously wrong) attempt:

public String getSectionText(Enum _section_enum);

The idea is that no matter which data source is specified at command line, I should be able to get the required type of text by fetching the appropriate section title.

Upvotes: 1

Views: 170

Answers (2)

David Conrad
David Conrad

Reputation: 16359

Create an interface that defines the method your enums must implement:

interface Sections {
    String getSectionTitle();
}

In your enums, implement that interface:

enum ASections implements Sections {
    SIDE_EFFECTS ("side effects"),
    DOSAGE       ("dosage");
    private String sectionTitle;
    private ASections(String s) { this.sectionTitle = s; }
    public String getSectionTitle() { return sectionTitle; }
}

enum BSections implements Sections {
    SIDE_EFFECTS ("adverse effects"),
    DOSAGE       ("dosage and usage");
    private String sectionTitle;
    private BSections(String s) { this.sectionTitle = s; }
    public String getSectionTitle() { return sectionTitle; }
}

In your Reader, the getSectionText method takes a Sections parameter instead of any specific enum (code against the interface, not the implementation):

class Reader {
    public String getSectionText(Sections sections) {
        return sections.getSectionTitle();
    }
}

Then you can pass either an ASections or a BSections into the reader:

public class Section8 {
    public static void main(String[] args) {
        Reader reader = new Reader();
        for (ASections asection : ASections.values()) {
            System.out.println(reader.getSectionText(asection));
        }
        for (BSections bsection : BSections.values()) {
            System.out.println(reader.getSectionText(bsection));
        }
    }
}

Output:

side effects
dosage
adverse effects
dosage and usage

By the way, there is an error in your enums as defined in the question. The constructor is public, however in Java the constructor of an enum must be private.

Upvotes: 2

NimChimpsky
NimChimpsky

Reputation: 47280

just have the enum implement an interface, MySection, which specifies one method of getSectionText.

Upvotes: 0

Related Questions