![](/img/trans.png)
[英]buffered reader retrieves no values and premature end of file in a slightly different structured XML from same server
[英]Same Method for Client and Server slightly different behavior
如果我的大多数类是客户端或服务器端,它们的行为都不同。 (客户端:gui,服务器:连接的东西),但是它们也有一些常见的行为。 我想知道什么是最好的方法。
类的示例内容:
public class Example{
private void commonMethod(){
//common code
}
public void clientMethod(){
commonMethod()
//Client code
}
public void serverMethod(){
commonMethod()
//Server code
}
}
我想要的是:
允许什么:
我要避免的是:
我在想的事情:
我的类是通过api加载的,示例客户端/服务器方法将是init。 所以在我的主类中,我需要为所有需要初始化的类运行该方法。
如果我是您(并且如果我了解您的需求),那么我将使用由客户端和服务器类实现的通用接口,中间是一个抽象类:
public interface Example {
public void method();
}
public abstract class AbstractExample implements Example {
@Override
public void method() {
common();
implMethod();
}
private void common() {
// common implementation
}
protected abstract void implMethod();
}
public class ExampleClientImpl extends AbstractExample {
@Override
protected void implMethod() {
// client implementation
}
}
public class ExampleServerImpl extends AbstractExample {
@Override
protected void implMethod() {
// server implementation
}
}
使用这种方法,您还可以将类拆分为通用/客户端/服务器包或更好的模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.