There must be a lot of things that beginners don't understand about design patterns. Today is just the weekend, just take some time to write a single piece mode combined with command chain mode to build system core article, perhaps for some people, the article is too superficial, this is for beginners tutorial, because the time is relatively tight (to accompany the wife shopping, hehe), One of the design is not standardized, code writing is not standardized, bugs, etc. also hope that the road heroes point out, to facilitate common progress. I have a limited level. ^_^
I believe we have read a lot about the application of design patterns in PHP books or articles, but there are few directly to give examples, most of them after reading a feeling of a daze, if there is no project practice, it is difficult to design the pattern of the part of the figure.
To avoid the complexity of the code. No exception handling is added.
Single-piece mode and the basic knowledge of the command chain model, we Google it. Not in detail. Look at the example directly below:
<?php
/*
* @author: Noangels
* @time: August 30, 08
*/
Interface irunaction{
Gets the method defined in the class that can be run by the app
static function loadactions ();
class to call the other functions in the class using the
function Runaction ($action, $args);
}
/*
The core part of the *app class system
*/
Class app{
static private $__instance = NULL;
static Private $__commands = Array ();
static private $__flag = 1;
Private Function __construct () {}
Single-piece pattern design Gets the only instance of the class
static function Load () {
if (self::$__instance = = null) Self::$__instance = new APP;
return self::$__instance;
}
Add a name to the app's $__instance to check whether an instance of the class has been added each time you add a new command
If there is, ignore the operation and if not, add in
Public Function AddCommand ($cmdName) {
foreach (Self::$__commands as $cmd) {
if (Strtolower (Get_class ($cmd)) = = Strtolower (Get_class ($cmdName))) {
Self::$__flag = 0;
Break
}
}
if (Self::$__flag = = 1) self::$__commands[] = $cmdName;
Self::$__flag = 1;
}
The kernel part of the command chain pattern design the entry function of the invocation instance
First check to see if the operation is allowed to be invoked in the class if it does not prompt for undefined action to exit
Public Function RunCommand ($action, $args) {
Self::$__flag = 0;
foreach (Self::$__commands as $cmd) {
if (In_array ($action, $cmd->loadactions ())) {
Self::$__flag = 1;
$cmd->runaction ($action, $args);
}
}
if (Self::$__flag = = 0) {
Self::$__flag = 1;
Exit ("Undefined action by Action: $action");
}
}
Deletes an instance of a class, as long as the name of the class is specified
Public Function Removecommand ($className) {
foreach (self::$__commands as $key => $cmd) {
if (Strtolower (Get_class ($cmd)) = = Strtolower ($className)) {
Unset (self::$__commands[$key]);
}
}
}
For everyone to test to see if add and delete succeeded
Public Function Viewcommands () {
Echo (count (self::$__commands));
}
}
Class User implements Interface Irunaction
Class User implements irunaction{
Define actions that can be invoked
static Private $__actions = Array (' AddUser ', ' modifyuser ', ' removeuser ');
Get the action that can be invoked, in the actual process do not directly love you a $__actions design as public call
Instead, you should design a loadactions function to get the value of $__actions
static public Function loadactions () {
return self::$__actions;
}
Run the specified function
Public Function Runaction ($action, $args) {
I don't understand. This function can be used to refer to the manual
Call_user_func (Array ($this, $action), $args);
}
Test function only
protected function AddUser ($name) {
Echo ($name);
}
}
Class Test similar User
Class Test implements irunaction{
static Private $__actions = Array (' Addtest ', ' modifytest ', ' removetest ');
static public Function loadactions () {
return self::$__actions;
}
Public Function Runaction ($action, $args) {
Call_user_func (Array ($this, $action), $args);
}
protected function Addtest ($name) {
Echo ($name);
}
}