Paul Ammann 2 Data Abstraction Abstract State Client State Representation State Internal State Methods behavior Constructors create objects Producers return immutable object Mutators change state ID: 775652
Download Presentation The PPT/PDF document " Abstraction Functions and Representatio..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Abstraction Functions and Representation Invariants
Paul Ammann
Slide22
Data Abstraction
Abstract State (Client State)
Representation State (Internal State)
Methods (behavior)
Constructors (create objects)
Producers (return immutable object)
Mutators (change state)
Observers (report about state)
This lecture is about
state
only
Slide33
What is “State”?
Key notion
Definition:
A state is an assignment of values to variables.
Need to consider all possible values for each variable
State space is cross product of possible values for each individual variable
Java example:
Variables:
List list;
int x;
Possible States:
list = [], x = 5
list = null; x = 0
list = [“cat”, “dog”]; x = -8
list = [“cat”, 1, null]; x = 0
etc.
Slide44
Motivation
Why hide implementation from client?
Makes reimplementation (maintenance) possible!
Protects implementation from client
Lecture covers two key notions
Abstraction function
Implemented by toString() in Java
Representation Invariant
Not standard in Java, but fits assertion mechanisms
Is available in C# (VisualStudio Code Contracts)
Slide55
Abstraction Function
Abstract state
What the client sees
Examples given in class
Representation state
What the implementation manipulates
Examples given in class
Abstraction function simply maps representation states to abstract states
Required for any implementation
Difference here – we’re documenting it!
Code examples: in-class exercises
Slide66
Rep Invariant
Rep invariant captures constraints on implementation variables
“Why my code works”
English descriptions are fine
But needs to be coded to be effective
Can check in Junit tests
Code examples: in-class exercises