michaelfox + oop   55

design:adapters_and_proxy_patterns [phpPatterns]
Having seen the Strategy pattern, it’s time to look at two related patterns, which use the same notion of a unified API to control a collection of (possibly) unrelated objects.

Both of these patterns give us the opportunity to turn our applications into something much “bigger” than we origionally planned, with minimal effort required to re-write code.

1)

The Adapter Pattern

The adapter pattern is conceptually a bit like an international power adapter that you take on holiday, so you can plug in your electric shaver or hairdryer to the local power supply.

You use an adapter pattern when you’ve already built a class to perform some task but now need to an alternative class (with perhaps a very different API) to perform the same function.

It’s similar to the Strategy pattern but where the Strategy pattern deals with hiding a set of algorithms behind a common interface, the Adapter pattern deals with hiding a collection of objects behind an interface.

Note: there are two variants of the Adapter pattern: the Object Adapter (which is what we’re talking about here) and the Class Adapter (which requires multiple inheritance, not supported by PHP).

It’s best demonstrated with an example. We’ve looked at the MVC Pattern.

Working, in general, from that example, which used MySQL as the target database, imagine now that we have customers for our “Products” application. Customer A says “I love it but I use Oracle”. Customer B says “I want to deploy you application in my N-Tier environment using SOAP to provide your application with data”.

So here we have two problems. For Oracle, simple database abstraction isn’t enough, because MySQL uses an SQL syntax which often varies significantly from Oracle. For example, imagine we want limit the number of rows we return from a SEL
php  inspiration  patterns  objects  bestpractices  oop  adapter 
december 2010 by michaelfox
Presentations
The Observer Pattern
The observer pattern, also known as subject-observer and listener, is a more complicated but very prevalent and useful pattern. Anyone who has done event handling in Java knows this pattern, every kind of event listener is an observer to one subject or another - the keyboard, a window, a mouse, etc.

The pattern describes how to attach any number of observers to a subject, which can then act on events that occur involving the subject. For example, this pattern is available in the PEAR Log:: package, allowing you to monitor log data with an observer object and take action - such as sending an email page on any critical errors - on certain kinds of log messages.

Here's a simple usage of this implementation of the observer pattern:
php  horde  framework  inspiration  patterns  objects  observer  events  observable  bestpractices  oop 
december 2010 by michaelfox
design:observer_pattern [phpPatterns]
The Observer Pattern is designed to help cope with one to many relationships between objects, allowing changes in an object to update many associated objects. It provides a powerful mechanism to extend our applications, in terms of how they respond to events, without needing alter existing (and working) code.

We’ll take a simplified look at how the observer pattern might be used in a typical PHP forum application then suggest other uses for the observer pattern

Under Observation

1) The Observer pattern is defined by the Gang of Four in Design Patterns as behavioural pattern - that is one we can use to modify the behaviour of our applications. It’s regarded as being so useful, that Sun bothered to implement it in the Java API (see Observable and Observer).

The basic principle behind the observer pattern is if you have some object, such as a Post object for a forum system, you can have other objects, such as a Mailer object, act as an observer and respond to any changes in the Post object, such as emailing relevant forums users that a new post has been added to the thread they were subscribed to.

Conceptually, the Observer pattern in something like a trigger in a database, which runs a stored procedure when a table row is modified for example (we won’t go too far with this analogy though).

Normally we might implement the mailing functionality in the Post object itself but what if later we want add further “events” to Post, when a new post is made, such as adding an entry to our forums RSS feed for example? And how many more things might we want to add in future?

The observer pattern provides us the mechanism to add such functionality without needing to alter the Post object.

The is easiest to see will some some example code.

First we define two interface classes; an Observable class which will be inherited by the Post class and allows it to become the subject of observation and an Observer class which will be inherited by the classes used to observer Post.
php  patterns  objects  observer  events  observable  bestpractices  oop 
december 2010 by michaelfox
Understanding and Applying Polymorphism in PHP | Nettuts+
The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way.

A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button “does,” however, depends on what it is connected to and the context in which it is used — but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task.

In the programming world, polymorphism is used to make applications more modular and extensible. Instead of messy conditional statements describing different courses of action, you create interchangeable objects that you select based on your needs. That is the basic goal of polymorphism.
php  oop  polymorphism 
september 2010 by michaelfox
Building and Maintaining Large JavaScript Applications
When I’ve talked about managing large applications in the past, I was criticized for going through many different options and then telling you that you can decide which one you liked the best. I suppose I might be afraid I’d make the wrong choice too, so in this article, I’m going to explain what I would use in my totally sweet large applications, but just know, there are lots of smart people who have different opinions.
by:alexsexton  javascript  dependency  objects  oop  jquery  inheritance  patterns  design  bestpractices  projects  library 
june 2010 by michaelfox
Ada 95: Contents
This book, originally published by Prentice Hall in 1996, was taken out of print in 2001 and the rights to the book were subsequently returned to me by Pearson (the successor company to Prentice Hall). I have decided to make it available online in HTML format, and at the same time I have corrected several errata which were present in the printed editions of the book. It’s perfectly possible that I might have missed some, or even introduced some brand-new ones, as part of the process of transforming the text into HTML. If you spot any mistakes, please let me know so I can correct the master copy, which can be found at http://www.it.bton.ac.uk/staff/je/adacraft/.

Downloadable copies are available as http://www.it.bton.ac.uk/staff/je/adacraft/bookhtml.zip (in zip format for Windows systems) or as http://www.it.bton.ac.uk/staff/je/adacraft/bookhtml.tar.gz (a gzipped tarball for Unix systems). Each distribution also includes the complete set of examples from the book, both for Windows (adacraft.zip) and for Unix (adacraft.tar.gz).
oop  development  bestpractices  book  download  ebooks 
may 2010 by michaelfox
The M in MVC: Why Models are Misunderstood and Unappreciated - Maugrim The Reaper's Blog
I've been writing a book about the Zend Framework recently - I'm sure some of you have noticed :-). A while back I finished a draft of two chapters called "The Architecture of Zend Framework Applications" and "Understanding The Zend Framework". The first chapter is an exploration of the Model-View-Controller (MVC) architectural pattern and why it has become the defacto standard for adoption in web applications. The second explores how the MVC architecture relates to the Zend Framework's components, their design and interactions.

By the time I'd finished both chapters I realised I had spent a lot of space explaining the Model, most of it discussing how the Zend Framework does not actually give you one. In fact, no web application framework offers a complete Model (for reasons I'll explain later). However nearly all frameworks manage to avoid making that perfectly obvious. Instead they continually link the concept of a Model to the related, but not identical, concept of data access. This is quite misleading.

This side of frameworks never gets a lot of attention. Yet it is a massive contributor to a whole class of problems in applications which attempt to utilise MVC by adopting web application frameworks. Further, I've always found myself better entertained by banging my head against brick walls than by trying to get the idea of a Model across to other developers. I'm not saying all developers are stupid or dumb, or that they don't get the concept in general, but all developers (PHP or not) don't quite link Models to the area of practice which justifies them - Object Oriented Programming principles.

In this entry I explore Models in terms of how developers relate them to Controllers and Views in applications and note a few strategies you can employ when using proper Models.
model  mvc  oop  logic  bestpractices 
may 2010 by michaelfox
• Why are interfaces widely ignored in the PHP world and what use do they have when working with symfony? | test.ical.ly
What are interfaces?

The best description I could find so far is: An interface is a contract.

* An interface defines public methods that a class needs to implement
* An interface does not contain any code
* A class can implement multiple interfaces
* Interfaces can be used for type hinting

Basically if you see a class implementing an interface you know how to use it no matter how the implementation within the class is achieved.
What are abstract classes?

* An abstract class can implement methods (code)
* An abstract class can define abstract methods (no code)
* An abstract class can not be instantiated
* Classes can only inherit from one abstract class

And the difference is now what?

Abstract classes and interfaces are in some ways similar but they do each have their unique use cases.

The simplest rule is:

* If you write an abstract class containing only abstract methods you use an abstract class as an interface.
php  oop  bestpractices  patterns  interfaces  abstract 
may 2010 by michaelfox
Dmitry Baranovskiy’s Web Log
More and more people on the Web digging into JavaScript and using it for daily tasks. It is always fascinating to watch how they approach inheritance in JavaScript.
javascript  inheritance  patterns  oop  design  programming  bestpractices  reference 
april 2010 by michaelfox
Using Inheritance Patterns to Organize Large jQuery Applications « AlexSexton.com
I want to introduce/reinforce a pattern for developing large applications with jQuery. I did not invent any of this, but I find that the resources that describe this technique are few and far-between.- so I'm taking a shot at it.

By and large, when using jQuery, developers seem to forget the paradigms they learned for well structured code in other languages. This is likely due to the fact that jQuery is effectively neutral when it comes to your structural methodology or inheritance patterns, and therefore doesn't push someone in any one direction. Many times in other libraries (See Dojo Declare/Provide/Require, or MooTools Class, etc.), a paradigm is used and exclusively offered, and then code generally ends up more uniform than the oh-so-common-massive-jquery-indented-chains that I'm sure you've seen .
javascript  jquery  patterns  oop  prototypal  inheritance 
april 2010 by michaelfox
JavaScript Programming Patterns « klauskomenda.com
JavaScript is meant to be used to add behaviour to a website, might it be for form validation or for more complex operations like drag & drop functionality or performing asynchronous requests to the webserver (aka Ajax). During the past few years, JavaScript libraries became increasingly popular. One of the reasons is definitely that websites are getting more and more complex and reinventing the wheel each time is not acceptable if you are working on a tight schedule. But letting aside libraries and focusing on the “bare” syntax of JavaScript, it is very valuable to know what kind of options you have in terms of programming patterns when writing JavaScript.

In this article I am trying to present some of the techniques out there that I have discovered. The patterns I would like to mention are the following:

* The Old-School Way
* Singleton
* Module Pattern
* Revealing Module Pattern
* Custom Objects
* Lazy Function Definition
javascript  patterns  oop  singleton  module  resources 
april 2010 by michaelfox
Use your singletons wisely
The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article I discuss strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. I also describe the properties of some classes that are truly singletons.
php  design  patterns  oop  singleton  testing  unittesting  ttd 
march 2010 by michaelfox
Singleton Classes in PHP - PHP articles and PHP tutorials - PHP 5, MySQL, PostgreSQL, AJAX, Web 2.0
The key steps in creating singleton classes are as follows:

* Prevent direct instantiation by making the contructor private
* Store the only instance of the class as a static property of the class.
* Provide a static method to access the instance. The instance is created the first time this method is called.
php  design  patterns  oop  singleton 
march 2010 by michaelfox

Copy this bookmark:



description:


tags: