版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
事件型设计模式事件型设计模式常用的四种用于处理事件的设计模式ReactorProactorAsynchronousCompletionTokenAcceptor-Connector.Reactor设计模式描述Reactor模式允许事件驱动的系统复用和分发对服务请求,这些请求来自与一个或多个客户端Reactor有时也叫做Dispatcher,Notifier结构Reactor结构TheReactorarchitecturalpatternallowsevent-drivenapplicationstodemultiplex&dispatchservicerequeststhataredeliveredtoanapplicationfromoneormoreclientsHandleownsdispatches*notifies**handlesetReactorhandle_events()register_handler()remove_handler()EventHandlerhandle_event()get_handle()ConcreteEventHandlerAhandle_event()get_handle()ConcreteEventHandlerBhandle_event()get_handle()SynchronousEventDemuxerselect()<<uses>>Reactor动态过程:MainProgram:ConcreteEventHandler:Reactor:SynchronousEventDemultiplexerregister_handler()get_handle()handle_events()select()handle_event()HandleHandlesHandlesCon.EventHandlerEventsservice()eventObservationsNoteinversionofcontrolAlsonotehowlong-runningeventhandlerscandegradetheQoSsincecallbacksstealthereactor’sthread!InitializephaseEventhandlingphase结构Reactor使用中注意的问题Reactor中注意以下组件事件监听组件的性能和时间间隔有无必要设定定时器事件事件处理采用同步还是异步方式事件处理的调度可否采用队列Proactor设计模式Proactor设计模式允许事件驱动的系统在异步操作完成时触发操作,使得此时系统能够复用和分发系统请求,借此提高并发处理中的性能TheProactor结构ProblemDevelopingsoftwarethatachievesthepotentialefficiency&scalabilityofasyncI/Oishardduetotheseparationintime&spaceofasyncoperationinvocations&theirsubsequentcompletioneventsSolutionApplytheProactorarchitecturalpattern(P2)tomakeefficientuseofasyncI/OHandle<<executes>>*<<uses>>isassociatedwith<<enqueues>><<dequeues>><<uses>><<uses>>Initiator<<demultiplexes
&dispatches>><<invokes>>EventQueueCompletionAsynchronousOperationProcessorexecute_async_op()AsynchronousOperationasync_op()AsynchronousEventDemuxerget_completion_event()Proactorhandle_events()CompletionHandlerhandle_event()ConcreteCompletionHandlerThispatternallowsevent-drivenapplicationstoefficientlydemultiplex&dispatchservicerequeststriggeredbythecompletionofasyncoperations,therebyachievingtheperformancebenefitsofconcurrencywithoutincurringitsmanyliabilitiesProactor动态结构ResultCompletionHandlerCompletion:AsynchronousOperation:ProactorCompletionHandlerexec_async_handle_Resultservice():AsynchronousOperationProcessor:Initiatorasync_operation()Resulthandle_events()eventeventEv.Queueoperation():CompletionEventQueueResultevent()InitiateoperationProcessoperationRuneventloopGenerate&queuecompletioneventDequeuecompletionevent&performcompletionprocessingNotesimilarities&differenceswiththeReactorpattern,e.g.:BothprocesseventsviacallbacksHowever,it’sgenerallyeasiertomulti-threadaproactorAsynchronousCompletionToken
设计模式AsynchronousCompletionToken(异步完成令牌)允许系统在异步操作完成之后及时处理相应。AsynchronousCompletionTokenStructure&ParticipantsDynamicInteractionshandle_event()Acceptor-Connector设计模式Acceptor-Connector(接收器-连接器)封装网络连接成为通讯的对等端点Acceptor-Connector常用于P2P系统当中,分别封装客户端和服务器14Acceptor-Connector结构Handleownsdispatches*notifies**handlesetReactorhandle_events()register_handler()remove_handler()EventHandlerhandle_event()get_handle()ConnectorSynchronousEventDemuxerselect()<<uses>>AcceptorServiceHandlerTheAcceptor-ConnectorPattern<<activate>>owns*usesuses<<creates>>ownsusesowns<<activate>>******usesnotifiesnotifiesnotifiesConnectorConnector()connect()complete()handle_event()ConcreteServiceHandlerBConcreteServiceHandlerAConcreteAcceptorConcreteConnectorAcceptorAcceptor()Accept()handle_event()peer_acceptor_ServiceHandleropen()handle_event()set_handle()peer_stream_Dispatcherselect()handle_events()register_handler()remove_handler()TransportHandleTransportHandleTransportHandleAcceptorDynamicsServiceHandlerEvents:Application:Acceptor:Dispatcherregister_handler()handle_events()accept()open()register_handler()handle_event()service():ServiceHandleropen()ACCEPT_EVENTHandle1Acceptor:Handle2Handle2Handle2Passive-modeendpointinitializephaseServicehandlerinitializephaseServiceprocessingphaseTheAcceptorensuresthatpassive-modetransportendpointsaren’tusedtoread/writedataaccidentallyAndviceversafordatatransportendpoints…ThereistypicallyoneAcceptorfactoryper-service/per-portAdditionaldemuxingcanbedoneathigherlayers,alaCORBASynchronousConnectorDynamicsHandleAddr:Application:Connector:Dispatcher:ServiceHandlerhandle_events()connect()open()register_handler()handle_event()service()ServiceHandlerEventsServiceHandlerHandleget_handle()MotivationforSynchronySyncconnectioninitiationphaseServicehandlerinitializephaseServiceprocessingphaseIftheservicesmustbeinitializedinafixedorder&theclientcan’tperformusefulworkuntilallconnectionsareestablishedIfconnectionlatencyisnegligiblee.g.,connectingwithaserveronthesamehostviaa‘loopback’deviceIfmultiplethreadsofcontrolareavailable&itisefficienttouseathread-per-connectiontoconnecteachservicehandlersynchronouslyAsynchronousConnectorDynamicsAddr:Application:Connector:Dispatcher:ServiceHandlerhandle_events()complete()connect()open()register_handler()handle_event()service()ServiceHandlerConnectorCONNECTEVENTEventsregister_handler()ServiceHandlerHandleHandleHandleget_handle()Motivation
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 汽车行业竞争下A公司绩效评价体系构建与优化
- 汽车安全气囊盖注塑模具的创新设计与多维度分析
- 吉檀迦利:泰戈尔的灵性之歌与生命哲思
- 商品房固定设施设备买卖协议
- 商场导视系统技师试卷及答案
- 大气臭氧前体物检测技师考试试卷及答案
- 2026年医院安全管理方案及防控注意事项
- 2026年钢筋钢材考试真题及答案解析
- 土方开挖工程监理实施细则
- 安全生产风险辨识方案
- 血管活性药物
- 2026年医用耗材培训考试试题及答案
- 结直肠癌筛查与早期诊断
- 幼儿园故事小马过河
- 行政执法培训课件
- 糖尿病骨质疏松管理的医患协同策略
- 幼儿园老师音乐培训课件
- 中国水稻研究所招聘笔试题库2026
- 化学品MRSL培训教材
- 2025至2030全球及中国无人机电池行业运营态势与投资前景调查研究报告
- 阿里巴巴组织文化案例分析
评论
0/150
提交评论