Object Oriented Software Construction - Bertrand Meyer
1370 pág.

Object Oriented Software Construction - Bertrand Meyer


DisciplinaProgramação Orientada A Objetos3.516 materiais53.535 seguidores
Pré-visualização50 páginas
by itself as a unit or reuse. A self-sufficient reusable module would
e a set of routines, one for each of the operations cited \u2014 creation, insertion,
ching. 
a forms the basis for a form of module, the \u201cpackage\u201d, found in what may be
apsulation languages: Ada, Modula-2 and relatives. More on this below. 
ation Variation 
rn is very general; there is in practice, as we have seen, a wide variety of
ta structures and algorithms. Such variety indeed that we cannot expect a
 to take care of all possibilities; it would be enormous. We will need a family
 cover all the different implementations. 
al technique for producing and using reusable modules will have to support
 module family. 
tion Independence 
m of reusable module should enable clients to specify an operation without
 it is implemented. This requirement is called Representation Independence.
 that a client module C from a certain application system \u2014 an asset
program, a compiler, a geographical information system\u2026 \u2014 needs to
ether a certain element x appears in a certain table t (of investments, of
words, of cities). Representation independence means here the ability for C
 information through a call such as 
:=
 has (t, x)
\u201cGenericity\u201d, pag
96.
§4.6 FIVE REQUIREMENTS ON MODULE STRUCTURES 85
without knowing what kind of table
 t is at the time of the call. C\u2019s author should only need
to know that t is a table of elements of a certain type, and that x denotes an object of that
type. Whether t is a binary search tree, a hash table or a linked list is irrelevant for him; he
should be able to limit his concerns to asset management, compilation or geography.
Selecting the 
the table man
This req
when they cre
after that, non
kind of table 
t from one of 
an abstract ha
You m
Information H
decisions wi
Independence
clients agains
a much small
the run-time f
Satisfyin
encountered 
away from m
if \u201ct is a
\u201cA
elseif \u201ct
\u201cA
elseif
(et
end
It would
(we cannot re
future variant
choice compl
underlying ru
of the object-
Factoring O
If Representa
ignore interna
Common Beh
developers of
that may exis
\u201cInformation Hid-
ing\u201d, page 51.
\u201cSingle Choice\u201d, 
page 61.
\u201cDYNAMIC BIND-
ING\u201d, 14.4, page 
480.
appropriate search algorithm based on t\u2019s implementation is the business of
agement module, and of no one else. 
uirement does not preclude letting clients choose a specific implementation
ate a data structure. But only one client will have to make this initial choice;
e of the clients that perform searches on t should ever have to ask what exact
it is. In particular, the client C containing the above call may have received
its own clients (as an argument to a routine call); then for C the name t is just
ndle on a data structure whose details it may not be able to access.
ay view Representation Independence as an extension of the rule of
iding, essential for smooth development of large systems: implementation
ll often change, and clients should be protected. But Representation
 goes further. Taken to its full consequences, it means protecting a module\u2019s
t changes not only during the project lifecycle but also during execution \u2014
er time frame! In the example, we want has to adapt itself automatically to
orm of table t, even if that form has changed since the last call. 
g Representation Independence will also help us towards a related principle
in the discussion of modularity: Single Choice, which directed us to stay
ulti-branch control structures that discriminate among many variants, as in 
n array managed by open hashing\u201d
 then
pply open hashing search algorithm\u201d
 is a binary search tree\u201d then
pply binary search tree traversal\u201d
c.)
 be equally unpleasant to have such a decision structure in the module itself
asonably expect a table management module to know about all present and
s) as to replicate it in every client. The solution is to hide the multi-branch
etely from software developers, and have it performed automatically by the
n-time system. This will be the role of dynamic binding, a key component
oriented approach, to be studied in the discussion of inheritance. 
ut Common Behaviors
tion Independence reflects the client\u2019s view of reusability \u2014 the ability to
l implementation details and variants \u2013, the last requirement, Factoring Out
aviors, reflects the view of the supplier and, more generally, the view of
 reusable classes. Their goal will be to take advantage of any commonality
t within a family or sub-family of implementations. 
APPROACHES TO REUSABILITY §4.686
The variety of implementations available in certain problem areas will usually
demand, as noted, a solution based on a family of modules. Often the family is so large
that it is natural to look for sub-families. In the table searching case a first attempt at
classification might yield three broad sub-families: 
\u2022 Tables m
\u2022 Tables o
\u2022 Tables m
Each of
significant co
sequential im
their original 
Possible
file. But reg
managed tab
indicating the
the searching
has (t: S
do
en
s
H
TA
ll 
he 
anaged by some form of hash-coding scheme. 
rganized as trees of some kind. 
anaged sequentially.
 these categories covers many variants, but it is usually possible to find
mmonality between these variants. Consider for example the family of
plementations \u2014 those in which items are kept and searched in the order of
insertion.
 representations for a sequential table include an array, a linked list and a
ardless of these differences, clients should be able, for any sequentially
le, to examine the elements in sequence by moving a (fictitious) cursor
 position of the currently examined element. In this approach we may rewrite
 routine for sequential tables as: 
EQUENTIAL_TABLE; x: ELEMENT): BOOLEAN is
-- Is there an occurrence of x in t?
from start until
after or else found (x)
loop
forth
end
Result := not after
d
Some possible 
table 
implementation
SEQUENTIAL_
TABLE
ARRAY_
TABLE
LINKED_
TABLE
FILE_
TABLE
TABLE
ASH_
BLE
TREE_
TABLE
\u201cACTIVE DATA 
STRUCTURES\u201d, 
23.4, page 774, wi
explore details of t
cursor technique.
§4.6 FIVE REQUIREMENTS ON MODULE STRUCTURES 87
This form relies on four routines which any sequential table implementation will be
able to provide: 
\u2022 start, a command to move the cursor to the first element if any. 
\u2022 forth, a 
course o
\u2022 after, a 
element
\u2022 found (x
value
 x.
At first 
the general r
searching in a
more; it is a t
illustrate ob
implementati
compile and e
For each
the cursor. Th
The firs
Then you may
(The last valu
The seco
a reference fir
you may repr
Sequential 
structure with 
cursor
1
v1
The general routine 
pattern was on 
page 82.
Array 
representation 
of sequential 
table with 
cursor
1
v1
command to advance the cursor by one position. (Support for forth is of
ne of the prime characteristics of a sequential table implementation.) 
boolean-valued query to determine if the cursor has moved past the last
; this will be true after a start if the table was empty. 
), a boolean-valued query to determine if the element at cursor position has
 
sight, the routine text for has at the bottom of the preceding page resembles
outine pattern used at the beginning of this discussion, which covered
ny table (not just sequential). But the new form is not a routine pattern any
rue routine, expressed in a directly executable notation (the notation used to
ject-oriented concepts in part C of this book). Given appropriate
ons for the four operations start, forth, after and found which it calls, you can
xecute the latest form of has. 
 possible sequential table representation you will need a representation for
ree example representations are by an array, a linked list and a file.
t