在设计控制台应用程序体系结构方面的考虑? [英] Architectural considerations in designing console applications?

查看:221
本文介绍了在设计控制台应用程序体系结构方面的考虑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近编程的控制台应用程序,我已经经历了很多痛苦,在许多方面,特别是在C#设计它,因为它纯面向对象的范例。我所面临的问题包括如何通过选择如何的问题返回入口点类,其中很多很多其他东西。

I have recently programmed a console application and I've experienced a lot of pain in designing it in many aspects, particularly in C#, given its pure OO paradigm. Questions I have faced include anything from how to pass the options to how to return problems to the entry point class, among many, many others.

我的问题是:在OO范式将任何你知道的控制台应用程序很好的设计,所以我可以从他们身上学到什么?良好的实现code是的尤其​​欢迎。

My question is: would any of you know of good designs of console applications in an OO paradigm so I can learn from them? Code of good implementations is particularly welcome.

编辑:我不是后命令行的API,但经过良好的设计原则,并特别好的实现我可以从学习

I'm not after command-line APIs, but after good design principles, and, in particular, good implementations I could learn from.

编辑2:有一个在应用程序简单的用户交互,但它不是一个完整的CLI / REPL排序。把它看成是TeX的命令,或多或少。有趣的是,即使有好的理论左右浮动(不大于x不同,使用模式Y,你应该知道OO原则...... [计算机科学教授会如此自豪!]),没有真正的code口可以看看在行动中看到这些概念。同样,我应该在哪里看(code!)在一个纯面向对象的范例一个很好的命令行应用程序?

EDIT 2: There is simple user interaction in the application, but it is not a full-fledged CLI/REPL sort. Think of it as the TeX command, more or less. Interestingly, even though there is good theory floating around (no different than X, use pattern Y, you should know OO principles...[your computer science professor would be so proud!]), there is no real code I can take a look at to see these concepts in action. Again, where should I look (code!) for a good command line application in a pure OO paradigm?

推荐答案

这听起来好像你正在构建与执行每次调用几个不同的操作之一的接口。我不知道,如果你指的是命令行应用程序(这确实一个动作,然后退出)或CLI应用程序(它显示一个提示,并多次响应用户输入)。在一般情况下,前者会简单得多建立比后者;我认为,如果你的应用程序需要在演进多个命令一些持久状态它才有意义使用CLI。如果你正在处理这样的事情,那么alphazero是正确的 - 你应该了解REPLs并复制一个好

It sounds as if you're building an interface that performs one of several distinct operations with each invocation. I'm not sure if you're referring to a "command-line" application (which does one action, then exits) or a CLI application (which displays a prompt and responds repeatedly to user input). In general, the former will be much simpler to build than the latter; I think it only makes sense to use a CLI if your application requires some persistent state that evolves over multiple commands. If you are tackling something like this, then alphazero is correct -- you should probably learn about REPLs and copy a good one.

在任何情况下,表现将取决于在命令行传递的参数运行,所以我会集体讨论的一部分...

In any case, the operation performed will depend on the arguments passed on the command-line, so I'll brainstorm about that part...

它的应用程序为一组不同的命令的对象,一个用于每个操作类型的显想。因此,入口点应用程序应该是某种CommandLineDispatcher对象把请求转发给相应的Command对象。

It's sensible think of the application as a set of distinct "Command" objects, one for each type of operation. The entry-point to the application should thus be a some sort of CommandLineDispatcher object that dispatches requests to the appropriate Command object.

要模块化,调度员应具有抽象的映射(例如,一个Hashtable),以每个命令令牌(通常是命令行字符串的第一个字)关联到处理它的命令对象中配置。调度员还可以处理常用选项的解析,可能使用一些现成的现成getopts的库做繁重。

To be modular, the dispatcher should be configured with an abstracted mapping (eg, a Hashtable) to associate each command token (usually the first word of the command-line string) to the Command object that handles it. The dispatcher may also handle common options-parsing, probably using some off-the-shelf "getopts" library to do the heavy lifting.

从简单开始,每个命令对象可以实现做其工作的一致的界面;也许是这样的:

To start simple, each Command object could implement a consistent interface for doing its work; maybe something like this:

public void execute(List<String> args)

这样,入口点调度刚刚发现命令被请求,而执行了。

对于错误处理:在的execute()方法可能只是抛出一个异常,通信错误...异常可以捕获并处理调度,或简单地记录到屏幕上。另外,没有命令可以调用一些共享使用函数的错误消息,一般指示结合起来。我不认为你建议切入点应该一定要意识到的问题;如果你需要强大的错误处理(例如,用于记录或报警功能),这似乎是它属于可能被提供到Command对象一个单独的组件。

Regarding error-handling: the execute() method might just throw an exception to communicate errors... Exception could be caught and processed by the dispatcher, or simply logged to the screen. Alternatively, failing Commands could invoke some shared usage function to combine an error message with general instructions. I don't think the "entry point" should necessarily be made aware of problems as you suggest; if you need robust error-handling (eg, for logging or alerting capabilities), this seems like it belongs in a separate component that could be provided to the Command object.

在一般情况下,一个命令行应用程序并不比响应用户输入任何其他应用程序不同 - 你需要一个调度程序解析和路由输入,处理程序(又名控制器)来执行的支持操作。如果您需要其他服务(日志,警报,数据库连接等),你会做得很好,创建单独的组件来这个逻辑和清晰的接口将其公开。

In general, a command-line application is no different than any other application that responds to user input -- you'll need a dispatcher to parse and route the input, and handlers (aka "controllers") to execute the supported operations. If you need other services (logging, alerting, database connectivity, etc), you'll do well to create separate components to isolate this logic and expose it with clean interfaces.

这篇关于在设计控制台应用程序体系结构方面的考虑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆