Polyton Design Pattern

/*
* The Polyton Design Pattern
* By Adam J Nowak ~ http://hyperspatial.com
*
* Creates unique numbered class instances and allows you to run a different set of functions for the first instance of the class.
*
* Methods:
* create(); //Constructor method, Use: $polyton = Polyton::create('Test String');
* get_version(); //Instance version number, Use: $polyton->get_version();
* get_data(); //Value of the $_data class property, Use: $polyton->get_data();
* set_data(); //Set value of the $_data class property, Use: $polyton->set_data('New Test String');
*
* Create Instances:
* 1 - Use static method to instantiate
* 2 - Constructor is private. *Don't Use: $var = new Polyton('new polyton');
* 3 - The one argument is a simple string to demonstrate constructor argument flow
*/

class Polyton{
private static $instance;
private static $version;
private $_version;
private $_data;

private function __construct($data){
$this->_data = $data;
$this->_version = self::$version;
echo "Polyton $this->_version Constructed"; //Replace w/ constructor statements
}
public static function create($data){
if(!isset(self::$instance)){
self::$version = 1;
self::$instance = new Polyton($data); //Create original
echo ' ~ Original<br>'; //Replace w/ original instance statements
}
else{
self::$version ++;
self::$instance = new Polyton($data); //Create subsequent version
echo ' ~ Reproduction<br>'; //Replace w/ instance statements
}
return self::$instance;
}
public function get_version(){ return $this->_version; }
public function get_data(){ return $this->_data; }
public function set_data($new_data){
$this->_data = $new_data;
return $this->_data;
}
}

/*
* Polyton Design Pattern example markup
*
* Create Instances:
* 1 - Use static method to instantiate
* 2 - Constructor is private so you cant use: $var = new Polyton('new polyton');
* 3 - The one argument is a simple string to demonstrate constructor argument flow
*/

$polyton = Polyton::create('Top');
$polyton2 = Polyton::create('Middle');
$polyton3 = Polyton::create('I like to argue');

// Display info about the second object you created
echo '<br>Polyton 2 Info:<br>';
echo "Version = " . $polyton2->get_version() . '<br>';
echo "Data = " . $polyton2->get_data() . '<br>';

// Change the value of the stored argument
echo '<br>Polyton 2 data change:<br>';
$polyton2->set_data('The Very Middle');
echo "Changed Data = " . $polyton2->get_data() . '<br>';

?>

0 Comments

You must be logged in to post a comment.