简体   繁体   English

编写文本编辑器的多态性有多重要?

[英]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. 我对OOP知之甚少,所以我无法判断当时是多么明智或者提出任何具体问题。

Now, after many years of software development (mostly C++), I've used polymorphism many times to solve various problems when designing software. 现在,经过多年的软件开发(主要是C ++),我在设计软件时多次使用多态来解决各种问题。 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. 这方面的证据是,在“OOP”开始流行之前很久就开发了完全可用的文本编辑器。

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. 当OO编程风靡时,这个术语被抛出了很多。 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. 任何这里都存在争论的关键 - 您需要编写,维护或扩展文本编辑器的次数 - 没有 - 因此,对于需要的相对简单的代码片段而言,OO范例几乎没有用处。高效率。

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

I once wrote a text editor in Basic. 我曾在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. 它无论如何都不是一个复杂的文本编辑器,它的一大亮点是用于某些菜单和对话框的文本模式窗口事物,但它仍然在当时的工作 - 即它证明我可以在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. 另一方面,在过去的20年中,PC上的文本编辑器应该处理的文本数量增加了很多,有时即使是具有多个千兆字节的现代PC也可能无法保持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. 简而言之,现代文本编辑器比二十年前在DOS和其他微处理器中使用的东西要复杂得多。 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.) (大多数Web浏览器以浏览器文档对象模型(DOM)的形式实现类似的东西,尽管当然不要求它们使用多态。)

Each of these objects inherits from a common base class (often abstract) that defines a method such as Compose(). 这些对象中的每一个都继承自定义诸如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. 然后,当需要显示或更新文档的结构时,代码只是遍历树,在每个对象上调用具体的Compose()。 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. 虽然一个简单的文本编辑器(在MS-DOS下面的edit.com下面)可以在静态类中更容易实现(因为功能非常有限),一旦你进入菜单和对话框,你会发现自己处于迫切需要面向对象的语言特性。

Personally, I frown upon procedural code anyway - I prefer a mixture of OOP (program structure, separation of functionality, etc...) and functional programming (implementation). 就个人而言,我还是对程序代码不屑一顾 - 我更喜欢混合使用OOP(程序结构,功能分离等)和函数式编程(实现)。

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 a)男人或女人b)欧洲人,亚洲人,美国人,非洲人,大洋洲人(我希望这是对的)等... c)以你的名字d)你的职业

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). 所以对于那些因为统计原因问你几个问题的人,你可能会被处理为一个来自大洋洲的女人(我不知道你来自哪里,但我只想假设),他是,42岁,在瑞士生活了23年(哈哈哈)。

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. 你可以在没有OOP的情况下写记事本。 But you most likely will need OOP for something like MS Word or OpenOffice. 但是你很可能需要OOP来代替MS Word或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. 你可能想看看这本书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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