正在加载图片...
$33.4 HIDING THE REPRESENTATION:THE PRIVATE STORY 1085 generic type G is private: package STACKS is As before,replacing all occurrences of FLOAT by G... end STACKS; See appendix B. The generie clause is heavier syntax than our O-O notation for generic classes (class C [G]...)because it offers more options.In particular,the parameters declared in a generic clause may represent not just types but also routines.The appendix on genericity vs.inheritance will discuss these possibilities. The generic clause is not repeated in the package body,which will be identical to the version given earlier,except for the substitution of G for FLO47 throughout. The is private specification directs the rest of the package to treat G as a private type. This means that entities of the type may only be used in operations applicable to all Ada types:use as source or target of an assignment,as operand of an equality test,as actual argument in a routine,and a few other special operations.This is close to the convention used for unconstrained formal generic parameters in our notation.In Ada,other possibilities are also available.In particular,you can restrict the operations further by declaring the parameter as limited private,which essentially bars all uses other than as actual argument to a routine. Although called a package,a generically parameterized module such as STACKS is really a package template,since clients cannot use it directly;they must derive an actual package from it by providing actual generic parameters.We may define a new version of our stack-of-reals package through such a generic derivation: package REAL STACKS I is new STACKS(FLOAT) Generic derivation is the principal Ada mechanism for adapting modules.It is somewhat inflexible,since you can only choose between generic modules(parameterized, but not directly usable)or usable modules (not extendible any more).In contrast, inheritance allows arbitrary extensions to existing modules,according to the Open-Closed principle.Appendix B pursues the comparison further. 33.4 HIDING THE REPRESENTATION:THE PRIVATE STORY Package STACKS,as given,fails to implement the principle of information hiding:the declarations of types STACK and STACK CONTENTS are in the interface,allowing clients to access the representation of stacks directly.For example,a client might include code of the form use REAL_STACKS_1;... s:STACK,… s.implementation (3):=7.0.s.last :51;§33.4 HIDING THE REPRESENTATION: THE PRIVATE STORY 1085 generic type G is private; package STACKS is … As before, replacing all occurrences of FLOAT by G … end STACKS; The generic clause is heavier syntax than our O-O notation for generic classes (class C [G]…) because it offers more options. In particular, the parameters declared in a generic clause may represent not just types but also routines. The appendix on genericity vs. inheritance will discuss these possibilities. The generic clause is not repeated in the package body, which will be identical to the version given earlier, except for the substitution of G for FLOAT throughout. The is private specification directs the rest of the package to treat G as a private type. This means that entities of the type may only be used in operations applicable to all Ada types: use as source or target of an assignment, as operand of an equality test, as actual argument in a routine, and a few other special operations. This is close to the convention used for unconstrained formal generic parameters in our notation. In Ada, other possibilities are also available. In particular, you can restrict the operations further by declaring the parameter as limited private, which essentially bars all uses other than as actual argument to a routine. Although called a package, a generically parameterized module such as STACKS is really a package template, since clients cannot use it directly; they must derive an actual package from it by providing actual generic parameters. We may define a new version of our stack-of-reals package through such a generic derivation: package REAL_STACKS_1 is new STACKS (FLOAT); Generic derivation is the principal Ada mechanism for adapting modules. It is somewhat inflexible, since you can only choose between generic modules (parameterized, but not directly usable) or usable modules (not extendible any more). In contrast, inheritance allows arbitrary extensions to existing modules, according to the Open-Closed principle. Appendix B pursues the comparison further. 33.4 HIDING THE REPRESENTATION: THE PRIVATE STORY Package STACKS, as given, fails to implement the principle of information hiding: the declarations of types STACK and STACK_CONTENTS are in the interface, allowing clients to access the representation of stacks directly. For example, a client might include code of the form [1] use REAL_STACKS_1;… s: STACK; … s ● implementation (3) := 7.0; s ● last := 51; See appendix B
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有