jerry_fuyi

My Links

Blog Stats

事件驱动和面向对象编程的关系

最近要做一个事件驱动的框架,为了理清概念,在互联网上以“事件驱动 面向对象 关系”搜索,无果,故Google之,发现Stack Overflow上一个问题,that's exactly what I want. 为提高英语水平,译之。

原文:What is the relation of 'Event Driven' and 'Object Oriented' programming?

 

问题:

These days, I hear almost everywhere about 'event driven' programming.
Wikipedia says:
In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads. Event-driven programming is the dominant paradigm used in graphical user interfaces and other applications (e.g. Javascript web applications) that are centered around performing certain actions in response to user input.
Isn't this exactly our old friend OOP? And if this is not OOP what is the difference?

最近我几乎在哪里都能听到EDP(事件驱动编程)。

维基百科写道:

在计算机编程中,EDP是一种程序流由用户操作(鼠标点击、键按下)、传感器输出,或从其他程序/线程传出的消息等事件决定的编程范式。EDP是在以用一定的输出来响应用户输入为中心的GUI(图形用户界面)和其他应用(例如JavaScript网络应用)中使用的最主要的范式。

这不就是我们的老朋友OOP(面向对象编程)吗?如果不是,区别在哪?

 

回答1:

1. Object Oriented Programming (OOP) and Event-Driven Programming (EDP) are orthogonal, which means that they can be used together.
2. In OOP with EDP all OOP principles (encapsulation, inheritance and polymorphism) stay intact.
3. In OOP with EDP objects acquire some mechanism of publishing event notifications and subscribing to event notifications from other objects.
4. The difference between OOP with / without EDP is control flow between objects.
    · In OOP without EDP control moves from one object to another object on a method call. Object mainly invokes methods of other objects.
    · In OOP with EDP control moves from one object to another object on event notification. Object subscribes on notifications from other objects, then waits for notifications from the objects it is subscribed on, does some work based on notification and publishes it's own notifications.
5. Conclusion: OOP+EDP is "exactly our old friend OOP" with control flow inverted thanks to Event-Driven Design.

1. OOP和EDP是相关的,这意味着它们可以一起使用。

2. 在有EDP的OOP中所有OOP原则(封装、继承和多态)保持完整。

3. 在有EDP的OOP中对象有一些发布事件通知和从其他对象订阅事件通知的机制。

4. 有无EDP的OOP之间的区别是对象之间的控制流:

    · 在没有EDP的OOP中控制流通过方法调用从一个对象转移到另一个对象。对象主要调用其他对象的方法。

    · 在有EDP的OOP中控制流通过事件通知从一个对象转移到另一个对象。对象订阅其他对象的通知,然后等待它订阅的对象的通知,根据通知做一些事并发布它自己的通知。

5. 总结:OOP+EDP就是控制流倒转(归功于事件驱动设计)的“我们的老朋友OOP”。

 

回答2:

Object Oriented Programming is defined by the pairing together of data and actions into a model of a real world object. Event driven programming is a style of programming in which we have a server, whether it be on a communications port or a user interface, waiting for an input command. It will then process that command and display/produce desired results.
Most event driven languages are object oriented. The objects await the events. A program in an object oriented language is not necessarily event driven, and event driven programming does not necessarily require an object oriented language. They are unrelated.

OOP的定义是把数据和动作组装到现实世界的对象中。EDP是一种编程风格,其中我们有一个服务器,或是通信端口或是用户界面。它等待输入命令,然后处理命令并显示/产生预期的结果。

大多数ED的语言都是OO的。对象等待事件。用OO语言写的程序不一定是ED的,EDP也不一定需要OO语言。它们是不相关的。

 

译者注:这个回答我不怎么赞同。

面向对象的概念核心是消息,技术核心是多态,为了实现多态需要继承,为了实现继承需要封装,仅有封装只能算是object-based。

“一个服务器”的说法是不准确的,EDP中可以有多个对象发布通知,也可以有多个对象订阅事件。

“等待输入命令”也是不准确的。在基于轮询的单线程系统中,确实是等待,但是在允许异步调用的系统中,事件触发主要利用回调。

 

posted on 2019-08-12 10:48 jerry_fuyi 阅读(...) 评论(...) 编辑 收藏