• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which is a better design for my text editor?  RSS feed

 
omar elgazzar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everybody.
Say I'm going to create a notepad-like text editor (or any other app, it doesn't matter).
What is the best design I can use for my app?
i.e. should I create an app builder class (maybe the main class) and instantiate a frame and its child components and modify their properties in the main class like this:

OR
should I use inheritance by extending classes like JFrame, JMenuBar, JButton, .. etc , and instantiate the subclasses in the corresponding classes like this:



Surely, I prefer the latter design, It seems like OOP, right? what do you think?
and what it has to do with performance i.e. which is better from this perspective?
and Should I care, at all, about performance when I'm making a trivial app, I guess, like this one ?!

N.B. You can consider me a leve 2 beginner
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should start with none of them. I think you should start by working out what you are going to do, takign new text, recording the text, etc. Work out how to do that first before you consider any displays.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omar elgazzar wrote:
Surely, I prefer the latter design, It seems like OOP, right? what do you think?
and what it has to do with performance i.e. which is better from this perspective?
and Should I care, at all, about performance when I'm making a trivial app, I guess, like this one ?!



No. Just extending a class doesn't constitute object-oriented programming. Generally you extend a class if you want to modify the way that class works, for example to make it more specialized, and if you also want to use it as an instance of the class you're extending. But your MyFrame doesn't do anything a JFrame can't do. You don't override any of the methods of JFrame to make it work differently either. So that's just overriding for the sake of overriding. And therefore I would recommend the first version where you just use a JFrame to do what it normally does, which also happens to be what you want to do anyway.

As for performance, whatever you had in mind for the meaning of that word, trivial differences like that won't make any difference at all. And in general the way to deal with performance issues is to measure whatever performance metric you had in mind and decide if it meets your needs or not. If not then you can start thinking about it.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how I would do it.

1) Like Campbell correctly recommended, start with what all things is the text editor is supposed to do. (Functionality)
2) Once done, come up with a UI design which is simple and intuitive. I personally prefer a white board sketch
3) Identify common elements in the UI. e.g. A save button as well as a save menu as well as an auto save in the background (maybe)

At this stage there is not a single line of code written, but you do have a crystal clear picture of what is required
Now on to figuring out how many classes might be required, how they are structured and how they interact with each other. For me, at this stage, one thing I always keep in mind is the single responsibility princle

1) Main class. viz. Application entry point
2) Since #3 above is a realistic requirement, I would go for custom actions which extend the AbstractAction. Advantage is the same instance of these actions can be reused across various controls like menu, button etc
3) JFrame which will be the main application 'view'. Customize it for size, menu bar, tool bar etc (This is what Paul meant. Do not extend for extension sake. Extend only when you are adding/modifying functionality)
4) JTextArea which will act as the editor. Customize it with context menus for copy/paste/whatever as required
5) Helper class as required. e.g. IO helper to read write to a file, Save helper to auto save updates in the background etc.

At this stage you have the main classes identified. However they still are individual classes and on their own, they do not amount to anything much. The key is to 'wire' them together.

e.g.The JFrame has a child JTextArea where the user inputs the content. When the user clicks save (button/menu/Ctrl+S) the entity which is notified is the custom Save abstract action #2 above. For the save to work properly, you need to somehow make the user input available to the save action which will in turn pass it on to the IO helper which will actually do the disk writing part.

One way of doing it is have the JFrame (which is 'aware' of the save action as well as the JTextArea) act as an intermediary. In other words, pass the JFrame instance in the constructor to the save action. Expose a method in the JFrame which will return the current text from the editor.
There can be other ways to achieve this.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!