Implementing the Stage Pattern in PHP 5 - Creating the programmatic model of the state pattern
(Page 2 of 4 )
A good place to start demonstrating the functionality provided by the stage pattern is with defining what I call a “target” class. As you’ll see for yourself in a moment, this class will be capable of modifying its behavior according to the variations introduced into its working environment.
Basically, the previously mentioned target class will be tasked with creating simple DIV elements, which will have their “overflow” CSS property set to a value of “hidden” by default. However, if it happens that the context where these DIVs will be used eventually changes, and in addition, the contents wrapped by them grows in size, then the DIVs will modify their overflow property to “scroll.”
Undoubtedly, this is a good example of a class that modifies its behavior in response to changes occurring inside its context; this precisely reflects the schema dictated by the stage pattern.
Okay, now that I have explained how I plan to illustrate the functionality of the stage pattern, please examine the signature of the “Div” class below, which as I stated previously, is responsible for displaying regular DIVs.
The definition for this brand new class is as follows:
// define 'Div' class
class Div{
private $data='default_data';
private $id='divid';
private $class='divclass';
public function __construct(){}
// set Div data
public function setData($data){
if(!preg_match("/[a-zA-Z0-9]+/",$data)){
throw new Exception('Invalid DIV data has been
supplied.');
}
$this->data=$data;
}
// set Div Id
public function setId($id){
if(!preg_match("/^[a-z]+$/",$id)){
throw new Exception('Invalid DIV id has been supplied.');
}
$this->id=$id;
}
// set Div class
public function setClass($class){
if(!preg_match("/^[a-z]+$/",$class)){
throw new Exception('Invalid DIV class has been
supplied.');
}
$this->class=$class;
}
// get Div data
public function getData(){
return $this->data;
}
// get Div id
public function getId(){
return $this->id;
}
// get Div class
public function getClass(){
return $this->class;
}
}
As you can see, the structure corresponding to the above “Div” class is indeed very easy to follow. The class in question presents the typical modifiers and accessing methods, which come in handy for setting and retrieving the values assigned to its “$data,” $id” and “$class” properties respectively, so I guess you shouldn’t have major problems understanding how it works.
However, as I explained earlier, the previous “Div” class is only a part of the stage pattern, since it can’t change its behavior by itself when the context varies. Therefore it’s necessary to define another class, which will be responsible for controlling the context where the target class is utilized, and eventually change its behavior.
Taking into account that a brand new contextual class has to be defined to complete the programmatic model imposed by the stage pattern, please jump into the following section and keep reading to learn how this class will be created.
Next: Changing the behavior of a target class >>
More PHP Articles
More By Alejandro Gervasio