UVA CS 655 - Data Abstraction and Encapsulation

Unformatted text preview:

Data Abstraction and EncapsulationParnas's PrinciplesData AbstractionSimple - Data OnlyInformation HidingSimple Objects(cont) Simple ObjectsObject Classes(cont) Object ClassesAbstract Types - MotivationAbs Types - (cont) MotivationAbstract Types - ExampleAbs Types (cont) ExampleAnother Abstract TypeAnother Abstract Type (cont)Using directory_typeObjects and Abstract TypesGenericsMore GenericsUsing queue_classGenerics with Dependent TypesSorting (body)Using GenericArguments in AdaData Abstraction and Encapsulation•Key research:–Dennis & Van Horn CACM, March '66•first notion of abstract types and capabilities–Parnas: "On the Criteria to be Used in Decomposing Systems into Modules." CACM, Dec '72–Liskov: CLU–Wulf/Shaw: Alphard–Lampson: Euclid–Good: GYPSY–Xerox: Mesa–Hoare/Brinch Hansen: monitors–Wirth: Modula & Modula-2–Honeywell-Bull: AdaParnas's Principles•One must provide the intended user with all the information needed to use the module and nothing more.•One must provide the implementer with all the information needed to complete the module and nothing more.Data Abstraction•Emphasis on behavior (operations capture behavior)•Implementation is not important to user of abstraction•In order to support data abstraction, language must provide:–linguistic construct•permits abstraction to be implemented as a unit•supports representation specification–ways to limit access to object•access should only be to defined operations•no access to representation--> clusters, modules, forms, packages, classes...Simple - Data Onlypackage earth is type continent is (Africa, Antarctica, Asia, Australia, Europe, NorthAmerica,SouthAmerica); radius: constant float:= 6.4e6; area: constant array (continent) of float:= (30.3e9, 13.0e9, 43.3e9, 7.7e9, 10.4e9, 24.9e9, 17.8e9); population : array (continent) of integer;end earth;Logical clusteringInformation Hidingpackage trig is function sin(x: float) return Float; function cos(x: float) return Float;end trig;package body trig is pi: constant Float:= 3.14159; function norm(x: float) return float is ...; - return x modulo 2*pi function sin(x: float) return float is ...; --return the sine of norm(x) function cosine (x: float) return float is ...; --return the cosine of norm(x)end trigExporting functions onlyImplementation is hiddenSimple Objectspackage directory_object is procedure insert(newname : in Name; newnumber : in Number); procedure lookup(oldname : in Name; oldnumber : out Number; found : out Boolean);end directory_object;package body directory_object is type Dirnode; type Dirptr is access Dirnode; type Dirnode is record entryname : Name; entrynumber : Number; left, right : Dirptr; end record; root: dirptr;Implements a directory- Creates one directory- exports two procs that can operate on directory- directory itself hidden from access(cont) Simple Objects procedure insert(newname : in Name; newnumber : in Number) is ... -- add new newname, newnumber to the directory procedure lookup(oldname : in Name; oldnumber : out Number; found : out Boolean); ... -- find oldname entry in directorybegin ...; -- initialize the directoryend directory_object; - - - - - - - - - - - - - - - - - - directory-object.insert (me, 41039);...directory_object.lookup (me, mynumber, ok);Operations user can perform on directoryObject Classesgeneric package directory_class is procedure insert(newname: in Name; newnumber: in Number); procedure lookup(oldname: in Name; oldnumber: out Number; found: out Boolean);end directory_class;package body directory_class is type Dirnode; type Dirptr is access Dirnode; type Dirnode is record entryname : Name; entrynumber : Number; left, right : Dirptr; end record; root: dirptr;Describes an instance of a directory(cont) Object Classes procedure insert(newname: in Name; newnumber: in Number) is ... -- add new newname, newnumber to the directory procedure lookup(oldname: in Name; oldnumber: out Number;found: out Boolean); ... -- find oldname entry in directorybegin ... -- initialize the directoryend directory_class; - - - - - - - - - - - - - - - - - - package homedir is new directory_classpackage workdir is new directory_classworkdir.insert (me, 41039);homedir.insert (me, 30570);workdir.lookup (me, mynumber, ok);Derived objectsNo notion of inheritanceAbstract Types - Motivationdatatype rational = rat of (int * int);val zero = rat (0, 1);and one = rat (1, 1);fun op ++ (rat(m1, n1): rational, rat(m2, n2): rational) = rat (m1*n2 + m2*n1, n1*n2) - - - - - - - - - - - - - - - - - - - - - - - rat(3,2) --creates rational number 3/2rat(6,4) --creates rational number 6/4Abs Types - (cont) Motivationif one ++ rat(1,2) = rat (6,4) then ... else ...rat(0,0) -- no correspondingrat(1,0) -- rational numbers! - - - - - - - - - - - - - - - - - - - - - - - - Have: Rational = {rat(m,n) | m,n integer}Desire: Rational = {rat(m,n) | m,n integer; n>0; m,n have no common factor} -- a type based on a (representation) type that does not exist -- in common languages.abstract type: based on a set of operations: constants, functions and procedures -- values defined only indirectly.Won’t test =although it shouldAbstract Types - Exampleabstype rational = rat of (int * int);with val zero = rat (0, 1); and one = rat (1, 1); fun op // (m: int, n: int) = if n <> 0 then rat(m,n) else ... -- invalid rational number and op ++ (rat(m1, n1): rational, rat(m2, n2): rational) =rat (m1*n2 + m2*n1, n1*n2)Abs Types (cont) Example and op == (rat(m1, n1): rational, rat(m2, n2): rational) =(m1*n2 = m2*n1) and float (rat(m,n): rational) = m / nend - - - - - - - - - - - - - - - - - - - - - - - val h = 1//2 --creates rational number 1/2val i = 6//4 --creates rational number 6/4if one ++ h = = i then ... else ... -- now this worksAnother Abstract Typepackage directory_type is type Directory is limited private; procedure insert(dir: in out Directory; newname: in Name;newnumber : in Number); procedure lookup(dir: in Directory; oldname: in Name;oldnumber: out Number; found: out Boolean);private type Dirnode; type Directory is access Dirnode; type Dirnode is record ...


View Full Document

UVA CS 655 - Data Abstraction and Encapsulation

Download Data Abstraction and Encapsulation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Abstraction and Encapsulation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Abstraction and Encapsulation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?