elements of object-oriented design

Post on 24-May-2015

157 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

ELEMENTS OF OBJECT-ORIENTED DESIGN

CPTR304: INTERNET AUTHORING

HENRY OSBORNE

DESIGN PATTERNS

GENERALLY RECOGNIZED AS AN EXCELLENT SET OF TRIED-AND-TRUE SOLUTIONS TO COMMON PROBLEMS THAT DEVELOPERS FACE EVERY DAY

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.

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.

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();

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.

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);

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.

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);

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;

}

}

}

$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.’);

}

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)

M-V-C

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.

SUMMARY• OBJECT ORIENTED PROGRAMMING, COUPLED WITH DESIGN

IS THE KEY TO RE-USABLE 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.

ELEMENTS OF OBJECT-ORIENTED DESIGN

top related