设计控制台应用程序时的架构考虑? [英] Architectural considerations in designing console applications?

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

问题描述

我最近编写了一个控制台应用程序,鉴于其纯 OO 范式,我在设计它的许多方面都经历了很多痛苦,尤其是在 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.

我的问题是:你们中有人知道面向对象范例中控制台应用程序的良好设计,以便我可以向他们学习吗?特别欢迎使用良好实现的代码.

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 原则......[你的计算机科学教授会很自豪!]),但没有真正的代码我可以接受看看这些概念在行动.同样,我应该在哪里寻找(代码!)纯 OO 范式中好的命令行应用程序?

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 是正确的——您可能应该了解 REPL 并复制一个好的.

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.

为了模块化,调度器应该配置一个抽象的映射(例如,哈希表)以将每个命令标记(通常是命令行字符串的第一个单词)与处理它的命令对象相关联.调度程序还可以处理常见的选项解析,可能使用一些现成的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.

简单来说,每个 Command 对象都可以实现一个一致的接口来完成它的工作;也许是这样的:

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)

这样,入口点调度器就可以找到被请求的命令,然后执行它.

This way, the entry-point dispatcher just finds the Command being requested, and executes it.

关于错误处理:execute() 方法可能只是抛出一个异常来传达错误......异常可以被调度程序捕获并处理,或者只是记录到屏幕上.或者,失败的命令可以调用一些共享的 usage 函数来将错误消息与一般指令结合起来.我认为切入点"不一定要像您建议的那样意识到问题;如果您需要强大的错误处理(例如,用于日志记录或警报功能),这似乎属于可以提供给 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天全站免登陆