Java 避免重复处理块的模式
我有一门课:Java 避免重复处理块的模式,java,oop,design-patterns,Java,Oop,Design Patterns,我有一门课: public class VisitorProcessing { public void visit(EventA eventA){ if(condition1....) // Do somethings 1 else{ if(condition2){ // Do something 2 }
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…”
所以我在寻找是否有任何已知的模式可以这样做
提前感谢如果无法更改访问方法,则可以创建一个流程(BasicEvent事件)方法,该方法将从每个访问方法中调用
public class VisitorProcessing {
public void visit(EventA eventA){
processEvent(eventA);
//... EventA specific code goes here
}
public void visit(EventB eventB){
processEvent(eventB);
//... EventB specific code goes here
}
public void visit(EventC eventC){
processEvent(eventC);
//... EventC specific code goes here
}
public void visit(EventD eventD){
processEvent(eventD);
//... EventD specific code goes here
}
private void processEvent(BasicEvent event) {
if(condition1....)
// Do somethings 10
else{
if(condition2){
// Do something 11
}
else{
// Do something 12
}
}
}
}
dispatch方法负责正确调用方法,每次只需编写3个methid
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 );
}
...
}
你可能想看看这张照片。 本质上,您有一个类
Handler
,它根据一个决策进行操作,并且包含一个Handler
类型的对象,当条件不满足时可以委托该对象。例如:
public Class Handler() {
private Handler next;
public setNext(Handler next) {
this.next = next;
}
public void action(params) {
if(some_condition) {
...
}
else {
if(next != null)
next.action(params);
}
}
}
当然,该类可以而且应该扩展以创建不同类型的处理程序。在您的案例中,此模式的优点是,您可以在相同的条件下使用相同的链,并根据调用visit
方法的情况改变操作。此外,您可以非常轻松地添加、编辑和删除条件,甚至在运行时修改链
就你而言:
public Class Condition1 extends Handler {
public void action(BasicEvent e) {
if (condition1) {
if(e instanceof EventA) // Do something 1
if(e instanceof EventB) // Do something 4
if(e instanceof EventC) // Do something 7
if(e instanceof EventD) // Do something 10
}
else {
if(next != null)
next.action(BasicEvent e);
}
}
}
public Class Condition2 extends Handler {
public void action(BasicEvent e) {
if (condition2) {
if(e instanceof EventA) // Do something 2
if(e instanceof EventB) // Do something 5
if(e instanceof EventC) // Do something 8
if(e instanceof EventD) // Do something 11
}
else {
if(next != null)
next.action(BasicEvent e);
}
}
}
public Class ConditionElse extends Handler {
public void action(BasicEvent e) {
if(e instanceof EventA) // Do something 3
if(e instanceof EventB) // Do something 6
if(e instanceof EventC) // Do something 9
if(e instanceof EventD) // Do something 12
// we reached the end of the chain
}
}
为其他条件创建类似的类(如果您有),然后形成链:
Condition1 condition_1 = new Condition1();
Condition2 condition_2 = new Condition2();
ConditionElse condition_else = new new ConditionElse();
condition_1.setNext(condition_2);
condition_2.setNext(condition_else);
然后您只需要一个访问方法:
public void visit(BasicEvent e){
condition1.action(e);
}
我希望我给了你一个适合你的案例的模式的快速概述。可以用更好的方法处理部分的实例,特别是如果您有4个以上的子类,那么重构是值得的,但我希望您能理解要点。这些是“做点什么”吗是的,条件相同,我的事件对象继承自同一对象(基本事件),具体在conditions@Nabil因此,条件1对于每种就诊方法都是不同的?不,条件是相同的,但是“做一些事情”是具体的我喜欢你的模式,我会测试它。我看到的唯一一点是事件和DoSomthing是手动关联的。我们需要说EventA==>doSomThinga,但这没什么大不了的,谢谢你的评论,很好的洞察力,现在我不需要所有这些灵活性(条件setNext)实例是真正的痛苦,这就是我们实现访问者模式的原因。但对于未来,如果我需要改变条件,我将测试责任链模式。我同意StephaneM的答案更适合您的需要。有了这个解决方案,添加和删除类不是问题。对于我的,添加和删除c条件可能会更快。在某个时候,你必须决定你想把困难的部分放在哪里,我认为在这种情况下,接受答案中的调度员是最好的选择。实际上,可能有一种方法可以将两者结合起来,我可能会在不久的将来发布。