I wrote a framework and I want to hear the thoughts of other on the design patterns it uses. Every method contains three design patterns - adapters(strategy), intercepting filters, and observers.
A normal class/method in a class looks like this:
class Run extends PVStaticObject{
public static function goForARun($miles) {
$return ='I ran '.$miles. ' miles today';
return $return;
}
}
With the design patterns the class/method looks like this:
class Run extends PVStaticObject{
public static function goForARun($miles) {
if (self::_hasAdapter(get_class(), __FUNCTION__))
return self::_callAdapter(get_class(), __FUNCTION__, $miles);
$miles = self::_applyFilter(get_class(), __FUNCTION__, $miles, array('event' => 'args'));
$return ='I ran '.$miles. ' miles today';
self::_notify(get_class() . '::' . __FUNCTION__, $miles, $return);
$return = self::_applyFilter(get_class(), __FUNCTION__, $return, array('event' => 'return'));
return $return;
}
}
A brieft explanation, the adapter will completely change the method by calling another method in its place. Its a way of changing the functionality of a class without modifying the core functionality.
Filters modify variables in a method by passing them out to another class's method or anonymous function where they are modified and return. Normal execution of the method continues.
Observers do not have a return and are purely call another class or anonymous function. They are for event drivin programming.
Examples of adding adapters, filters and observers are below.
Run::addObserver('Run::goForARun', 'run_observer', function($miles, $return){
echo PVHtml::div('Running '. $miles. ' has caused you to lose 2 pounds', array('style' => 'margin-top:10px;'));
}, array('type' => 'closure'));
Run::addAdapter('Run','goForARun', function($miles){
echo PVHtml::p('Because of the weather, you were not able to run '.$miles. ' today');
}, array('type' => 'closure'));
Run::addFilter('Run', 'goForARun', 'run_filter', function($data, $options) {
$data = PVHtml::strong($data);
$data = PVHtml::p($data);
return $data;
}, array('type'=> 'closure', 'event' => 'return'));
So what is thoughts and feedback on the design patterns? They are meant to faciliate aspect oriented and event driven design. They also to replace design patterns like dependency injection easily.
PS: More examples of AOP here.