oopclass-designresponsibility

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:

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.


Solution

  • 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.