【发布时间】:2014-01-04 01:24:15
【问题描述】:
我正在阅读一些文本文件来创建对象。哪个类应该根据 OOP 原则处理文本文件?
我的 GUI 对象有一个方法来绘制表格并用数据填充它。此数据要么从 HTML 页面解析,要么从缓存的文本文件中读取。我可以想到两种方法来处理这个问题,我想知道哪种方法更好。
选项 1:
public void drawSchedule()
{
try
{
if (CacheManager.hasData("schedule")) //this is not the complete logic, but enough for this post
{
String cacheString = CacheManager.readData(this, "schedule");
Schedule schedule = new Schedule(cacheString);
}
else
{
//read data from HTML page
}
catch (IOException e)
{
//generic error handling
e.printStackTrace();
}
}
选项 2:
public void drawSchedule()
{
try
{
if (CacheManager.hasData("schedule")) //this is not the complete logic, but enough for this post
{
String cacheString = CacheManager.readData(this, "schedule");
//parse data here so we end up with a bunch of variables
//courseList would be an ArrayList of Courses, if it makes any difference
Schedule schedule = new Schedule(firstDay, courseList);
}
else
{
//Read data from HTML page
}
catch (IOException e)
{
//generic error handling
e.printStackTrace();
}
}
【问题讨论】:
-
parsing在选项 1 中的表现如何? -
@PM77-1 在选项 1 中,
Schedule的构造函数将解析字符串。 -
在不知道细节的情况下,我只从封装(和解耦)的角度来选择 选项 1。