1 / 77

Data Abstraction

Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes. Data Abstraction. Created: 19 August 2004. Abstraction. Maxim Simplicity is good; complexity is bad. Abstraction Concentrating on the essentials and ignoring the details

sorrentino
Download Presentation

Data Abstraction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Engr 691Special Topics in Engineering Science Software ArchitectureSpring Semester 2004Lecture Notes

  2. Data Abstraction Created: 19 August 2004

  3. Abstraction • Maxim • Simplicity is good; complexity is bad. • Abstraction • Concentrating on the essentials and ignoring the details • Sometimes described as "remembering the what and ignoring the how". • Most effective weapon in fight against complexity 1

  4. System Large and complex Simple with complexity inside Module Module Module Abstraction 2

  5. Two Kinds of Abstraction • Procedural abstraction • Separate logical properties of an action from details of how the action is implemented • Use when developing an algorithm following the top-down approach • Use when coding task in programming language, make each task a procedure • Data abstraction • Separate logical properties of data from details of how data are represented • Focus on problem’s data rather than tasks 3

  6. Concrete Data Structure • Structure and values of custom data types defined by programmer are known to other parts of the program • In Pascal or C, all data structures are visible • E.g., a collection of records about the employees of a company • store records in a global Pascal or C array • the array and all its elements are visible to all parts of program • any statement in program can directly access and modify elements of the array 4

  7. Abstract Data Structure • A module consisting of data and operations • Data are hidden within module and can only be accessed by means of operations • Its name and interface are known but not implementation • Operations are explicitly given; values are defined implicitly by means of operations • Support information hiding • Related concept: encapsulation • data and the operations that manipulate the data are combined in a module 5

  8. Abstract Data Structure (cont.) • State • Manipulated by the operations. • A value, or collection of information, held by abstract data structure • Examples • E.g., stack • operations push(), pop(), and empty() for accessing and manipulating • disallow direct access to concrete data structure that implements stack. • implementation might use an array, a linked list, or some other concrete data structure • actual implementation is "hidden" from the user of the stack 6

  9. Abstract Data Structure (cont.) • E.g., a collection of records about the employees of a company • constraints • only allow collection of records to be accessed through a small group of procedures • array of records can be manipulated directly inside group • other parts of program must use one of the procedures in group to manipulate records in collection • originally implemented with an array hidden behind the interface • modifications of program, such as • change the implementation from an array to a linked list • move the collection to a disk file By approaching the design of the collection as an abstract data structure, • limited the parts of the program changed to the small group of procedures that used the array directly • other parts of the program are not affected. 7

  10. Type and Concrete Data Type • Type • Category of entities sharing common characteristics • Variable of type specified as • a value (state) drawn from some set (domain) of possible values • a set of operations that can be applied to those values • Concrete data type • values are most prominent features • values and their representations are explicitly prescribed • operations on the values are implicit 8

  11. Abstract Data Type (ADT) • Set of abstract data structures all with same domain of possible states and set of operations • Operations are explicitly prescribed • Values are defined implicitly in terms of the operations • Instance of the ADT • Particular abstract data structure from an ADT • Language support • C and Pascal not directly support ADTs • C++ and Java have class construct to directly define ADTs 9

  12. Defining ADTs To specify an ADT, need • Name of the ADT • Sets (or domains) upon which it is built • Type being defined • Auxiliary types (e.g., primitive data types and other ADTs) used as parameters or return values of the operations. • Signatures (syntax or structure) of the operations. • name • input sets (i.e., the types, number, and order of the parameters) • output set (i.e., the type of the return value) 10

  13. Defining ADTs • Semantics (or meaning) of the operations Two primary approaches for specifying • Axiomatic (or algebraic) approach • set of logical rules (properties or axioms) that relate the operations to one another • meanings of the operations are defined implicitly in terms of each other • more elegant but difficult to apply • Constructive (or abstract model) approach • meaning of the operations explicitly in terms of operations on other abstract data types • underlying model may be any well-defined mathematical model or a previously defined ADT • more useful 11

  14. Defining ADTsAxiomatic Specification of Unbounded Stack ADT • Name • Stack (of Item) • Item represents the arbitrary unspecified type for the entities stored in the stack ; A formal generic parameter • Sets • Stack • set of all stack instances • Item • set of all items that can appear in a stack instance • boolean • primitive Boolean type { False, True } 12

  15. Defining ADTsAxiomatic Specification of Unbounded Stack ADT • Signatures • Constructor • constructs and initializes an instance of the ADT • create: Stack • Mutator • returns the instance with its state changed • push: (Stack, Item) Stack pop: Stack Stack • Accessor • returns information from the state of an instance without changing the state • top: Stack Item empty: Stack boolean • Destructor • destroys an instance of the ADT • destroy: Stack 13

  16. Defining ADTsAxiomatic Specification of Unbounded Stack ADT • Semantics • Axioms for Stack ADT s: instance of type Stack ; x: entity of type Item • top(push(s,x)) = x • precondition: not empty(S) • pop(push(s,x)) = s • precondition: not empty(S) • empty(create()) = True empty(create()) • empty(push(s,x)) = Falsenot empty(push(s,x)) rewrite rewrite 14

  17. Defining ADTsConstructive Specification of Bounded Stack ADT • Name • StackB (of Item) • Sets • StackB: • set of all stack instances • Item: • set of all items that can appear in a stack instance • boolean: • primitive Boolean type • int: • primitive integer type { ..., -2, -1, 0, 1, 2, ... } 15

  18. Defining ADTsConstructive Specification of Bounded Stack ADT • Signatures • Constructor • create: int StackB • Mutator • push: (StackB, Item) StackB pop: StackB StackB • Accessor • top: StackB Item empty: StackB boolean full: StackB boolean • Destructor • destroy: StackB 16

  19. Defining ADTsConstructive Specification of Bounded Stack ADT • Semantics • Precondition • logical assertion that specifies required characteristics of the values of arguments • Postcondition • logical assertion that specifies characteristics of the result computed by the operation with respect to the values of the arguments. • Invariant • Interface invariants • states publically accessible features and abstract properties of the ADT instance • Implementation (representation) invariants • gives the required relationships among the internal data fields of the implementation 17

  20. Defining ADTsConstructive Specification of Bounded Stack ADT • Semantics of bounded Stack ADT • create (int size) StackB S’ • precondition • size >= 0 • postcondition • S' is a valid new instance of StackB && S' has the capacity to store size items && empty(S’) 18

  21. Defining ADTsConstructive Specification of Bounded Stack ADT • Semantics of bounded Stack ADT • push(StackB S, Item I) StackB • precondition • S is a valid StackB instance && not full (S) • postcondition • S' is a valid StackB instance && S' = S with I added as the new top • pop(StackB S) StackB S' • precondition • S is a valid StackB instance && not empty(S) • postcondition • S' is a valid StackB instance && S' = S with the top item deleted 19

  22. Defining ADTsConstructive Specification of Bounded Stack ADT • top(StackB S) Item I • precondition • S is a valid StackB instance && not empty(S) • postcondition • I = the top item on S • empty(StackB S) boolean e • precondition • S is a valid StackB instance • postcondition • e is true iff S contains no elements (i.e., is empty) 20

  23. Defining ADTsConstructive Specification of Bounded Stack ADT • full(StackB S) boolean f • precondition • S is a valid StackB instance • postcondition • f is true iff S contains no space for additional items (i.e., is full) • destroy(StackB S) • precondition • S is a valid StackB instance • postcondition • StackB S no longer exists 21

  24. Java ClassesClass and Instance Methods • Class method • Associated with class as a whole, not with any specific instance • Has Keyword static • public static void main(String[] args) { // beginning code for the program } • Instance method • Associated with an instance of the class • No keyword static • public void pop() { // code for pop operation } 22

  25. Java ClassesClass and Instance Variables • Class variable • Associated with class as a whole; only one copy of the variable for entire class • With keyword static • Instance variable • Associated with an instance of the class; each instance has its own instance of the variable • With no keyword static 23

  26. Java ClassesPublic and Private Accessibility • public components are accessible from anywhere in the program • Associated with the class as a whole; there is only one copy of the variable for the entire class • With keyword static • Instance variable • Associated with an instance of the class; each instance has its own instance of the variable • With no keyword static 24

  27. Java ClassesPrimitive or Reference Variables A Java variable is a strongly typed container in memory that is declared to hold either • A value of the associated primitive data type such as integers (int), floating point numbers (double), booleans (boolean), and single characters (char) • A reference to (i.e., memory address of) an instance of the associated class (or other reference) type 25

  28. Implementing ADTs as Java Classes 1.Use Java class construct to represent entire ADT To allow access to class from anywhere in program, make the class public • Example public class StackB { // implementation of instance methods //and data here } 26

  29. Implementing ADTs as Java Classes 2. Use instance of Java class to represent instance of ADT Use variables of the class type to hold references to instances • Example • StackB stk; 27

  30. Implementing ADTs as Java Classes 3. As each component of class defined, ensure that semantics of ADT operations are implemented appropriately • Appropriate implementation (representation) invariant to capture what it means for internal state of instance to be valid • Interface and implementation invariants established (i.e., made true) by constructors and preserved (i.e., kept true) by mutator and accessor methods • Method's postcondition is established by method in any circumstance when called with precondition true • Class and its methods should be documented with invariants, preconditions, and postconditions 28

  31. Implementing ADTs as Java Classes 4. Represent ADT's constructors by Java constructor methods Often include a parameterless default constructor • Constructor is • Method with same name as class • No return type specified • Normally invoked by Java operator new • Example • public class StackB { public StackB(int size) { // initialization code } // rest of StackB methods and data ... } • StackB stk = new StackB(100); 29

  32. Implementing ADTs as Java Classes 5. Represent ADT operations by instance methods of class • State of ADT instance becomes implicit argument of all method calls • Apply method to class instance by using selector (i.e., "dot") notation • Example • push an item x onto stk if (!stk.full()) stk.push(x); • examine top item and remove it if (!stk.empty()) { it = stk.top(); stk.pop(); } 30

  33. Implementing ADTs as Java Classes 6. Make the constructors, mutators, accessors, and destructors public methods of the class 31

  34. Implementing ADTs as Java Classes 7. Represent ADT mutator operations by Java procedure (i.e., void) methods, except those that explicitly require new instances to be generated (e.g., a copy or clone operation) • Example public void pop() { // code to implement operation } 32

  35. Implementing ADTs as Java Classes 8. For certain mutator operations (e.g., copy or clone), implement Java methods to return new instances rather than modify the current instance 33

  36. Implementing ADTs as Java Classes 9. Represent ADT accessor operations by Java function methods of proper return type • Example public boolean empty() { // code to implement operation } 34

  37. Implementing ADTs as Java Classes 10. If necessary for deallocation of internal resources, represent the ADT destructor methods by explicit Java procedures Normally just use automatic garbage collection • Example public void destroy() { // code to free resources } • Java framework allows finalize() method to be called implicitly whenever garbage collector runs 35

  38. Implementing ADTs as Java Classes 11. Use private data fields to represent encapsulated state of instance needed • Example public class StackB { // public operations of class instance // encapsulated data fields of class instance private int topItem; // Pointer to next index for insertion private int capacity; // Maximum number of items in stack private Object[] stk; // the stack } 36

  39. Implementing ADTs as Java Classes 12. Do not use public data fields in the class • public data fields violate the principle of information hiding • Introduce appropriate accessor and mutator methods to allow manipulation of the hidden state 13. Include, as appropriate, private methods to aid in implementation 37

  40. Implementing ADTs as Java Classes 14. Add any other methods needed to make the ADT fit into the Java environment • Add publictoString method • returns a Java String reflecting the "value" of instance in a format suitable for printing • Add publicclone method • creates new instance that has same value as current instance 38

  41. Implementing ADTs as Java Classes 15. In general, avoid use of class (i.e., static) variables • Good programming practice to use class constants where appropriate • data fields declared with both static and final modifiers • values may be initialized but cannot be changed • Constants may be declared private if usage restricted to class or public if users of class also need access • Example • public static final int SUNDAY = 1; 39

  42. Java Implementation of the Bounded Stack // A Bounded Stack ADT public class StackB { // Interface Invariant: Once created and until destroyed, this // stack instance has a valid and consistent internal state public StackB(int size) // Pre: size >= 0 // Post: initialized new instance with capacity size && empty() { stk = new Object[size]; capacity = size; topItem = 0; } 40

  43. Java Implementation of the Bounded Stack public void push(Object item) // Pre: not full() // Post: item added as the new top of this instance's stack { stk[topItem] = item; topItem++; } public void pop() // Pre: not empty() // Post: item at top of stack removed from this instance { topItem--; stk[topItem] = null; } 41

  44. Java Implementation of the Bounded Stack public Object top() // Pre: not empty() // Post: return item at top of this instance's stack { return stk[topItem-1]; } public boolean empty() // Pre: true // Post: return true iff this instance's stack has no elements { return (topItem <= 0); } 42

  45. Java Implementation of the Bounded Stack public boolean full() // Pre: true // Post: return true iff this instance's stack is at full capacity { return (topItem >= capacity); } public void destroy() // Pre: true // Post: internal resources released; stack effectively deleted { stk = null; capacity = 0; topItem = 0; } 43

  46. Java Implementation of the Bounded Stack // Implementation Invariants: 0 <= topItem <= capacity // stack is in array section stk[0..topItem-1] // with the top at stk[topItem-1], etc. private int topItem; // Pointer to next index for insertion private int capacity; // Maximum number of items in stack private Object[] stk; // the stack } 44

  47. Better Approach to Implementing ADTs in Java Ι. Define Java interface that specifies type signatures for ADT's mutator and accessor (and, if needed, destructor) operations 45

  48. Better Approach to Implementing ADTs in Java II. Specify and document interface by interface invariants, preconditions, and postconditions that must be supported by any implementation • Example public interface StackADT { // Interface Invariant: Once created and until destroyed, this // stack instance has a valid and consistent internal state public void push(Object item); // Pre: not full() // Post: item added as the new top of this instance's stack ... public Object top(); // Pre: not empty() // Post: return item at top of this instance's stack ... } 46

  49. Better Approach to Implementing ADTs in Java III. Provide one or more concrete classes that implement the interface • Example public class StackInArray implements StackADT { // Interface Invariant: Once created and until destroyed, this // stack instance has a valid and consistent internal state public StackInArray(int size) // Pre: size >= 0 // Post: initialized new instance with capacity size && empty() { stk = new Object[size]; capacity = size; topItem = 0; } 47

  50. Better Approach to Implementing ADTs in Java public void push(Object item) // Pre: not full() // Post: item added as the new top of this instance's stack { stk[topItem] = item; topItem++; } ... public Object top() // Pre: not empty() // Post: return item at top of this instance's stack { return stk[topItem-1]; } ... // Implementation Invariants: 0 <= topItem <= capacity // stack is in array section stk[0..topItem-1] with the top at stk[topItem-1] private int topItem; // Pointer to next index for insertion private int capacity; // Maximum number of items in stack private Object[] stk; // the stack } 48

More Related