[英]How can I refactor this code and apply OO patterns?
I have four RichTable
instances in my class and there is a notion of current
table instance . 我的
RichTable
中有四个RichTable
实例,并且有一个current
表实例的概念。 Depending on a flag resetAll
I need to clear out selections of either all the tables or all tables except the current one . 取决于标志
resetAll
我需要清除所有表或除当前表以外的所有表的选择 。 If resetAll
is true then clear out everything , otherwise leave out the current one . 如果
resetAll
为true,则清除所有内容,否则忽略当前内容。 The index of the current table is passed as a parameter to the method that does the clean up action. 当前表的索引作为参数传递给执行清除操作的方法。
The call for clearing out everything looks like this : 清除所有内容的调用看起来像这样:
clearSubTypeSettings(true,-1);
The call for clearing all but the current one looks like this : 清除当前请求以外的所有请求的调用看起来像这样:
clearSubTypeSettings(true, col);
The implementation of the above method is this : 上面方法的实现是这样的:
private void clearSubTypeSettings(boolean resetAll, int exceptControl) {
if (!resetAll) {
clearAllExceptCurrent(exceptControl);
} else {
clearAll();
}
}
Now these two methods clearAllExceptCurrent(exceptControl)
and clearAll()
look almost the same . 现在,这两种方法
clearAllExceptCurrent(exceptControl)
和clearAll()
看起来几乎相同。 Here are the implementations : 这里是实现:
private void clearAll() {
for (int i = 0; i < SUBTYPE_TABLES; i++)
if (getSubTypeTable(i).getSelectedRowKeys() != null) {
RichTable richTable = getSubTypeTable(i);
RowKeySet rowkeySet = richTable.getSelectedRowKeys();
rowkeySet.clear();
AdfFacesContext.getCurrentInstance().addPartialTarget(richTable);
}
}
And 和
private void clearAllExceptCurrent(int exceptControl) {
for (int i = 0; i < SUBTYPE_TABLES; i++)
if (i != exceptControl && getSubTypeTable(i).getSelectedRowKeys() != null) {
RichTable richTable = getSubTypeTable(i);
RowKeySet rowkeySet = richTable.getSelectedRowKeys();
rowkeySet.clear();
AdfFacesContext.getCurrentInstance().addPartialTarget(richTable);
}
}
I feel like I am writing duplicate redundant code here and will complicate maintenance in future . 我觉得我在这里编写重复的冗余代码,将来会使维护复杂化。 How can I improve this code and make it more object oriented ?
如何改善此代码并使它更面向对象?
You can let clearAll()
delegate (=> OOP pattern) to clearAllExceptCurrent()
(=> improve code by removing duplicated code, make it more maintainable): 您可以让
clearAll()
委托 (=> OOP模式)到clearAllExceptCurrent()
(=>通过删除重复的代码来改进代码,使其更易于维护):
private void clearAll() {
clearAllExceptCurrent(-1);
}
The only difference between your two methods is the condition i != exceptControl
in clearAllExceptCurrent()
. 两种方法之间的唯一区别是
clearAllExceptCurrent()
的条件i != exceptControl
。 By passing -1
this condition is always true
and therefore effectively non-existent. 通过传递
-1
此条件始终为true
,因此实际上不存在。
The bulk of the repeated code is the bit that clears the table. 重复代码的大部分是清除表的位。 So how about:
那么如何:
private void clearTable(int id) {
if (getSubTypeTable(i).getSelectedRowKeys() != null) {
RichTable richTable = getSubTypeTable(i);
RowKeySet rowkeySet = richTable.getSelectedRowKeys();
rowkeySet.clear();
AdfFacesContext.getCurrentInstance().addPartialTarget(richTable);
}
}
Then: 然后:
private void clearAll() {
for (int i = 0; i < SUBTYPE_TABLES; i++) {
clearTable(i);
}
}
private void clearAllExceptCurrent(int exceptControl) {
for (int i = 0; i < SUBTYPE_TABLES; i++) {
if (i != exceptControl) {
clearTable(i)
}
}
}
EDIT: Moved if
statement inside clearTable
编辑:感动
if
语句中clearTable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.