从流程图生成java代码
流程图如下:
可以通过以下 java 代码进行描述:
if (A == 1 && B ==1){
actionA();
}
if (B == 3 || (B == 1 && A == 2)){
actionB();
actionC();
}
if (B == 2){
actionC();
}
有没有更好的方法在 java 代码中翻译流程图?我正在寻找某种通用模式来做到这一点。我的问题源于这样一个事实:向流程图添加单个条件会导致代码发生非常重大的变化。
The following flowchart:
may be described by the following java code:
if (A == 1 && B ==1){
actionA();
}
if (B == 3 || (B == 1 && A == 2)){
actionB();
actionC();
}
if (B == 2){
actionC();
}
Is there a better way to translate a flowchart in java code? I am looking for some sort of general pattern to do this. My question arises from the fact that adding a single condition to the flowchart results in very significant changes to the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以封装 ActionB 和 ActionC,而 ActionC 在 ActionBC 中的 ActionB 之后被调用,并为流程图中的每个单元格创建一个新方法。一般来说,你应该得到类似的东西:
<代码>
无效 B1(){
如果(B==1)
A1();
如果(B==2)
动作C();
...
}
无效 A1(){
如果(A1==2)
动作BC();
}
private void actionBC(){...}
等等...这样,扩展流程图就不会导致代码爆炸。
You could encapsulate ActionB and ActionC, while ActionC is being called after ActionB in ActionBC and make a new method for each cell in your flow chart. In general you should get something like:
void B1(){
if(B==1)
A1();
if (B==2)
actionC();
...
}
void A1(){
if(A1==2)
actionBC();
}
private void actionBC(){...}
And so on... In that way, expanding your flowchart won't explode you code.
这看起来像一个图结构,每个顶点都是一个条件或一个动作。
寻找行动只是遵循条件值给出的路径。
This looks like a graph structure, each vertex is a condition or an action.
Finding the action will just be to follow the path given by the values of the conditions.
我使用 www.browxy.com 从代码生成流程图,反之亦然。有一个绘制缺陷图的新功能,可通过按钮激活:“切换工作区”
I'm using www.browxy.com to generate flowchart from code and vice-versa. There is a new feature to draw flawcharts that is activated with the button: "switch workspace"