1 / 36

Object Oriented Data Models

Object Oriented Data Models. L19. Topics. Objects in PHP Generalisation Polymorphism Reusable classes Objects and Databases Next week Data modelling patterns - Fowler. Tenets of the Relational model. Data in 1NF relations - no repeated fields Data in 3NF relations - no redundant data

tierra
Download Presentation

Object Oriented Data Models

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Data Models L19

  2. Topics • Objects in PHP • Generalisation • Polymorphism • Reusable classes • Objects and Databases • Next week • Data modelling patterns - Fowler

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

  4. Common applications • Core business processing • Accounting systems • Order processing • Stock control • Administrative systems • student records • admissions • bookings

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

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

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

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

  9. 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");

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

  11. Inheritance • Salaried staff and contract staff are exclusive categories, but they overlap in specification – all are ‘employees’ and share some common characteristics Common attributes Common methods employee Is a Is a Local attributes Local methods (new or replacement) Salaried staff contractor

  12. Generalisation and Specialisation class 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; } }

  13. 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 annum Joe earns 3000 per annum

  14. 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)

  15. Power of Polymorphism class 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

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

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

  18. Emps and Depts class 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

  19. 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’

  20. 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)

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

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

  23. Degree classification • 0 - 34 fail • 35 - 39 pass • 40 - 49 3rd • 50 - 59 2.1 • 60 - 69 2.2 • 70 - 100 1st

  24. Relational DB using a theta join • Grade table class min max fail 0 34 pass 35 39 3rd 40 49 • Student table name mark fred 38 sally 57 • Getting the class select name, class from student, grade where mark between min and max;

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

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

  27. Classification name : varchar getClass(mark : number) : string 1..* 1..* classInterval topMark : number class : String

  28. 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()

  29. rangeFunction name : string getValue(val : number) : string 1..* 1..* range limit : number value : String

  30. 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; } }

  31. $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

  32. Where can we use this class? • Honours grades • Beaufort scale • Salary grades • Discount rates for multiple purchase • Tax rate bands • ...

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

  34. Row to Object mapping class 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>"); } }

  35. 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. 83176 Chris Wallace is in Room 3P14 on Ext. 83177

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

More Related