SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented...

20
SWE 4743 Abstract Data Types Richard Gesick

description

SWE Abstract Data Types Even if a class doesn’t implement a pre-defined interface, it still defines an ADT as embodied in its public interface –In this case, the ADT definition is combined with its implementation Explicit interfaces are used if we expect to have multiple implementations of the ADT –interface List, class ArrayList, class LinkedList, … If we expect an ADT to have only one implementation, we often combine the ADT definition and implementation in the same class

Transcript of SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented...

Page 1: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743

Abstract Data Types

Richard Gesick

Page 2: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 2-20

Abstract Data Types• Object-oriented design is based on the

theory of abstract data types• Domain and implementation concepts

are modeled in software as ADTs• Interfaces capture the essence of an

ADT• Classes provide implementations of

ADTs

Page 3: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 3-20

Abstract Data Types• Even if a class doesn’t implement a pre-defined interface,

it still defines an ADT as embodied in its public interface– In this case, the ADT definition is combined with its

implementation• Explicit interfaces are used if we expect to have multiple

implementations of the ADT– interface List, class ArrayList, class LinkedList, …

• If we expect an ADT to have only one implementation, we often combine the ADT definition and implementation in the same class

Page 4: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 4-20

Defining ADTs• ADT = syntax + semantics• Each ADT operation has: name, parameter list, return type• ADT clients must conform to this syntax, or they will fail to

compile• Each ADT operation also has semantics: What is the

meaning of the operation? What does it do?• Classes that implement ADTs must faithfully adhere to

operation semantics as well as syntax– What would happen if ArrayStack’s push implementation fires a

nuclear missile rather than pushing a value on the stack?

Page 5: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 5-20

Defining ADTs• Source code precisely defines ADT syntax

• Compilers enforce ADT syntax

• Source code does not precisely define semantics

• Compilers cannot enforce semantics

• ADT semantics are typically defined by comments in the code, if they’re defined at all

• Comment each operation explaining what it does

Page 6: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 6-20

Defining ADTs• Imprecise or incomplete definitions of ADT

semantics lead to reliability problems:– Clients and implementers have different ideas

of what an operation does– Differing assumptions lead to defects

• Reliability is even more important in object-oriented programming than elsewhere.– Why?

Page 7: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 7-20

Design By Contract• DBC is a technique for more precisely

defining ADT semantics, thus preventing misunderstandings

• DBC is based on the real-world notion of a legal contract– A contract involves a “client” and a “supplier”– Each side has obligations and expected benefits,

which are precisely defined in the contract– If a party performs their obligations, they are

guaranteed to receive the promised benefits

Page 8: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 8-20

Defining operation semantics:Pre-conditions & Post-conditions

• An ADT is a contract between client and supplier

• Each operation has Pre-conditions and Post-conditions

• Pre-conditions are the client’s obligations• Post-conditions are the supplier’s obligations• The pre-conditions and post-conditions

define the semantics of the operation

Page 9: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 9-20

Defining operation semantics:Pre-conditions & Post-conditions

• If a client invokes an operation having satisfied all pre-conditions, the supplier must ensure that all post-conditions are met upon return

• If the client did not satisfy all pre-conditions, the supplier is under no obligation to satisfy the post-conditions. The supplier could:– Return an error or throw an exception– Just fail (crash, return bad results, etc.)

Page 10: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 10-20

Exceptions• What if the caller satisfied the pre-conditions, but

for some reason the supplier is unable to satisfy the post-conditions?– The supplier throws an exception or returns an error– Why might a supplier fail to satisfy the post-

conditions?– External factors beyond supplier’s control (hard disk

crash, Internet down, etc.)• If the client fails to meet pre-conditions, the supplier can

throw an exception or return an error if it wants to, but isn’t required to

Page 11: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 11-20

Exceptions• If an operation could throw an exception or return an

error, the contract should explicitly define the possible exceptions/errors so clients can handle them

Page 12: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 12-20

Class Invariants• Pre and Post-conditions apply only to

single operations• Some supplier obligations apply to every

operation on the ADT• Class Invariants are post-conditions that

apply to every operation

Page 13: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 13-20

Class Invariants• What is a constructor’s job?• Constructors must establish all class invariants

– When a constructor completes, all class invariants must be satisfied– If a constructor cannot establish the class invariants, it should throw

an exception

• In addition to their post-conditions, public operations must also ensure that all class invariants are satisfied upon return– I.E., the class invariants are ANDed with the post-conditions of every

public operation

• Class invariants may be temporarily violated while a public operation is executing, but they must be reestablished before the operation returns

Page 14: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 14-20

Class Invariants• An ordered list is a good example of an ADT with which invariants, preconditions, and postconditions can be demonstrated. An invariant of an ordered list is that both before and after any operation, the list is ordered. This may seem obvious, but work is required to maintain the list order during insertion and removal.

Page 15: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 15-20

Class InvariantsAnd, depending on implementation, the list may not be ordered at some point during an operation.• Consider insertion into an ordered list implemented

using a C++ array. If insertion involves appending the new item and then sorting the list, then during the insertion operation the list will not be ordered.

• This does not violate the invariant, so long as the invariant is true when the operation concludes.

Page 16: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 16-20

Testing• How do you test?

– If statements– Assertions

• Guidelines– Do not use assertions on public interface methods– Do not use assertions to do any work your program requires for

correct operation– Use for

• Internal invariants, control flow invariants• Pre, post, invariants for private methods

• By default, assertions are disabled– Use –ea to enable assertions

Page 17: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 17-20

DBC vs. Defensive Programming• Defensive Programming says:

– Operation implementations should be bullet-proof– Check all parameters for validity before using them– Return error or throw exception if parameters are invalid, but

never crash• Puts heavy burden on the supplier• Results in lots of parameter checking code• Client and supplier often have redundant checks• Results in more code (harder to maintain)• Slows programs down (too much redundant checking)

Page 18: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 18-20

DBC vs. Defensive Programming• DBC says:

– Ensuring that pre-conditions are met is the client’s job– Operation implementations need not contain code to verify that

pre-conditions were met (e.g., no parameter checking code)– If pre-conditions are not met and something unseemly occurs, it is

the client’s fault, and they got what they deserved– Suppliers must throw an exception if post-conditions cannot be

met• Puts more burden on clients• Results in less and more efficient code• As a debugging tool, operation implementations may

include assert statements to verify that pre-conditions were met, but this is optional, and all assertions should be turned off in the final release of the software

Page 19: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 19-20

DBC vs. Defensive Programming• Designers must make a conscious choice

between Defensive Programming and DBC

Page 20: SWE 4743 Abstract Data Types Richard Gesick. SWE 4743 2-20 Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.

SWE 4743 20-20

Documenting ADTs with javadoc• Interfaces and classes

– Header comment– @invariant (custom tag)

• Operations– Header comment– @pre (custom tag)– @post (custom tag)– @param– @returns (this is really a post-condition)– @throws