thelost
thelost

Reputation: 6694

Factory for helper classes with static methods?

I have a couple of helper classes (? extends HelperBase) which only have public static methods. I would like to have something similar to what a factory is (with singleton support), but as there is no need for an instance, I am unsure which way to go best.

In the end, I would like to have something like:

enum HELPER_TYPE {
    Type_A
}

abstract class HelperBase {
    abstract void do();

    static HelperBase getHelper(HELPER_TYPE type) {
    // ...
    }
}

class Helper1 extends HelperBase {
    static void doImpl() {
    // ... impl here
    }

    void do() {
        doImpl();
    }
}

// ... and then:

HelperBase helper = HelperBase.getHelper(HELPER_TYPE.Type_A);
helper.do();

Is there a better way ? Any suggestion would be appreciated.

Upvotes: 1

Views: 637

Answers (1)

Narendra Pathai
Narendra Pathai

Reputation: 41955

enum HELPER_TYPE implements IHelper{
    Type_A{
        @Override
        public void doSomething(){

        }
    },
    Type_B{
       @Override
       public void doSomething(){
       }
    }
}

interface IHelper{
    public void doSomething();
}

How about having an interface with required methods and having enum implement that?

Now you have polymorphic behavior among all helpers implementing IHelper interface.

So you can call them like HELPER_TYPE.Type_A.doSomething()

Upvotes: 1

Related Questions