如何使用内部类正确实现Java接口?

时间:2018-09-10 17:05:30

标签: java interface

我有一段代码将事件写入日志文件:

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
}

我想保持代码的整洁,所以我想在界面中进行所有文件I / O,所以我创建了一个内部类:

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();
    }
}

如何使用此新功能?

1 个答案:

答案 0 :(得分:0)

  

我想保持我的代码非常干净,所以我想做所有文件   接口中的I / O,所以我创建了一个内部类

不必在接口中声明类“干净”。
此外,这不是内部类,而是静态类,因为该类在接口中声明。
所有这些都是相对直观的。该类是一个实现,而接口是一个API。 API声明实现结构听起来并不好。

关于您的问题,我认为WriteMeToTheLog(包含您提取的日志记录逻辑)还必须实现LogWriter,因为它看起来像是LogWriter的实现。
客户端类应该具有对WriteMeToTheLog的依赖关系,可能是在构造函数中设置的字段,尽管它仍然可以实现LogWritter(如果有意义)。

那会给:

class WriteMeToTheLog implements LogWriter { ...}

然后:

public class OneOfMyClasses extends BaseClass implements LogWriter {
    private LogWriter logWriter;

    public OneOfMyClasses (LogWriter logWriter){
       this.logWriter = logWriter;
    }

    @Override
    public void writeLog(){
        Date rightNow = new Date();       
        logWriter.write();
    }
}

现在您可以通过设置依赖项来实例化客户端类:

OneOfMyClasses o = new OneOfMyClasses(new WriteMeToTheLog(new LogEntry(timestampSDF.format(rightNow), boolean01, someInt, boolean02).toString().getBytes());