是否可以在代码库中嵌入Cockburn样式的文本UML用例内容以提高代码可读性?

foo*_*mes 6 uml coding-style readability use-case

在代码中试验Cockburn用例

我正在编写一些复杂的UI代码.我决定使用带有鱼,风筝和海平面的Cockburn用例(Martin Fowler在他的书"UML Distilled"中讨论过).我将Cockburn用例包装在静态C#对象中,以便我可以针对静态常量测试逻辑条件,静态常量表示UI工作流程中的步骤.我的想法是你可以阅读代码并知道它在做什么,因为包装的对象及其公共内容通过命名空间为你提供了ENGLISH用例.

此外,我将使用反射来抽出包含所描述的用例的错误消息.这个想法是堆栈跟踪可以包括一些用例步骤IN ENGLISH ....事实证明这是一种有趣的方式来实现迷你,伪造轻量级域语言,但无需编写DSL编译器.所以我的问题是这是否是一个很好的方法呢?有没有人在那里做过类似的事情?


c#示例片段如下

假设我们有一些aspx页面,它有3个用户控件(有很多可点击的东西).用户必须点击一个特定用户控件中的内容(可能进行某种选择),然后UI必须在视觉上提示用户选择成功.现在,在选择该项目时,用户必须浏览网格视图以在其中一个用户控件中查找项目,然后选择一些内容.这听起来很容易管理,但代码可能变得丑陋.

在我的例子中,用户控制主页捕获的所有已发送事件消息.这样,页面就像UI事件的中央处理器一样,可以跟踪用户点击时发生的情况.

因此,在主aspx页面中,我们捕获第一个用户控件的事件.

using MyCompany.MyApp.Web.UseCases;   

protected void MyFirstUserControl_SomeUIWorkflowRequestCommingIn(object sender, EventArgs e)
{
  // some code here to respond and make "state" changes or whatever
  //
  // blah blah blah


  // finally we have this (how did we know to call fish level method?? because we knew when we wrote the code to send the event in the user control)
  UpdateUserInterfaceOnFishLevelUseCaseGoalSuccess(FishLevel.SomeNamedUIWorkflow.SelectedItemForPurchase)

}



protected void UpdateUserInterfaceOnFishLevelGoalSuccess(FishLevel.SomeNamedUIWorkflow  goal)
{
  switch (goal)
  {
     case FishLevel.SomeNamedUIWorkflow.NewMasterItemSelected:
           //call some UI related methods here including methods for the other user controls if necessary....
           break;
     case FishLevel.SomeNamedUIWorkFlow.DrillDownOnDetails:
           //call some UI related methods here including methods for the other user controls if necessary....
           break;
     case FishLevel.SomeNamedUIWorkFlow.CancelMultiSelect:
           //call some UI related methods here including methods for the other user controls if necessary....
           break;

     // more cases...


     }
  }

}


//also we have
protected void UpdateUserInterfaceOnSeaLevelGoalSuccess(SeaLevel.SomeNamedUIWorkflow  goal)
{
  switch (goal)
  {
     case SeaLevel.CheckOutWorkflow.ChangedCreditCard:
        // do stuff


     // more cases...


     }
  }

}
Run Code Online (Sandbox Code Playgroud)

因此,在MyCompany.MyApp.Web.UseCases命名空间中,我们可能有这样的代码:

class SeaLevel...
class FishLevel...
class KiteLevel...
Run Code Online (Sandbox Code Playgroud)

嵌入在类中的工作流用例可以是内部类或静态方法或枚举,也可以是任何为您提供最干净的命名空间的工具.我不记得我原来做了什么,但你得到了照片.

Lou*_*nco 1

我认为这是设计模式(四人帮)中中介者模式的变体——所以我想说这是一种有效的方法。在该模式中,他们讨论了控件之间复杂的交互是使用它的原因。

编辑:链接到维基百科上的 Mediator