“事件驱动"和“面向对象"编程的关系是什么? [英] What is the relation of 'Event Driven' and 'Object Oriented' programming?

查看:414
本文介绍了“事件驱动"和“面向对象"编程的关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天,我几乎到处都听到有关事件驱动"编程的信息.

These days, I hear almost everywhere about 'event driven' programming.

维基百科说:

在计算机编程中,事件驱动的编程是一种编程范例,其中程序的流程由事件决定,例如用户操作(鼠标单击,按键按下),传感器输出或消息从其他程序/线程.事件驱动编程是图形用户界面和其他应用程序(例如,响应用户输入执行某些操作)的主要范例.

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.

这不是我们的老朋友吗?如果不是OOP,有什么区别?

Isn't this exactly our old friend OOP? And if this is not OOP what is the difference?

推荐答案

  1. 面向对象编程(OOP)和事件驱动编程(EDP)是正交的,这意味着它们可以一起使用.

  1. Object Oriented Programming (OOP) and Event-Driven Programming (EDP) are orthogonal, which means that they can be used together.

在具有EDP的OOP中,所有OOP原理(封装,继承和多态性)均保持不变.

In OOP with EDP all OOP principles (encapsulation, inheritance and polymorphism) stay intact.

在具有EDP的OOP中,对象具有某种发布事件通知和从其他对象预订事件通知的机制.

In OOP with EDP objects acquire some mechanism of publishing event notifications and subscribing to event notifications from other objects.

带/不带EDP的OOP之间的区别是对象之间的控制流.

The difference between OOP with / without EDP is control flow between objects.

  • 在没有EDP的OOP中,方法调用将控制权从一个对象移到另一个对象.对象主要调用其他对象的方法.

  • In OOP without EDP control moves from one object to another object on a method call. Object mainly invokes methods of other objects.

在具有EDP的OOP中,事件通知后,控件从一个对象移动到另一个对象.对象从其他对象订阅通知,然后等待来自其订阅对象的通知,根据通知执行一些工作,然后发布自己的通知.

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.

结论:由于事件驱动的设计,OOP + EDP是我们的老朋友OOP",控制流反转了.

Conclusion: OOP+EDP is "exactly our old friend OOP" with control flow inverted thanks to Event-Driven Design.

这篇关于“事件驱动"和“面向对象"编程的关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆