我有一段代码将事件写入日志文件:
Date rightNow = new Date();
File logfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), ("MyLogfile" + fileSDF.format(rightNow) + ".txt"));
FileOutputStream fos;
boolean documents_directory_exists = logfile.getParentFile().exists();
boolean documents_directory_created = true;
if(!documents_directory_exists) documents_directory_created = logfile.getParentFile().mkdirs();
if(documents_directory_created){
try {
fos = new FileOutputStream(logfile, true);
fos.write(new LogEntry(timestampSDF.format(rightNow), boolean01, someInt, boolean02).toString().getBytes());
fos.close();
} catch (IOException ioe) {
Log.e(SomeClass.class.getName(), String.format(Locale.US, "%s %s", Constants.DEFAULT_FILE_ERROR_MESSAGE, ioe.getMessage()));
}
} else {
Log.e(SomeClass.class.getName(), String.format(Locale.US, "%s %s", Constants.DEFAULT_FILE_ERROR_MESSAGE, "Cannot create the necessary directories."));
}
我把这个代码放在多个地方,所以我想我应该把它放在一个界面上,这样我的代码就更干净了。所以我创建了这个界面:
public interface LogWriter {
void writeLog(LogEntry logEntry);
}
其中LogEntry是:
public class LogEntry{
private String timestamp;
private boolean booean01;
private int someInt;
private boolean boolean02;
public LogEntry(timestamp, boolean01, someInt, boolean02){
this.timestamp = timestamp;
this.boolean01= boolean01;
this.someInt= someInt;
this.boolean02= boolean02;
}
// Getters and Setters
}
public interface LogWriter {
void writeLog(LogEntry logEntry);
class WriteMeToTheLog {
LogEntry logEntry;
private static final SimpleDateFormat fileSDF = new SimpleDateFormat(Constants.ACCESS_LOGFILE_NAME_FORMAT);
public WriteMeToTheLog(LogEntry logEntry) {
this.logEntry = logEntry;
}
public void write(){
Date rightNow = new Date();
File logfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), ("MyLogfile" + fileSDF.format(rightNow) + ".txt"));
FileOutputStream fos;
boolean documents_directory_exists = logfile.getParentFile().exists();
boolean documents_directory_created = true;
if(!documents_directory_exists) documents_directory_created = logfile.getParentFile().mkdirs();
if(documents_directory_created){
try {
fos = new FileOutputStream(logfile, true);
fos.write(new LogEntry(timestampSDF.format(rightNow), boolean01, someInt, boolean02).toString().getBytes());
fos.close();
} catch (IOException ioe) {
Log.e(SomeClass.class.getName(), String.format(Locale.US, "%s %s", Constants.DEFAULT_FILE_ERROR_MESSAGE, ioe.getMessage()));
}
} else {
Log.e(SomeClass.class.getName(), String.format(Locale.US, "%s %s", Constants.DEFAULT_FILE_ERROR_MESSAGE, "Cannot create the necessary directories."));
}
}
}
}
这就是我要去的地方
迷路的。
在我拥有原始代码块的一个类中,我实现了这个新接口:
public class OneOfMyClasses extends BaseClass implements LogWriter {
public myMethod(){
// This is where I had the original block of code
// WHAT DO I DO HERE NOW???
}
@Override
public void writeLog(){
Date rightNow = new Date();
writeMeToTheLog(new LogEntry(timestampSDF.format(rightNow), boolean01, someInt, boolean02).toString().getBytes());
writeMeToTheLog.write();
}
}
如何使用此新功能?