简体   繁体   中英

How essential is polymorphism for writing a text editor?

Many years ago when I didn't know much about object oriented design I heard one guy said something like "How can you write a text editor without polymorphism?" I didn't know much about OOP and so I couldn't judge how wise that though was or ask any specific questions at that time.

Now, after many years of software development (mostly C++), I've used polymorphism many times to solve various problems when designing software. Yet I've never created text editors. So I still can't evaluate that guy's idea.

Is using polymorphism so essential for implementing a text editor in object-oriented languages and why?

Polymorphism for writing a text editor is by no means essential. In fact, polymorphism for solving any programming problem is not essential. It's just one way to do it. Sometimes it makes solving certain kinds of problems easier, and sometimes it just gets in the way.

The evidence for this is that there are perfectly usable text editors developed long before "OOP" became popular.

I would say "no", because it's entirely possible to write perfectly good text editors in non-object-oriented languages, so it can't be that essential.

Polymorphism is a great technique for the problems it addresses, but it's by no means the golden hammer for everything that ails a software developer.

This is a term that was thrown around a lot when OO programming was the rage. This guy was probably trying to intimidate you with large words, I doubt if he fully understood what he was saying although it is a simple concept when explained.

Any here lies the crux of the argument - how many times would you have to write, maintain or extend a text editor - none - therefore imho an OO paradigm is of little use in a for what is a relatively simple piece of code that needs to be highly efficient.

许多设计模式,比如纪念品,飞锤等可以用来设计/实施的文本编辑器需要继承和多态。

I once wrote a text editor in Basic. It wasn't a sophisticated text editor by any means, it's big highlight being a text-mode windowing thing used for some menus and dialogs, but it still did it's job at the time - ie it proved I could write a text editor in Basic. I even used it sometimes. I won't be showing the source in public - it's just too embarrassing!

When your text editor is mostly just inserting/deleting characters in a big array of strings and displaying them, little or no abstraction is needed other than the usual provided-as-standard abstractions of arrays and strings.

On the other hand, the amount of text that a text editor on a PC is expected to cope with has increased a lot over the last 20 years, sometimes to the extent that even a modern PC with multiple Gigabytes may not be able to keep the whole file in RAM. On top of that there are character set and encoding issues. A good text editor is expected to remember a (potentially large) number of bookmarks into multiple files, and to maintain them so they refer to the same point despite edits. And then there's syntax highlighting, the ability to record/playback macros, and more.

In short, modern text editors are much more complex than the things used in DOS and on other micros twenty years ago. That complexity is no doubt much easier to manage with a good toolkit for handling abstractions.

The other points about polymorphism as being just a tool are spot on.

However if "the guy" did have some experience with writing text editors he may well have been talking about using polymorphism in the implementation of a document composition hierarchy.

Basically this is just a tree of objects that represent the structure of your document including details such as formatting (bold, italic etc) coloring and so on.

(Most web browsers implement something similar in the form of the browser Document Object Model (DOM), although there is certainly no requirement that they use polymorphism.)

Each of these objects inherits from a common base class (often abstract) that defines a method such as Compose().

Then when it is time to display or to update the structure of the document, the code simply traverses the tree calling the concrete Compose() on each object. Each object is then repsonsible for composing and rendering itself at the appropriate location in the document.

This is a classic use of polymorphism because it allows new document "components" to be added (or changed) without any (or minimal) change to the main application code.

Once again though, there are many ways to build a text manipulation program, polymorphism is definitely not required to build one.

While a simple text editor (below edit.com from MS-DOS) may be realized easier in a static class only (because the functionality is very limited), as soon as you get to menus and dialogs, you will find yourself in dire need of object oriented language features.

Personally, I frown upon procedural code anyway - I prefer a mixture of OOP (program structure, separation of functionality, etc...) and functional programming (implementation).

This may sound like a religious argument of some sort, but I find my personal style quite recommendable. Usually I need far less lines of code (which are much easier to understand) than most of the developers I work with and my code feels much more "agile" and "flexible".

Try it. :-)

Oh - and polymorphy is not hard to understand. Simply imagine that you (as a person) can be handled as:

a) Man or woman b) European, asian, american, african, oceanian (I hope this is right), etc... c) By your name d) By your occupation

But still you are a person - and a living being, and a part of the universe... and YOU.

So for someone who asks you for statistical reasons a few questions, you may be handled as a, say, woman from oceania (I don't know where you come from, but lets just assume) who is, hm, 42 years old and lived in, hm, Switzerland for 23 years (hahaha).

For your employer, you may be competent in programming and talking to your collegues.

However, HOW you fill those roles is dependent on your implementation. This is you.

Is using polymorphism so essential for implementing a text editor in object-oriented languages and why?

Depends on what kind of text editor you're talking about.

You can write notepad without OOP. But you most likely will need OOP for something like MS Word or OpenOffice.

Design Patterns: Elements of Reusable Object-Oriented Software uses text editor for examples (ie "case study") of Design Pattern application. You may want to check out the book.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM