Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object orientation - 3 Review of Tutorial 2 Encapsulation Reusable Types Inheritance.

Similar presentations


Presentation on theme: "Object orientation - 3 Review of Tutorial 2 Encapsulation Reusable Types Inheritance."— Presentation transcript:

1 Object orientation - 3 Review of Tutorial 2 Encapsulation Reusable Types Inheritance

2 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

3 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

4 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

5 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.

6 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 ….

7 0 0 < 1Calm 1 1 1 - 3Light air 2 2 4 - 6Light breeze 3 3 7 - 10Gentle breeze 4 4 11 - 16Moderate breeze 5 5 17 - 21Fresh breeze 6 6 22 - 27Strong breeze 7 7 28 - 33Near gale 8 8 34 - 40Gale 9 9 41 - 47Strong gale 101048 - 55Storm 111156 - 63Violent storm 1212>= 64Hurricane Beaufort Scale

8 Degree classification 0 - 34 fail 35 - 39pass 40 - 49 3rd 50 - 59 2.1 60 - 692.2 70 - 100 1st

9 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;

10 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

11 0 34 39 49 59 69 100 fail pass 3rd 2.2 2.1 1st Honours Grades topmark mark class classInterval class classlist classification name

12

13 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;

14 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') ) );

15 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()

16 Reusable Type

17 Where can we use this type Honours grades Beaufort scale Salary grades Discount rates for multiple purchase Tax rate bands...

18 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

19 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

20

21 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

22 Subtype inherits the types attributes warningReport attributes boatname varchar(50), datetime date, position latlong obsdatetime date, obsPosition latlong Inherited from Report Specific to warningReport

23 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) );

24 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


Download ppt "Object orientation - 3 Review of Tutorial 2 Encapsulation Reusable Types Inheritance."

Similar presentations


Ads by Google