我有一个执行文件读取的常用方法。该方法由4个不同的呼叫者调用。
如果文件大小超过一定数量的条目,则要求基于调用者抛出不同的错误消息。我能想到的一种方法是从调用者那里获取超出大小限制的处理程序。
如下所示,
public void readFromFile(File file,OnEntriesExceed onEntriesExceed)
//Method read a file line by line{
if(the number of lines exceeds )//read line by line
OnEntriesExceed.handle(totalNumberOfLines);
//calling the specific handler.
现在调用者可以调用以下方法
来电者1:
readFromFile(f1,(totalNumberOfLines)-> {throw new RuntimeException("entries limit exceeded in some case ")})
来电者:
readFromFile(f2,(totalNumberOfLines)-> {throw new RuntimeException("entries limit exceeded in some other case "});
来电者3:
readFromFile(f3,(totalNumberOfLines)-> {throw new RuntimeException("entries limit exceeded in some case 1")})
来电者4:
readFromFile(f4,(totalNumberOfLines)-> {throw new RuntimeException("entries limit exceeded in some other case 2"});
在这种情况下,我是否可以在java中实现其他最佳实践?