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 {
public String c() {
}
public String d() {
}
}
public class secondclass implements Base {
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() {}
public void c();
public void d();
@Override
public synchronized static abstract Base getInstance(String arg0) {
if(arg0.equalsIgnoreCase("300")) {
return new myclass();
}
}
}
public class secondclass extends Base {
private secondclass() {}
public void e();
public void f();
@Override
public synchronized static abstract Base getInstance(String arg0) {
if(arg0.equalsIgnoreCase("900")) {
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) {
}