Object orientation - 3 Review of Tutorial 2 Encapsulation Reusable Types Inheritance
Tutorial 2 System installation - packaging: –Release of my database to you was rather simple - just copy these files and then run these scripts in Oracle and create this page –But what if we had to deliver this application to a customer to be installed on their system? Changing the application - dependencies: –Modifying the procedures isn’t enough- you have to recompile them because they run in the database –you may have to drop types and tables as well - all in the right order An answer to the last part on the web
Encapsulation -1 in Oracle, you can access the instance variables from outside: –target.pos.latitude.degrees but what if we decide we want to change the way data is stored –not degrees and minutes but –minutes only can’t change the attributes now because we would have to change all programs which use this type (clients) too
Encapsulation -2 Better to hide the attributes, and only allow access via functions –target.position.latitude.getDegrees() –target.position.latitude.getMinutes() now can change the internal storage withou affecting clients This is ENCAPSULATION Not possible in Oracle - all attributes ‘public’ Java has better access control and multiple constructors
Types and Records The boat type can be simply implemented in a Relational table as follows Boat typeBoat Record name : varcharname : Varchar degrees : number latitude :dm minutes : real pos: latlong degrees : number longitude :dm minutes : real But not all types can be mapped onto a fixed record. We also need variable length records.
Reusable Types The types we have created are useful in any application dealing with points on the earth’s surface These are re-usable - beyond the Mayday application Reusable types –speed up development –should be fully tested –make systems easier to understand but sometimes we have to work to make a good reusable type ….
0 0 < 1Calm Light air Light breeze Gentle breeze Moderate breeze Fresh breeze Strong breeze Near gale Gale Strong gale Storm Violent storm 1212>= 64Hurricane Beaufort Scale
Degree classification fail pass rd st
Using a Relational DB Grade table class min max fail034 pass3539 3rd4049 Student table name mark fred38 sally57 Getting the class select name, class from student, grade where mark between min and max;
Common problems Converting a wind speed in Beaufort scale –25 knots is Force 6 Converting average mark on degree to an Honours classification –56 marks is a 2.1
fail pass 3rd st Honours Grades topmark mark class classInterval class classlist classification name
create type classinterval as object ( topmark number, class varchar(50) ); create type classList as varray(20) of classinterval; create type classification as object ( name varchar(50), steps classList, member function getclass(mark number) return varchar); create type body classification as member function getclass(mark number) return varchar is begin for i in 1..steps.count loop if (mark <= steps(i).topmark) then return steps(i).class; end if; end loop; return null; end;
Creating a classification object create table Class of classification; insert into Class values ( 'Honours Grades', classList( classInterval(34,'fail'), classInterval(39,'pass'), classInterval(49,'3rd'), classInterval(59,'2.2’), classInterval(69,'2.1'), classInterval(100,'1st') ) );
Reusable Types This Classification type is OK for Honours grades But can we use it for Beaufort scale too?. Three approaches –Use Classification type (but the names will all be wrong) –Create another similar type with ‘wind’ name (wasted work) –Generalise the Classification type with more neutral names (can be hard to find good names and not so readable) We can generalise the names –classInterval > step –topmark > limit –class > value –classList > stepList –classification > stepFunction –getClass() > getValue()
Reusable Type
Where can we use this type Honours grades Beaufort scale Salary grades Discount rates for multiple purchase Tax rate bands...
Inheritance We want to add reports to the boats application. Every report will contain a date, a time, and (usually) a position Specific reports include: –position reports with speed and direction –weather reports with wind speed and direction –warnings with position and time of observation –trouble report with nature and severity of problem
Possible solutions Have separate tables for each type of report –hard to create a chronological log of all reports Have one table, with every possible attribute –big rows, lots of unused columns, hard to know which ones are supposed to be relevant Define a common type and specialised sub- types
Defining a type and subtype create or replace type Report as object( boatname varchar(50), datetime date, position latlong) not instantiable not final ; / show errors create or replace type warningReport under Report( obsdatetime date, obsPosition latlong ); / abstract supertype subtype
Subtype inherits the types attributes warningReport attributes boatname varchar(50), datetime date, position latlong obsdatetime date, obsPosition latlong Inherited from Report Specific to warningReport
Create log and insert reports create table log of report; insert into log values (warningreport( 'Perdika', to_date('03-feb-12:17:40','yy-mon-dd:hh24:mi'), latlong(dm(34,23),dm(21,00)), to_date('03-feb-10:11:50','yy-mon-dd:hh24:mi'), latlong(dm(33,10),dm(20,00)) ) ); insert into log values (positionreport( 'Perdika', to_date('03-feb-12:17:45','yy-mon-dd:hh24:mi'), latlong(dm(34,23),dm(21,00)), 25, 90) );
Next week Tutorial –work with tutor to design some of the improvements - –draw a diagram –don’t need to implement Lecture –Association –Inheritance of functions –Polymorphism