Beginners for the design pattern must have a lot of do not understand, today is just the weekend, took out a little time to write a single-piece mode combined with command chain mode to create the core of the article, may be for some people, the article is too simple, this is a tutorial for beginners, because time is tight (to accompany the wife shopping, hehe), One of the appearance of the design is not standardized, code writing is not standardized, and so on the bug and so on the road to the hero points to facilitate common progress. I have limited level. ^_^
I believe that we have read a lot about the application of design patterns in PHP books or articles, but very few directly to give examples, most of them after reading a feeling of a sense of confusion, if there is no project practice, it is difficult to make the design pattern part of the figure.
To avoid the complexity of the code. Exception handling is not added.
A single-piece mode and the basic knowledge of the command chain mode, we have Google for ourselves. Not in detail. See the example below:
/*
* @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 ();
The entry function in the class calls the other functions in the
function Runaction ($action, $args);
}
/*
Core parts of the *app class system
*/
Class app{
static private $__instance = NULL;
static Private $__commands = Array ();
static private $__flag = 1;
Private Function __construct () {}
Single-piece mode design Gets the unique instance of the class
static function Load () {
if (self::$__instance = = null) Self::$__instance = new APP;
return self::$__instance;
}
Add a new command in the $__instance named to the app to check if an instance of the class has been added before
If there is, ignore the operation if not, add it 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 core part of the command chain pattern design invokes the instance's entry function
First check whether the operation is allowed in the class to invoke the action if it is not prompted undefined operation exits
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");
}
}
Delete an instance of a class, as long as you specify the name of the class
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 it was added and removed successfully
Public Function Viewcommands () {
Echo (count (self::$__commands));
}
}
Class User Implementation Interface Irunaction
Class User implements irunaction{
Defining actions that can be invoked
static Private $__actions = Array (AddUser, ModifyUser, Removeuser);
Get the action that can be invoked, do not directly love you in the actual process of $__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) {
Do not understand this function can be used to refer to the manual
Call_user_func (Array ($this, $action), $args);
}
Just a test function.
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);
}
}
Here is the test code
App::load ()->addcommand (new User);
App::load ()->addcommand (new User);
App::load ()->addcommand (new User);
App::load ()->addcommand (new User);
App::load ()->runcommand (AddUser, noangels);
App::load ()->addcommand (new Test);
App::load ()->runcommand (addtest, NULL);
The App class is designed in a single-piece mode, which is the core part of the system. I'm sure you can see the code. The Load method is loaded into the App class instance, which is equivalent to some books in the getinstance static method. He's got Addcommand,runcommand. Removecommand three public methods. RunCommand is the core part. It is also the core launcher of the command chain mode. See the source code for the implementation. The code is clearly written, and it is not to be mentioned again.
The class User,test implements the interface Irunaction, which defines a static private variable $__actions, which contains an array of operations that can be called by the app's RunCommand function.
The following are the operating processes of the system:
App Launch
-------AddCommand, add the class to which the operation you want to run belongs to the app. If you add a class that is designed in a single-piece mode. You can add AddCommand (Singletonclass::load ()) as follows.
AddCommand (New SomeClass)
-------RunCommand. Run the operation. For example, there is an operation AddUser in the user class. I can enable RunCommand ($acttion, $args) directly. Iterate through the $__commands array in the app, If an instance of one of the classes owns the operation, the Runaction function of the instance is called. If you do not add an instance of a class with AddCommand, you are prompted not to define an action to exit.
Runaction in the class user and class test called Call_user_func, a very useful function. Call the corresponding function in the class.
Hint: Explain and example part on this, concrete how you understand, and how to use this thought, see your own understanding, everything must do by oneself. (PS: Can be made into the framework of a single entry file, do not implement MVC is to see what you think of it.)
http://www.bkjia.com/PHPjc/508273.html www.bkjia.com true http://www.bkjia.com/PHPjc/508273.html techarticle Beginners for the design mode there must be a lot of things do not understand, today is just the weekend, take a little time to write a single-piece mode combined with the command chain mode to build the core of the article, possibly ...