正确设计代码编辑器应用程序
我正在开发一个个人项目,它基本上是一个代码编辑器。想象一下标准的“文件”菜单,其中包含菜单项“新建”、“打开”、“保存”、“另存为”、“全部保存”、“关闭”、“关闭全部”。
我坚持正确的设计。目前我有:
- 一个 Document 类,代表文档代码编辑控件、选项卡栏中的相应选项卡以及各种属性,如标题、文件名、IsModified 等。
- 一个 Documents 类,代表所有打开的文档。包含诸如 New、Open(FileName)、... 之类的方法。
问题是我无法弄清楚哪个类/菜单命令负责哪些任务。
例如,使用“文件”->“新建”菜单命令很简单 - 调用 Documents.New 即可。
但是文件->打开又如何呢? Documents.Open 方法需要文件名作为参数。因此,在调用此方法之前,我需要打开一个文件选择对话框,让用户选择文件并为每个文件调用 Documents.Open(FileName)。此支持代码的最佳位置在哪里,在菜单命令中,重写 Documents.Open 并将其放在那里?
与保存操作相同。哪个负责保存?是使用 Document.Editor.SaveToFile(FileName) 的 Documents 类还是更好地在 Document 类中创建 Save 方法?在中间的某个地方还需要询问用户是否要保存当前文档......
我被卡住了。有什么想法吗?
编辑:编程语言是Delphi。
I'm working on personal project which is basically a code editor. Imagine standard File menu with menu items New, Open, Save, Save As, Save All, Close, Close All.
I'm stuck with proper design. Currently I have:
- A Document class which represents a document - code editing control, respective tab in tab bar and various properties such as Caption, Filename, IsModified etc.
- A Documents class which represents all open documents. Contains methods like New, Open(FileName), ...
The problem is that I can't figure out which class / menu command is responsible for which tasks.
For example, with File->New menu command is simple - call Documents.New and that's it.
But what for File->Open? The Documents.Open method expects filename as a parameter. So before calling this method I need to open an file selection dialog, let user select files and for each file call Documents.Open(FileName). Where is best place for this supporting code, in menu command, rewrite Documents.Open and put it there?
The same with Save actions. Which is responsible for saving? Is it Documents class which uses Document.Editor.SaveToFile(FileName) or better create Save method in Document class? Somewhere in the middle also need to ask user if he wants to save current document...
I'm stuck. Any ideas?
Edited: The programming language is Delphi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恕我直言,您向 Documents 类添加了太多职责。 Documents 类的唯一职责应该是维护具有某些相关功能的 Documents 集合(例如,处理同一文档的多个实例、检查所有文档是否已关闭、计算子文档等)。
打开文档甚至创建新文档document(例如,如果您必须选择一种格式怎么办)是单独的操作,最终会生成一个新文档,然后将其添加到 Documents 类中。在我看来,在准备好移交 Document 对象之前,您甚至不应该与 Documents 类进行交互。
我希望您有一些类来表示 UI 并与用户交互以获取创建或打开文件所需的所有信息。你应该从那里处理一切。否则,你就会用 UI 相关的东西污染你的模型。
IMHO, you are adding too many responsibilities to your Documents class. The sole responsibility of the Documents class should be to maintain the collection of Documents with some related feature (e.g., handle multiple instances of same document, check if all documents are closed, count children, etc.)
Opening a document or even creating a new document (e.g., what if you have to pick a format) are separate operations that ultimately result in a new document that is then added to the Documents class. In my opinion you should not even be interacting with the Documents class until you are ready to hand over the Document object.
I hope that you have some class for representing the UI and for interacting with the user to get all the information needed to create or open a file. You should handle everything from there. Otherwise, you're tainting your model with UI related stuff.
您需要一个单例对象(DocumentManager)来管理所有文档操作。这将具有以下功能:
You need a Singleton Object (DocumentManager) which manages all document operations. This will have functions like: