开发者

Properly design a code editor application

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 Doc开发者_C百科uments.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.


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.


You need a Singleton Object (DocumentManager) which manages all document operations. This will have functions like:

  • Get(idList)
  • GetNew
  • Save(docList)
  • Refresh
  • etc...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜