正在加载图片...
SB.3 EMULATING INHERITANCE WITH GENERICITY 1175 B.3 EMULATING INHERITANCE WITH GENERICITY To compare genericity with inheritance,we will study how,if in any way,the effect of each feature may be emulated in a language offering the other. First consider a language such as Ada(again meaning Ada 83),offering genericity but not inheritance.Can it be made to achieve the effects of inheritance? The easy part is name overloading.Ada,as we know,allows reusing the same routine name as many times as needed for operands of different types;so you can define types such as TAPE,DISK and others,each with its own version of the routines: This extract and the procedure open (p:in out TAPE;descriptor:in INTEGER); next few are in Ada procedure close (p:in out D/SK); syntax. No ambiguity will arise if the routines are distinguished by the type of at least one operand.But this solution does not provide polymorphism and dynamic binding,whereby d.close,for example,would have a different effect after assignments d:=di and d:=ta, where di is a DISK and ta a TAPE. To obtain the same effect,you have to use records with variant fields:define type DEVICE(unit:DEVICE TYPE)is record ..Fields common to all device types... case unit is when tape =>..fields for tape devices ... when disk =>..fields for disk devices.... ..Other cases...; end case end record where DEVICE TYPE is an enumerated type with elements tape,disk etc.Then there would be a single version of each the procedures on devices (open,close etc.),each containing a case discrimination of the form case d'uit is when tape =>..action for tape devices...; when disk=>...action for disk devices...; ..other cases.... end case See"Single Choice” This uses explicit discrimination in each case,and closes off the list of choices, page 61. forcing every routine to know of all the possible variants;addition of new cases will cause changes to all such routines.The Single Choice principle expressly warned against such software architectures. So the answer to the question of this section is essentially no: Emulating inheritance It appears impossible to emulate inheritance through genericity§B.3 EMULATING INHERITANCE WITH GENERICITY 1175 B.3 EMULATING INHERITANCE WITH GENERICITY To compare genericity with inheritance, we will study how, if in any way, the effect of each feature may be emulated in a language offering the other. First consider a language such as Ada (again meaning Ada 83), offering genericity but not inheritance. Can it be made to achieve the effects of inheritance? The easy part is name overloading. Ada, as we know, allows reusing the same routine name as many times as needed for operands of different types; so you can define types such as TAPE, DISK and others, each with its own version of the routines: procedure open (p: in out TAPE; descriptor: in INTEGER); procedure close (p: in out DISK); No ambiguity will arise if the routines are distinguished by the type of at least one operand. But this solution does not provide polymorphism and dynamic binding, whereby d ● close, for example, would have a different effect after assignments d := di and d := ta, where di is a DISK and ta a TAPE. To obtain the same effect, you have to use records with variant fields: define type DEVICE (unit: DEVICE_TYPE) is record … Fields common to all device types … case unit is when tape => … fields for tape devices …; when disk => … fields for disk devices …; … Other cases …; end case end record where DEVICE_TYPE is an enumerated type with elements tape, disk etc. Then there would be a single version of each the procedures on devices (open, close etc.), each containing a case discrimination of the form case d'unit is when tape => … action for tape devices …; when disk => … action for disk devices …; … other cases …; end case This uses explicit discrimination in each case, and closes off the list of choices, forcing every routine to know of all the possible variants; addition of new cases will cause changes to all such routines. The Single Choice principle expressly warned against such software architectures. So the answer to the question of this section is essentially no: Emulating inheritance It appears impossible to emulate inheritance through genericity. This extract and the next few are in Ada syntax. See “Single Choice”, page 61
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有