












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Abstraction SWE 619 Fall 06 (c) Saket Kaushik 2 Agenda Motivation
Understand procedural abstractions
Specify procedural abstractions
SWE 619 Fall 06 (c) Saket Kaushik 3 Software construction
SWE 619 Fall 06 (c) Saket Kaushik 4 Software construction Need a methodology for
Code should be
SWE 619 Fall 06 (c) Saket Kaushik 7 Example 1: Merge Sort PROB: Sort an array in inc/dec order Example: sort [4,2,7,5,9,8] in inc. order
SWE 619 Fall 06 (c) Saket Kaushik 8 Example 1: Merge Sort Algo
= combine [2,4] and [7] Æ [2,4,7] Similarly, combine [5,9] and [8] Æ [5,8,9] Finally, combine [2,4,7] and [5,8,9] Æ [2,4,5,7,8,9]
SWE 619 Fall 06 (c) Saket Kaushik 9 Example 2: Play Problem: Create a play by assembling a group of writers. Decomposition: Each writer is given a list of characters, general plot and is required to write dialogues for one of the characters. Individual solutions may not combine properly! SWE 619 Fall 06 (c) Saket Kaushik 10 Abstraction Improper decomposition is harmful! Abstraction changes level of detail to be considered
Hence – simplifies analysis
SWE 619 Fall 06 (c) Saket Kaushik 13 Abstraction in programs Fundamental principal in program development
SWE 619 Fall 06 (c) Saket Kaushik 14 Example 3: Task Abstraction
//Calling program int iArray={1,2,5,6,8}; int var = (3*8)/4; int index = locate(iArray, var); // abstract from a task
SWE 619 Fall 06 (c) Saket Kaushik 15
SWE 619 Fall 06 (c) Saket Kaushik 16 Abstraction
Specification
SWE 619 Fall 06 (c) Saket Kaushik 19 Benefits of abstraction
Each implementation can be understood in isolation An abstraction can be used without having to understand how it is implemented; it can be implemented without having to understand how it is used.
One implementation can be substituted for another without disturbing the using programs SWE 619 Fall 06 (c) Saket Kaushik 20 Kinds of Abstraction Procedural Abstraction: Lecture 2
Data Abstraction: Lecture 4, Iteration Abstraction: Lecture 6 Type Hierarchy: Lecture 7 Polymorphic abstraction: Lecture 8
SWE 619 Fall 06 (c) Saket Kaushik 21 Abstraction Mechanisms
Abstracts identity of the data by replacing them with parameters Generalizes modules
Abstracts from the implementation details to the behavior users can depend on. Isolates modules from one another’s implementations. Language independence?? SWE 619 Fall 06 (c) Saket Kaushik 22 Abstraction by Parameterization Generalizes modules
Example: Write a program to add two integers –: 1441 and 984
public int add(int x, int y){return x+y;}
SWE 619 Fall 06 (c) Saket Kaushik 25 Abstraction by Specification Associate a specification with each procedure Meaning of procedure call is understood from this specification;not the body of the procedure No need to look at implementation to realize what the procedure is designed to accomplish SWE 619 Fall 06 (c) Saket Kaushik 26 Parts of a Procedural Specification
SWE 619 Fall 06 (c) Saket Kaushik 27 Example (Liskov fig. 1.3) float sqrt (float coef)
}
{ SWE 619 Fall 06 (c) Saket Kaushik 28 Procedures: an Abstraction
May accept inputs; usually produce output Side effects? For same inputs, same output? Output for all possible inputs?
By writing a procedure, you abstract from a task
SWE 619 Fall 06 (c) Saket Kaushik 31 Yet Another Example
SWE 619 Fall 06 (c) Saket Kaushik 32 Yet Another Example
SWE 619 Fall 06 (c) Saket Kaushik 33 Requires-Modifies-Effects Must be sufficiently informative Requires states conditions that must be true at the beginning of execution Effects describes the conditions that will hold after execution provided conditions stated in Requires were satisfied. Modifies: modifications to input parameters If Requires conditions were satisfied (by the caller), Effects conditions are guranteed (by the callee). Software contracts SWE 619 Fall 06 (c) Saket Kaushik 34 What does a Contract Look Like?
P is the precondition (Requires clause) Q is the postcondition (Effects clause) S is the program text
SWE 619 Fall 06 (c) Saket Kaushik 37 Defensive Programming (cont) Meyer: “Don’t do it” You should look at the context (who calls the procedure)
SWE 619 Fall 06 (c) Saket Kaushik 38 Designing Procedural Abstractions Simplicity Minimality
Underdetermined behavior
Deterministic implementation
Generality
SWE 619 Fall 06 (c) Saket Kaushik 39 Total vs. Partial Procedures A procedure is total if its behavior is specified for all possible inputs. A partial procedure always has a precondition. Partial procedures are less safe than total procedures.