ELEMENTS OF OBJECTORIENTED DESIGN
CPTR304: INTERNET AUTHORING
HENRY OSBORNE
DESIGN PATTERNS

GENERALLY RECOGNIZED AS AN EXCELLENT SET OF TRIEDAND-TRUE SOLUTIONS TO COMMON PROBLEMS THAT
DEVELOPERS FA...
DESIGN PATTERN THEORY

DESIGN PATTERNS ARE NOT REALLY ABOUT CODE AT ALL—
THEY SIMPLY PROVIDE GUIDELINES THAT THE DEVELOPER...
THE SINGLETON PATTERN

• PROBABLY THE SIMPLEST DESIGN PATTERN.
• ITS GOAL IS TO PROVIDE ACCESS TO A SINGLE RESOURCE
THAT I...
class DB {
private static $_singleton;
private $_connection;
private function __construct()
{
$this->_connection = mysql_c...
THE FACTORY PATTERN

USED IN SCENARIOS WHERE YOU HAVE A GENERIC CLASS

(THE FACTORY ) THAT PROVIDES THE FACILITIES FOR
CRE...
class Configuration {

const STORE_INI = 1;
const STORE_DB = 2;
const STORE_XML = 3;
public static function getStore($type...
THE REGISTRY PATTERN

BY TAKING THE SINGLETON PATTERN A LITTLE FURTHER THE
REGISTRY PATTERN CAN BE IMPLEMENTED. THIS ALLOW...
class Registry {
private static $_register;
public static function add(&$item, $name = null)
{
if (is_object($item) && is_...
if (array_key_exists($name, self::$_register)) {
return self::$_register[$name];
} else {
$msg = "’$name’ is not registere...
$db = new DB();
Registry::add($db);
// Later on
if (Registry::exists(’DB’)) {

$db = Registry::get(’DB’);
} else {
die(’We...
THE MODEL-VIEW-CONTROLLER (MVC) PATTERN

•

PROVIDES A METHODOLOGY FOR SEPARATING THE
BUSINESS LOGIC (MODEL) FROM THE DISP...
M-V-C
THE ACTIVERECORD PATTERN
THIS IS USED TO ENCAPSULATE ACCESS TO A DATA SOURCE
SO THAT THE ACT OF ACCESSING ITS COMPONENTS—B...
SUMMARY
• OBJECT ORIENTED PROGRAMMING, COUPLED WITH DESIGN IS THE KEY TO REUSABLE AND HIGHLY MODULAR CODE.
• THE MORE FORE...
ELEMENTS OF OBJECTORIENTED DESIGN
Elements of Object-oriented Design
Upcoming SlideShare
Loading in …5
×

Elements of Object-oriented Design

337 views

Published on

Object Oriented programming, coupled with Design Patterns—including those provided by the Standard PHP Library—is the key to re-usable and highly modular code

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
337
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Elements of Object-oriented Design

  1. 1. ELEMENTS OF OBJECTORIENTED DESIGN CPTR304: INTERNET AUTHORING HENRY OSBORNE
  2. 2. DESIGN PATTERNS GENERALLY RECOGNIZED AS AN EXCELLENT SET OF TRIEDAND-TRUE SOLUTIONS TO COMMON PROBLEMS THAT DEVELOPERS FACE EVERY DAY
  3. 3. DESIGN PATTERN THEORY DESIGN PATTERNS ARE NOT REALLY ABOUT CODE AT ALL— THEY SIMPLY PROVIDE GUIDELINES THAT THE DEVELOPER CAN TRANSLATE INTO CODE FOR PRETTY MUCH EVERY POSSIBLE LANGUAGE.
  4. 4. THE SINGLETON PATTERN • PROBABLY THE SIMPLEST DESIGN PATTERN. • ITS GOAL IS TO PROVIDE ACCESS TO A SINGLE RESOURCE THAT IS NEVER DUPLICATED, BUT THAT IS MADE AVAILABLE TO ANY PORTION OF AN APPLICATION THAT REQUESTS IT WITHOUT THE NEED TO KEEP TRACK OF ITS EXISTENCE.
  5. 5. class DB { private static $_singleton; private $_connection; private function __construct() { $this->_connection = mysql_connect(); } public static function getInstance() { if (is_null (self::$_singleton)) { self::$_singleton = new DB(); } return self::$_singleton; } } $db = DB::getInstance();
  6. 6. THE FACTORY PATTERN USED IN SCENARIOS WHERE YOU HAVE A GENERIC CLASS (THE FACTORY ) THAT PROVIDES THE FACILITIES FOR CREATING INSTANCES OF ONE OR MORE SEPARATE “SPECIALIZED” CLASSES THAT HANDLE THE SAME TASK IN DIFFERENT WAYS.
  7. 7. class Configuration { const STORE_INI = 1; const STORE_DB = 2; const STORE_XML = 3; public static function getStore($type = self::STORE_XML) { switch ($type) { case self::STORE_INI: return new Configuration_Ini(); case self::STORE_DB: return new Configuration_DB(); case self::STORE_XML: return new Configuration_XML(); default: throw new Exception("Unknown Datastore Specified."); } } } class Configuration_Ini { // ... } class Configuration_DB { // ... } class Configuration_XML { // ... } $config = Configuration::getStore(Configuration::STORE_XML);
  8. 8. THE REGISTRY PATTERN BY TAKING THE SINGLETON PATTERN A LITTLE FURTHER THE REGISTRY PATTERN CAN BE IMPLEMENTED. THIS ALLOWS THE USE OF ANY OBJECT AS A SINGLETON WITHOUT IT BEING WRITTEN SPECIFICALLY THAT WAY.
  9. 9. class Registry { private static $_register; public static function add(&$item, $name = null) { if (is_object($item) && is_null($name)) { $name = get_class($item); } elseif (is_null($name)) { $msg = "You must provide a name for non-objects"; throw new Exception($msg); } $name = strtolower($name); self::$_register[$name] = $item; } public static function &get($name) { $name = strtolower($name);
  10. 10. if (array_key_exists($name, self::$_register)) { return self::$_register[$name]; } else { $msg = "’$name’ is not registered."; throw new Exception($msg); } } public static function exists($name) { $name = strtolower($name); if (array_key_exists($name, self::$_register)) { return true; } else { return false; } } }
  11. 11. $db = new DB(); Registry::add($db); // Later on if (Registry::exists(’DB’)) { $db = Registry::get(’DB’); } else { die(’We lost our Database connection somewhere. Bear with us.’); }
  12. 12. THE MODEL-VIEW-CONTROLLER (MVC) PATTERN • PROVIDES A METHODOLOGY FOR SEPARATING THE BUSINESS LOGIC (MODEL) FROM THE DISPLAY LOGIC (VIEW) AND THE DECISIONAL CONTROLS (CONTROLLER)
  13. 13. M-V-C
  14. 14. THE ACTIVERECORD PATTERN THIS IS USED TO ENCAPSULATE ACCESS TO A DATA SOURCE SO THAT THE ACT OF ACCESSING ITS COMPONENTS—BOTH FOR READING AND FOR WRITING—IS, IN FACT, HIDDEN WITHIN THE CLASS THAT IMPLEMENTS THE PATTERN, ALLOWING ITS CALLERS TO WORRY ABOUT USING THE DATA, AS OPPOSED TO DEALING WITH THE DATABASE.
  15. 15. SUMMARY • OBJECT ORIENTED PROGRAMMING, COUPLED WITH DESIGN IS THE KEY TO REUSABLE AND HIGHLY MODULAR CODE. • THE MORE FORETHOUGHT YOU GIVE YOUR DESIGN, THE MORE LIKELY YOU ARE TO BE ABLE TO RE-USE AT LEAST SOME OF IT, LATER, SAVING TIME AND EFFORT IN THE FUTURE—NOT TO MENTION THAT PROPER DESIGN TECHNIQUES ALSO MAKE CODE EASIER TO MAINTAIN AND EXTEND. • BRINGING THIS EXPERIENCE TO THE TABLE MAKES YOU TRULY VERSATILE; DESIGN PATTERNS ARE, LARGELY LANGUAGE- AND PROBLEM-INDEPENDENT.
  16. 16. ELEMENTS OF OBJECTORIENTED DESIGN

×