36
Object Oriented Data Models L19

Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

  • View
    241

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Object Oriented Data Models

L19

Page 2: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Topics

• Objects in PHP– Generalisation– Polymorphism– Reusable classes– Objects and Databases

• Next week – Data modelling patterns - Fowler

Page 3: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Tenets of the Relational model

• Data in 1NF relations - no repeated fields

• Data in 3NF relations - no redundant data

• Field values drawn from small range of supplied data types

• Relationships require join operation

• Basic CRUD operations

• Passive - changes only when UPDATEd

• Balanced implementation- updates and read

Page 4: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Common applications

• Core business processing– Accounting systems– Order processing– Stock control

• Administrative systems– student records– admissions– bookings

Page 5: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Tricky application domains

• Design systems - CAD, CAM, CASE

• Text searching - search engine

• Multi-media, hyper-media systems -images, video, audio, complex networks

• Spatial data - Geographic Information Systems

• Decision support systems - analysis of large body of ‘static’ data

• Real-time active systems - air-traffic control

Page 6: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Complex entities• Electronic components in a Computer Aided Design (CAD)

system

• Mechanical components in a Computer Aided Manufacturing (CAM) system

• Software requirements, specifications and components in a CASE environment

• Multi-media documents - text, images and sounds in multimedia document managers

• Spatial or geographic data, such as geometric figures and maps in a Geographic Information System (GIS)

Page 7: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Objects and entities

• Can view as extension of Entity-Relationship modelling• Entity type = = class

– roughly = table definition – part of schema

• Entity instance = = object – roughly= row – part of factbase– + OID Object Identifier to ensure uniqueness– + type ref so Object is not tied to table

Page 8: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Class• Defines the attributes of the objects of that kind

– attributes may be• basic types - strings, numbers, dates

• array or object encapsulated in object

• references to other objects

– methods• constructor - to create a new object

• accessors - to access attributes, compute derived data

• modifiers - to change attributes

Page 9: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

A PHP class• <?php• class emp {• var $name, $sal, $comm;• function emp($n,$s,$c) {• $this->name=$n;• $this->sal=$s;• $this->comm=$c;• }• function yearsal() {• return $this->sal * 12 + $this->comm;• }• }

• $x = new emp('Jones', 200, 5);

• print_r($x);• $xsal = $x->yearsal();

• print ("$x->name earns $xsal per annum");

Page 10: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

PHP v Java

• Object orientation in PHP4 is rather minimal by comparison with Java

• PHP variables are untyped

• All variables publicly accessible

• PHP $this === Java self (and no default)

• PHP -> === Java .

Page 11: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Inheritance• Salaried staff and contract staff are

exclusive categories, but they overlap in specification – all are ‘employees’ and share some common characteristics

employee

Salaried staff

contractor

Is aIs a

Common attributesCommon methods

Local attributesLocal methods (new or replacement)

Page 12: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Generalisation and Specialisationclass employee { var $name; function yearsal() { return false; } function initial() { return substr($this->name,0,1); }}class salaried extends employee { var $sal, $comm; function salaried($n,$s,$c) { $this->name=$n; $this->sal=$s; $this->comm=$c; } function yearsal() { return $this->sal * 12 + $this->comm; }}class contractor extends employee { var $rate, $days; function contractor($n,$r,$d) { $this->name=$n; $this->rate=$r; $this->days=$d; } function yearsal() { return $this->rate * $this->days; }}

Page 13: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Creating and using Objects

$x = new salaried ('Fred', 100, 99);$xsal= $x->yearsal();$i = $x->initial();print ("$i $x->name earns $xsal per annum<br>");$y = new contractor('Joe', 10, 300);$ysal= $y->yearsal();print ("$y->name earns $ysal per annum<br>");

Output is

F Fred earns 1299 per annumJoe earns 3000 per annum

Page 14: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Polymorphism

• Many shapes

• An ‘employee’ object may be really a ‘salaried’ object or a ‘contractor’ object

• Sometimes we want to treat as an instance of the generalisation (employee), sometimes as the special case (salaried or contractor)

Page 15: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Power of Polymorphismclass company { var $name, $staff; function company($n) { $this->name = $n; } function employ($s) { $this->staff[] = $s; } function yearsal() { $tot = 0; foreach($this->staff as $person) { $tot = $tot + $person->yearsal(); } return $tot; }} $c = new company("University of the West of England"); $c->employ($x); $c->employ($y);

$csal= $c->yearsal();

print ("$c->name pays $csal per annum<br>");

University of the West of England pays 4299 per annum

Page 16: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

How does it work?

• In the calculation loop, each employee is accessed in turn.

• When the function annualsal() is called, it is the version appropriate to the objects base class which is used, so the appropriate calculation is performed.

Page 17: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Object References

• In a relational model, rows are related by shared values – the deptno in an Emp row = the deptno in a Dept row

• Objects have a system-generated unique identifier (across all objects, not just the rows in a single table like an auto_increment column)

• References can be stored as an attribute of another object

Page 18: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Emps and Deptsclass dept { var $name, $location, $manager; function dept($n, $l, $m) { $this->name = $n; $this->location = $l; $this->manager = $m; }}

$d = new dept("Accounting","Swindon", & $x);

// print("Department $d->name is managed by $d->manager->name<br>");// cant chain in PHP 4.3 $boss = $d->manager; print("Department $d->name is managed by $boss->name<br>");

Department Accounting is managed by Fred

Page 19: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Reflection• Reflection refers to the ability of a program

to find out about itself• For example, there are functions to:

– Get the class of an object– Get the superclass of an object– Get the names of the class methods (functions)– Get the names of the class variables

• Classes could be called ‘self-describing’

Page 20: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Reflection code

$class= get_class($x); $super = get_parent_class($x); print ("$x->name is a $class ($super)<br>");

$class= get_class($y); $super = get_parent_class($y); print ("$y->name is a $class ($super)<br>");

Fred is a salaried (employee)Joe is a contractor (employee)

Page 21: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

User-defined types• Codd’s relational model has ‘domains’

– commercial RDBMS and programming languages provide only standard types with standard functions (integer, string, date)

• Applications require – restrictions on standard types e.g. restricted values - can

sometimes use Constraints to enforce – types defined by several values bound together

• eg international currency requires amount and currency code– functions which operate on these types

• eg to convert between 2 international currencies

Page 22: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

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

Page 23: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Degree classification

• 0 - 34 fail

• 35 - 39 pass

• 40 - 49 3rd

• 50 - 59 2.1

• 60 - 69 2.2

• 70 - 100 1st

Page 24: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Relational DB using a theta join• 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;

Page 25: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

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

Page 26: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

0 34 39 49 59 69 100fail

pass

3rd

2.2

2.1

1st

Honours Grades

topmarktopmark

mark

class

classIntervalclassInterval

classclass

classlistclasslist

classificationclassification

namename

Page 27: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Classification

name : varchar

getClass(mark : number) : string

classInterval

topMark : numberclass : String

1..*1..*

Page 28: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

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 > range– topmark > limit– class > value– classification > rangeFunction– getClass() > getValue()

Page 29: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

rangeFunction

name : string

getValue(val : number) : string

range

limit : numbervalue : String

1..*1..*

Page 30: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

class range { var $limit, $value; function range($l,$v) { $this->limit = $l; $this->value = $v; } } class rangeFunction { var $name, $ranges; function rangeFunction($n) { $this->name= $n; } function addRange($r) { $this->ranges[]=$r; } function getValue($val) { foreach($this->ranges as $r ) { if ($val <= $r->limit) { return $r->value; } } return false; } }

Page 31: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

$pr = new rangeFunction("Prices");$r = new range(9 , 9); $pr->addRange ($r);$r = new range(19 , 7); $pr->addRange ($r);$r = new range(99 , 5); $pr->addRange ($r);$r = new range(9999999 , 4); $pr->addRange ($r); //print_r($pr);

for($i=0;$i<200;$i+=5) { $price = $pr->getValue($i); $cost = $i * $price; print("price for $i at £$price is £$cost<br>");}

price for 0 at £9 is £0 price for 5 at £9 is £45 price for 10 at £7 is £70 price for 15 at £7 is £105 price for 20 at £5 is £100 price for 25 at £5 is £125

Page 32: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Where can we use this class?

• Honours grades

• Beaufort scale

• Salary grades

• Discount rates for multiple purchase

• Tax rate bands

• ...

Page 33: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Objects and Databases

• Objects don’t fit well into a RDBMS– How many tables would the Beaufort Scale require?

– Some RDBMS have stored procedures but not integrated with data

• Object-Relational databases (e.g. Oracle 9i) try to bridge the gap by adding types, methods, inheritance, references but not popular

• J2EE and .NET do the mapping to and from Objects in the Middle tier.

Page 34: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Row to Object mappingclass person { var $usercode,$familyname, $givenname, $job, $dept, $ext, $homephone, $room, $fullname; var $dblink; function person($db) { $this->dblink = $db; }

function get($username) { $res = mysql_query("select * from staff where username='$username'",$this->dblink); if (!$e = mysql_fetch_object($res) ) return false; $this->username = $e->username; $this->givenname = $e->givenname; $this->familyname = $e->familyname; $this->ext = $e->ext; $this->homephone =''; $this->room = $e->room; $this->fullname = $this->givenname . ' ' . $this->familyname; } function put() { $res = mysql_query("update staff set givenname='$this->givenname', familyname = '$this->familyname', ext= '$this->ext', room= '$this->room' where username='$this->username'", $this->dblink); return $res; } function show() { print(" $this->fullname is in Room $this->room on Ext. $this->ext<br>"); } }

Page 35: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Testing

$x = new person( $db);$x->get('CW');$x->show();

$x->ext= $x->ext+1;

$x->put($db);

$y = new person($db); $y->get('CW');$y->show();

Chris Wallace is in Room 3P14 on Ext. 83176Chris Wallace is in Room 3P14 on Ext. 83177

Page 36: Object Oriented Data Models L19. Topics Objects in PHP –Generalisation –Polymorphism –Reusable classes –Objects and Databases Next week –Data modelling

Tutorial work

• Exercises– Update the Rose model to show the OO

features– A new subtype for Volunteers who get paid a

flat annual fee– Salgrade table as an object in PHP