object orientation - 3 review of tutorial 2 encapsulation reusable types inheritance
Post on 22-Dec-2015
220 views
TRANSCRIPT
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 type Boat Record
name : varchar name : 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 < 1 Calm1 1 - 3 Light air2 4 - 6 Light breeze3 7 - 10 Gentle breeze4 11 - 16 Moderate breeze5 17 - 21 Fresh breeze6 22 - 27 Strong breeze7 28 - 33 Near gale8 34 - 40 Gale9 41 - 47 Strong gale10 48 - 55 Storm11 56 - 63 Violent storm12 >= 64 Hurricane
Beaufort Scale
Degree classification
• 0 - 34 fail
• 35 - 39 pass
• 40 - 49 3rd
• 50 - 59 2.1
• 60 - 69 2.2
• 70 - 100 1st
Using a Relational DB• Grade table
class min maxfail 0 34pass 35 393rd 40 49
• Student tablename markfred 38sally 57
• Getting the classselect name, classfrom student, gradewhere 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
0 34 39 49 59 69 100fail
pass
3rd
2.2
2.1
1st
Honours Grades
topmarktopmark
mark
class
classIntervalclassInterval
classclass
classlistclasslist
classificationclassification
namename
Classification
name : varchar
getClass(mark : number) : string
classInterval
topMark : numberclass : String
classlist
1..*1..*
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;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
Stepfunction
name : varchar
getValue(inval : number) : string
Step
limit : numbervalue : String
StepList
1..*1..*
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
Report
boatname : varchardatetime : dateposition : latlong
<<Abstract>>
PositionReport
speed : numberdirection : number
WeatherReport
windspeed : numberwinddirection : number
WarningReport
obsDatetime : dateobsPosition : latlong
Defining a type and subtypecreate or replace type Report as object( boatname varchar(50), datetime date, position latlong) not instantiable not final;/show errorscreate or replace type warningReport under Report( obsdatetime date, obsPosition latlong);/
abstractabstractsupertypesupertype
subtypesubtype
Subtype inherits the types attributes
warningReport attributes
boatname varchar(50), datetime date, position latlong obsdatetime date, obsPosition latlong
Inherited from Report
Inherited from Report
Specific to warningReport
Specific to warningReport
Create log and insert reportscreate 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