Static Factory: Java Design Problem

I have a Base interface that defines some common functions. Now this basic interface can be implemented by more than 50 classes, each of which has several different additional methods. I want to have a static factory that will return an instance of any of the 50 classes depending on the transition of the parameter to this static factory method.

public interface Base {
     public void a();
     public void b();
}

public class myclass implements Base {
     //   a and b implementation

     public String c() {
     }
     public String d() {
     }
}

public class secondclass implements Base {
     //   a and b implementation

     public String e() {
     }
     public String f() {
     }
}

How can I implement a static factory method. I'm not sure about the return type

  public synchronized static {return type} getInstance(String arg0) {
         // do something and return any one class based on arg0
  }

edits: Script

If the passed parameter is 300 I want to return a class object myclass

If the parameter is 900 I want to return a class object secondclass

Etc. since so many conditions are not possible.

NOT TO DO

public synchronized static {return type} getInstance(String arg0) {
   // do something and return any one class based on arg0
   if(arg0.equals("300")) {
       return new myclass();

   }

}

: . API, 100 500. , .

UPDATE: ,

public abstract class Base {

      public abstract void a();
      public abstract void b();

      public synchronized staticabstract Base getInstance(String arg0);

}


public class myclass extends Base {

     private myclass() {}    // private constructor not publicly instantiate 
     // a and b implementation

     public void c();
     public void d();

     @Override
     public synchronized static abstract Base getInstance(String arg0) {
           if(arg0.equalsIgnoreCase("300")) {      // only 1 if condition
                return new myclass();
           }
     }
}



public class secondclass extends Base {

     private secondclass() {}    // private constructor not publicly instantiate 
     // a and b implementation

     public void e();
     public void f();

     @Override
     public synchronized static abstract Base getInstance(String arg0) {
           if(arg0.equalsIgnoreCase("900")) {      // only 1 if condition
                return new secondclass();
           }
     }
}



Client side:
one applicaiton
Base b=Base.getInstance(300);
if(b instanceof myclass) {

}

second application

Base b=Base.getInstance(900);
if(b instanceof secondclass) {

}
+4
3

:

static <T extends Base> T create(Class<T> type){
  // create concrete instance of type T and return it
}

create Class. type , if.

, , , .

EDIT: , no-arg , :

return type.newInstance();

EDIT 2. , factory , , :

static Base create(String someParameter){
  // create the concrete class and return it
}

:

Base b = create("myArgument");
if(b instanceof SomeDerived){
  SomeDerived d = (SomeDerived) b;
  // use d
} else if(b instanceof OtherDerived){
  // you get the idea
}

, create Class<? extends Base>.

+4

factory ( Java):

factory , , ,

, , - :

Base x = Factory.getInstance(TypeX.class); 
x.methodOfBase();

TypeX .

, API java.util.Collections, , factory:

public static <T> List<T> synchronizedList(List<T> list)

, - , ( List) .

, x1(), x2(), TypeX Base, TypeX.
TypeX ( ), factory .

3 static factory constructors , , .

+3

.

:

public synchronized static Base getInstance(String arg0) {
    if(arg0.equals("myClass"))
        return new myClass();
    else if (arg0.equals("secondClass"))
        return new secondClass();
}

:

Base instance = getInstance("myClass");
((myClass) instance).c();

:

myClass instance = (myClass) getInstance("myClass");
instance.c();
0

Source: https://habr.com/ru/post/1534708/


All Articles