我正在做一个由客户端 - 服务器游戏组成的学校项目。
可以像示例1 和示例2 中所示编写Java代码:
在示例2 中,我看到通过创建3个私有方法并在构造函数中调用它们来缩短构造函数的可能性(IMHO看起来更整洁)。
哪个示例是Java中的最佳实践/推荐?
示例1 :
public ServerConnection()
{
private Socket serverSocket = null;
private SSLSocket sslServerSocket = null;
private DataOutputStream dos;
private DataInputStream dis;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private DataOutputStream ssldos;
private DataInputStream ssldis;
private ObjectOutputStream ssloos;
private ObjectInputStream sslois;
try
{
serverSocket = new Socket("localhost", 9000);
String password = "password";
KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(ClassLoader.getSystemResourceAsStream("SSL/mykey.jks"), password.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustManagerFactory.init(trustStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLSocketFactory sslSocketFactory = (SSLSocketFactory)context.getSocketFactory();
sslServerSocket = (SSLSocket) sslSocketFactory.createSocket("localhost", 9001);
sslServerSocket.startHandshake();
dos = new DataOutputStream(serverSocket.getOutputStream());
dis = new DataInputStream(serverSocket.getInputStream());
oos = new ObjectOutputStream(serverSocket.getOutputStream());
ois = new ObjectInputStream(serverSocket.getInputStream());
ssldos = new DataOutputStream(sslServerSocket.getOutputStream());
ssldis = new DataInputStream(sslServerSocket.getInputStream());
ssloos = new ObjectOutputStream(sslServerSocket.getOutputStream());
sslois = new ObjectInputStream(sslServerSocket.getInputStream());
}
catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException | KeyManagementException e)
{
System.out.println("ServerConnection 2 " + e.getMessage());
}
}
示例2 :
public ServerConnection()
{
private Socket serverSocket = null;
private SSLSocket sslServerSocket = null;
private DataOutputStream dos;
private DataInputStream dis;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private DataOutputStream ssldos;
private DataInputStream ssldis;
private ObjectOutputStream ssloos;
private ObjectInputStream sslois;
try
{
serverSocket = new Socket("localhost", 9000);
keyMethod();
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLSocketFactory sslSocketFactory = (SSLSocketFactory)context.getSocketFactory();
sslServerSocket = (SSLSocket) sslSocketFactory.createSocket("localhost", 9001);
sslServerSocket.startHandshake();
setStreams();
setSSLStreams();
}
catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException | KeyManagementException e)
{
System.out.println("ServerConnection 2 " + e.getMessage());
}
private void keyMethod()
{
String password = "password";
KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(ClassLoader.getSystemResourceAsStream("SSL/mykey.jks"), password.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustManagerFactory.init(trustStore);
}
private void setStreams()
{
dos = new DataOutputStream(serverSocket.getOutputStream());
dis = new DataInputStream(serverSocket.getInputStream());
oos = new ObjectOutputStream(serverSocket.getOutputStream());
ois = new ObjectInputStream(serverSocket.getInputStream());
}
private void setSSLStreams()
{
ssldos = new DataOutputStream(sslServerSocket.getOutputStream());
ssldis = new DataInputStream(sslServerSocket.getInputStream());
ssloos = new ObjectOutputStream(sslServerSocket.getOutputStream());
sslois = new ObjectInputStream(sslServerSocket.getInputStream());
}
}
答案 0 :(得分:2)
示例2是您最好的选择,因为它遵循最佳实践指南,更易于阅读,反过来又使您的代码更清晰。将代码片段转换为名称解释方法目的的方法也是理想的。有几个原因可以让你想要简短明了的方法:
首先,它增加了其他方法在方法细粒度时可以使用方法的机会。
其次,它允许更高级别的方法更像是一系列注释。当方法细化时,覆盖也会更容易。
以Martin Fowler在博客上提供的例子为例:
碎片代码:
void printOwing() {
printBanner();
//print details
System.out.println ("name: " + _name);
System.out.println ("amount " + getOutstanding());
}
重构代码:
void printOwing() {
printBanner();
printDetails(getOutstanding());
}
void printDetails (double outstanding) {
System.out.println ("name: " + _name);
System.out.println ("amount " + outstanding);
}
这也有助于其他可能查看您的代码的人快速了解正在发生的事情。
您可以在Refactoring了解更多福勒先生的博客。 另请参阅SourceMaking博客,了解您应该提取方法的原因。
答案 1 :(得分:0)
通常,除非有正当理由,否则指南是尝试拆分任何超过100 LOC的功能。有理由的案例包括
等