我有一堂课:
public class VisitorProcessing {
public void visit(EventA eventA){
if(condition1....)
// Do somethings 1
else{
if(condition2){
// Do something 2
}
else{
// Do something 3
}
}
}
public void visit(EventB eventB){
if(condition1....)
// Do somethings 4
else{
if(condition2){
// Do something 5
}
else{
// Do something 6
}
}
}
public void visit(EventC eventC){
if(condition1....)
// Do somethings 7
else{
if(condition2){
// Do something 8
}
else{
// Do something 9
}
}
}
public void visit(EventD eventD){
if(condition1....)
// Do somethings 10
else{
if(condition2){
// Do something 11
}
else{
// Do something 12
}
}
}
}
所有事件对象都扩展相同的父对象BasicEvent。条件仅指事件对象,可以从父事件计算得出。
我想开始进行重构以将分支的逻辑集中在一个地方,因为我不确定条件。我唯一确定的是处理“ DoSomthings ...”。
所以我正在搜索是否有任何已知的模式可以做到这一点。
提前致谢
调度方法负责方法的正确调用,您每次只需要编写3个方法即可。
public interface DoSomething {
void doSomething1();
void doSomething2();
void doSomething3();
}
public class VisitorProcessing {
public void dispatch( DoSomething ds) {
if(condition1....)
ds.doSomething1();
else{
if(condition2){
ds.doSomething2();
}
else{
ds.doSomething3();
}
}
}
public void visit(EventA eventA){
DoSomething ds = new DoSomething()
{
void doSomething1() {
// Do somethings 1
}
void doSomething2(){
// Do something 2
}
void doSomething3(){
// Do something 3
}
}
dispatch( ds );
}
public void visit(EventB eventB){
DoSomething ds = new DoSomething()
{
void doSomething1() {
// Do somethings 3
}
void doSomething2(){
// Do something 4
}
void doSomething3(){
// Do something 5
}
}
dispatch( ds );
}
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句