| |||||||||||
| |||||||||||
Code to illustrate UML examples
Object Orientation: Design Patterns example from a Well House Consultants training course
More on Object Orientation: Design Patterns [link]
Source code: umli.php Module: Q908
<?php
/* An example which analyses a web server log file. This is a major OO demo and will be backed up by sample UML diagrams. Overview ... we're going to read in a web server log file and make up "visit" objects each of which will include a number (1 or more) access objects. We'll then call methods on the visits to inform the person running the application Written by ... version number ... who to call for support ... copyright ... */ // --------- These classes would normally be in a separate (series of) file(s). class visit { function factory($sourcefile) { // Static utility method! $fh = fopen($sourcefile,"r"); $ips = array(); $vis = array(); while ($lyne = fgets($fh)) { $parts = explode(" ",$lyne); // Spike solution - should do better later $remote = $parts[0]; if ($ips[$remote]) { // This is another access fom same place $visit_number = $ips[$remote]; $vis[$visit_number]->addhit($lyne); } else { // This is the first access from a place array_push($vis, new visit($lyne)); $ips[$remote] = count($vis)-1; } } fclose($fh); return $vis; } function __construct ($definition) { // php5 equiv to "function visit" $this->hits = array(); $this->addhit($definition); } function addhit($definition) { $potential = new access($definition); if ($potential->method != "OTHER") array_push($this->hits, $potential); } function getelapsed() { if (count($this->hits) > 0) { $first = $this->hits[0]->getsincemidnight(); $last = $this->hits[count($this->hits)-1]->getsincemidnight(); return ($last - $first); // Need to add overmidnight correction } else { return -1; } } function getcount() { return count($this->hits); } function getid() { if (count($this->hits) < 1) return "Others Only"; return $this->hits[0]->getip(); } } // --------------- /* Sample source record - to help class maintainer! 223-136-179-250.dynamic.hinet.net - - [06/Feb/2012:04:05:27 +0000] "GET /bing/images/WHClogoBW.jpg HTTP/1.1" 200 12344 "http://www.wellho.net/forum/The-Tcl-programming-language/suspend-then-background-process-run-from-expect.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3" */ class access { function __construct($source) { // We're likely to split up and define quite a bit (more) here! if (preg_match('!(\S+)[^:]+:(\d+):(\d+):(\d+).*(GET|POST)\s+([^\?\s]+)[^"]+"\s+(\S+)!',$source,$gotten)) { list($all, $this->ip, $this->hour, $this->min, $this->sec, $this->method, $this->url, $this->status) = $gotten; } else { $this->method = "OTHER"; } } function getip() { return $this->ip; } function getsincemidnight() { return $this->hour * 3600 + $this->min * 60 + $this->sec ; } } // --------- And here is the application / test harness $visits = visit::factory("sample.log"); // Run a static (class rather than object) function $response = ""; foreach ($visits as $v) { $elapsed = $v->getelapsed(); $nhits = $v->getcount(); $remote = $v->getid(); $response .= "From $remote, $nhits accesses over $elapsed seconds<br />\n"; } ?> <html> <head><title>Web Server Analysis Test Harness</title></head> <body><h1>Test Harness - visit analysis</h1> <?= $response ?> </body></html> Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Python * Object Oriented Programming in PHP * Python Programming * Intermediate Python Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering all the subjects we teach are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Object Orientation: Design Patterns" training module. You'll find a description of the topic and some
other closely related examples on the "Object Orientation: Design Patterns" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |