正在加载图片...
$21.2 FINDING THE ABSTRACTIONS 703 The key to this solution-and its refinements in the rest of this chapter-is polymorphism and dynamic binding.Attribute requested is polymorphic:declared of type COMMAND,it will become attached to objects of one of its effective descendant types such as LINE INSERTION.The calls requested.execute and requested.undo only make sense because of dynamic binding:the feature they trigger must be the version redefined for the corresponding command class,executing or undoing a LINE INSERTION,a LINE DELETION or a command of any other type as determined by the object to which requested happens to be attached at the time of the call. The system's actions No part of the structure seen so far is application-specific.The actual operations of the application,based on its specific object structures -for example the structures representing the current text in a text editor-are elsewhere;how do we make the connection? The answer relies on the execute and undo procedures of the command classes, which must call application-specific features.For example procedure execute of class LINE DELETION must have access to the editor-specific classes to call features that will yield the text of the current line,give its position in the text,and remove it. As a result there is a clear separation between the user interaction parts of a system, largely application-independent,and the application-specific parts,closer to the model of each application's conceptual model-be it text processing,CAD-CAM or anything else. The first component,especially when generalized to a history mechanism as explained next,will be widely reusable between various application domains. How to create a command object After decoding a request,the system must create the corresponding command object.The instruction appeared abstractly as "Create appropriate command object and attach it to requested";we may express it more precisely,using creation instructions,as if"Request is LINE INSERTION"then LINE INSERTION requested.make (input text,cursor_index) elseif"Request is LINE DELETION"then LINE DELETION requested.make (current line,line index) elseif "Polymorphic cre- This uses the SOME TYPE x...form of the creation instruction,which creates an ation”page479. object of type SOME TYPE and attaches it to x;remember that SOME TYPE must conform to the type declared for x,as is the case here since requested is of type COMMAND and all the command classes are descendants of COMMAND. If each command type uses a unique integer or character code,a slightly simpler form relies on an inspect:§21.2 FINDING THE ABSTRACTIONS 703 The key to this solution — and its refinements in the rest of this chapter — is polymorphism and dynamic binding. Attribute requested is polymorphic: declared of type COMMAND, it will become attached to objects of one of its effective descendant types such as LINE_INSERTION. The calls requested ● execute and requested ● undo only make sense because of dynamic binding: the feature they trigger must be the version redefined for the corresponding command class, executing or undoing a LINE_INSERTION, a LINE_DELETION or a command of any other type as determined by the object to which requested happens to be attached at the time of the call. The system’s actions No part of the structure seen so far is application-specific. The actual operations of the application, based on its specific object structures — for example the structures representing the current text in a text editor — are elsewhere; how do we make the connection? The answer relies on the execute and undo procedures of the command classes, which must call application-specific features. For example procedure execute of class LINE_DELETION must have access to the editor-specific classes to call features that will yield the text of the current line, give its position in the text, and remove it. As a result there is a clear separation between the user interaction parts of a system, largely application-independent, and the application-specific parts, closer to the model of each application’s conceptual model — be it text processing, CAD-CAM or anything else. The first component, especially when generalized to a history mechanism as explained next, will be widely reusable between various application domains. How to create a command object After decoding a request, the system must create the corresponding command object. The instruction appeared abstractly as “Create appropriate command object and attach it to requested”; we may express it more precisely, using creation instructions, as if “Request is LINE INSERTION” then ! LINE_INSERTION ! requested ● make (input_text, cursor_index) elseif “Request is LINE DELETION” then ! LINE_DELETION ! requested ● make (current_line, line_index) elseif … This uses the ! SOME_TYPE ! x … form of the creation instruction, which creates an object of type SOME_TYPE and attaches it to x; remember that SOME_TYPE must conform to the type declared for x, as is the case here since requested is of type COMMAND and all the command classes are descendants of COMMAND. If each command type uses a unique integer or character code, a slightly simpler form relies on an inspect: “Polymorphic cre￾ation”, page 479
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有