简体   繁体   English

什么是正确的模式?

[英]What is the right pattern?

What is the correct way to resolve: 解决的正确方法是什么:

Class A {

methodDoSomeOperation() {

  ClassB b.someOperation(.....);
....

 }


ClassB {

someOperation(....) {


 if (this.someCondition) then{
   ClassC c.anotherOperation(....)
  } else {
 ClassD d....
 }
}

 }

Methods c.anotherOperation takes a long time. 方法c.anotherOperation需要很长时间。 I would like receive notification regarding what was selected: c.anotherOperation(....) or d..... for display in a user interface before invoking c.anotherOperation . 我想收到有关所选内容的通知: c.anotherOperation(....)d.....在调用c.anotherOperation之前在用户界面中显示。

Something like: 就像是:

someOperation(....) {


if (this.someCondition) then{
 //notify ClassA what was selected and continue
 ClassC c.anotherOperation(....)
 } else {
 ClassD d....
 }
}

Obviously it is possible to invoke some method in ClassA but this creates strong coupling and confusing logic. 显然,可以在ClassA调用某些方法,但这会产生强耦合和混淆逻辑。 Is it possible use some features of Spring? 有可能使用Spring的一些功能吗?

Could it be that your looking for a Decorator Pattern ? 难道你正在寻找装饰模式吗? This would apply if each class added/aggregated their part. 如果每个类添加/聚合它们的部分,这将适用。

Or even a Chain of Responsibility ? 甚至是责任链 This may be more applicable since each class would optionally do their processing. 这可能更适用,因为每个类都可以选择进行处理。 Assuming Classes A, B, and C are Processing objects, they would have logic defining what sorts of operations they would work on, thus optionally performing their particular processing, and then passing on to the next processing object. 假设类A,B和C是处理对象,它们将具有定义它们将要处理的操作类型的逻辑,从而可选地执行它们的特定处理,然后传递给下一个处理对象。 Considering that you seem to be chaining A, B, and C together, each performing their part, this appears to apply best to your description. 考虑到您似乎将A,B和C链接在一起,每个人都表现出自己的作用,这似乎最适用于您的描述。 Then to get an indication of which Processing objects did their part, you could define a Context and pass it along the chain, and each Object would fill in their part. 然后,为了获得哪些Processing对象发挥作用,您可以定义一个Context并将其传递给链,每个Object都会填充它们的一部分。

Would you consider some form of the Observer pattern? 你会考虑某种形式的观察者模式吗? Whatever the class that is closer to the UI(May be class A?) can be registered as an observer to the Class B. this can be done upfront or as part of the call to the B.SomeOperation() by adding a parameter. 无论哪个类靠近UI(可能是A类?)都可以注册为B类的观察者。这可以通过添加参数来预先完成或作为对B.SomeOperation()的调用的一部分。 Then when you do the check B.someCondition, you can call the registered observer an then call the long running operation on C. 然后当你检查B.someCondition时,你可以调用注册的观察者然后在C上调用长时间运行的操作。

Do you want to give the user the opportunity to cancel c.anotherOperation() before it starts? 你想让用户在启动前取消c.anotherOperation()吗? If so then someOperation() doesn't actually call it. 如果是这样,那么someOperation()实际上并没有调用它。 Instead it causes an ok/cancel dialog to be displayed, and the ok choice is treated by the UI as a command that leads to c.anotherOperation(). 相反,它会导致显示ok / cancel对话框,并且UI将ok选项视为导致c.anotherOperation()的命令。 (Maybe by way of ClassA and ClassB again, depending on what their responsibilities are.) (也许再次通过ClassA和ClassB,取决于他们的职责。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM