将程序尽量写的优雅,我相信是每个程序员的追求。但是什么才是优雅的程序?这就是仁者见仁了。
举个例子:
public class Test {
private static Test mTest;
private Test(){
}
public static Test getInstance(){
if (mTest == null){
mTest = new Test();
}
return mTest;
}
private Map<String, String> funA(){
Map<String,String> map = new HashMap<>();
//// TODO: 16/1/29 your code
return map;
}
private void funB(){
//// TODO: 16/1/29 your code
Map b = funA();
}
}
public class Test {
private static Test mTest;
private Map mMap;
private Test(){
}
public static Test getInstance(){
if (mTest == null){
mTest = new Test();
}
return mTest;
}
private void funA(){
mMap = new HashMap<>();
//// TODO: 16/1/29 your code
}
private void funB(){
//// TODO: 16/1/29 your code
Map b = mMap;
}
}
我这有两个问题:
①单例类是否需要尽量避免使用成员变量,比如Test2里的mMap(单例类里的成员变量会在程序周期内一直被持有)
②Test跟Test2哪个更优雅?