我有以下设计要求:
interface Server {}public class ServerImpl implements Server {}, 必须是单例public class ServerABC extends ServerImpl {}public class ServerXYZ extends ServerImpl {}
现在我想将方法放在类中,Server它们的实现应该由ServerABCor给出ServerXYZ。
在ServerImpl
public class ServerImpl implements Server {
private static Server server;
public static synchronized RESTClient getInstance() {
if (server == null) {
server = new ServerImpl ();
}
return server;
}
}
我怎样才能使这项工作?
如果我将方法放在Server接口中,ServerImpl则必须实现它,但我希望子类(ServerABC和ServerXYZ)提供实现。应该使用 ServerImpl 对象调用这些方法。