Athanasios Kataras
Athanasios Kataras

Reputation: 26450

Patterns to instantiate abstract class?

I have an abtract class lets call it ClassAbstract.

I also have a number of concrete classes Class#NonAsbtract extends ClassAbstract.

Consider the following piece of code:

public void someMethod(some arguments) {
    // my solution : ClassAbstract ca = buildClassAbstract(enumArgument);
    ClassAbstract ca = new Class#NonAsbtract();
}

Right now I am using this method in the same class with someMethod:

private ClassAbstract buildClassAbstract(SomeEnum enum) {
    switch(enum) {
         case ARG_ONE:
              return new Class1NonAbstract();
    }
}

Do you think it's worth it to go to any pattern

Upvotes: 0

Views: 101

Answers (1)

bigGuy
bigGuy

Reputation: 1744

You are looking for Factory pattern. You almost did it, just create factory class and move your buildClassAbstract method to it.

Upvotes: 2

Related Questions