简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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