正在加载图片...
$18.6 UNIQUE VALUES 655 It is tedious,however,to have to come up with the individual constant values.The following notation has the same practical effect as [U1]: [U4 Successful,Open error,Read error:INTEGER is unique A unique value specification,coming in lieu of a manifest integer value in the declaration of a constant integer attribute,indicates that the value is chosen by the compiler rather than the developer.So the conditional instruction [U2]and the multi- branch [U3]are still applicable. All unique values within a class are guaranteed to be positive and different;if they are declared together,as the three in [U4],they are also guaranteed to be consecutive.So if you want to express that code will only receive one of their values,you can include the invariant clause code >Successful;code <Read error With this invariant,a descendant-which,as we know,may change the invariant only by strengthening it-may constrain the possible values of code further,for example to just two possibilities;it may not extend the set of possibilities. You should only use Unique values to represent a fixed set of possible values.As soon as this set is open to variation,or the instructions in a structure such as [U3]are non- trivial,it is preferable to devise a set of classes which variously redefine some features, and then to rely on dynamic binding,satisfying the Open-Closed principle.More generally,do not use unique values for classification since the object-oriented method has better techniques.The preceding example is typical of good uses of the mechanism;others would be traffic light states(green,yellow,red:INTEGER is unique)or,as seen earlier, notes on the scale (do,re,mi,...:INTEGER is unique).But a declaration savings, checking,money_market:INTEGER is unique is probably a misuse if the various kinds of account have different features or different implementations of a common feature;here inheritance and redefinition will most likely provide a better solution. These observations can be summed up as a methodological rule: Discrimination principle Use unique values to describe a fixed number of possible cases.For classification of data abstractions with varying features,use inheritance. Although similar in some respects to the "enumerated types"of Pascal and Ada, unique declarations do not introduce new types,only integer values.The discussion section will explore the difference further.§18.6 UNIQUE VALUES 655 It is tedious, however, to have to come up with the individual constant values. The following notation has the same practical effect as [U1]: [U4] Successful, Open_error, Read_error: INTEGER is unique A unique value specification, coming in lieu of a manifest integer value in the declaration of a constant integer attribute, indicates that the value is chosen by the compiler rather than the developer. So the conditional instruction [U2] and the multi￾branch [U3] are still applicable. All unique values within a class are guaranteed to be positive and different; if they are declared together, as the three in [U4], they are also guaranteed to be consecutive. So if you want to express that code will only receive one of their values, you can include the invariant clause code >= Successful; code <= Read_error With this invariant, a descendant — which, as we know, may change the invariant only by strengthening it — may constrain the possible values of code further, for example to just two possibilities; it may not extend the set of possibilities. You should only use Unique values to represent a fixed set of possible values. As soon as this set is open to variation, or the instructions in a structure such as [U3] are non￾trivial, it is preferable to devise a set of classes which variously redefine some features, and then to rely on dynamic binding, satisfying the Open-Closed principle. More generally, do not use unique values for classification since the object-oriented method has better techniques. The preceding example is typical of good uses of the mechanism; others would be traffic light states (green, yellow, red: INTEGER is unique) or, as seen earlier, notes on the scale (do, re, mi, …: INTEGER is unique). But a declaration savings, checking, money_market: INTEGER is unique is probably a misuse if the various kinds of account have different features or different implementations of a common feature; here inheritance and redefinition will most likely provide a better solution. These observations can be summed up as a methodological rule: Although similar in some respects to the “enumerated types” of Pascal and Ada, unique declarations do not introduce new types, only integer values. The discussion section will explore the difference further. Discrimination principle Use unique values to describe a fixed number of possible cases. For classification of data abstractions with varying features, use inheritance
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有