For some people, the content of the article is too simple. This is a tutorial for beginners, because the time is relatively tight (to accompany his wife to go shopping, huh, huh), and the design is not standardized, the code writing is not standard, and bugs are also pointed out by various heroes to facilitate common progress. my level is limited. pai_^
I believe that everyone has read many books or articles about the application of design patterns in php, but few of them directly give examples. Most of them are confused after reading them. If there is no project practice, it is difficult to clarify the design pattern.
To avoid Code complexity, no exception handling is added.
The basic knowledge of the single-piece mode and the command chain mode is as follows:
Copy codeThe Code is as follows:
<? Php
/*
* @ Author: NoAngels
* @ Time: January August 30
*/
Interface IRunAction {
// Obtain the methods defined in the class that can be run in the APP
Static function LoadActions ();
// The entry function in the class calls other functions in the class
Function runAction ($ action, $ args );
}
/*
* Core Components of APP Systems
*/
Class APP {
Static private $ __instance = null;
Static private $ __commands = array ();
Static private $ __flag = 1;
Private function _ construct (){}
// Design the unique instance of the class in single-piece Mode
Static function Load (){
If (self ::$ __ instance = null) self ::$ instance = new APP;
Return self: :__ __ instance;
}
// Add the instance named $ __instance to the APP. Check whether an instance of this class has been added before each time a new command is added.
// If yes, ignore the operation and add it if no.
Public function addCommand ($ role name ){
Foreach (self ::__ commands as $ cmd ){
If (strtolower (get_class ($ cmd) = strtolower (get_class ($ variable name ))){
Self: $ __flag = 0;
Break;
}
}
If (self ::$ __flag = 1) self ::__ __commands [] = $ alias name;
Self: $ __flag = 1;
}
// The entry function of the instance is called in the core part of the command chain mode design.
// First, check whether the operation is allowed to be called in the class. If not, an undefined operation is prompted 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 ");
}
}
// Delete an instance of a class. You only need to specify the class name.
Public function removeCommand ($ className ){
Foreach (self ::__ commands as $ key => $ cmd ){
If (strtolower (get_class ($ cmd) = strtolower ($ className )){
Unset (self ::__ __commands [$ key]);
}
}
}
// You can test whether or not the deletion is successful.
Public function viewCommands (){
Echo (count (self ::__ commands ));
}
}
// Class User implementation interface IRunAction
Class User implements IRunAction {
// Define the operation that can be called
Static private $ __actions = array ('adduser', 'modifyuser', 'removeuser ');
// Obtain the operation that can be called. Do not directly love you in the actual process. $ __actions is designed as a public call.
// Instead, 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 ){
// If you do not understand the usage of this function, refer to the manual.
Call_user_func (array ($ this, $ action), $ args );
}
// Test the function.
Protected function addUser ($ name ){
Echo ($ name );
}
}
// Similar User of the Test class
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 );
}
}
// The following 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 single-piece mode and is the core part of the system. I believe you can see from the code that the Load method loads APP-class instances, which is equivalent to the getInstance static method in some books. he has three public methods: addCommand, runCommand, and removeCommand. runCommand is the core part. it is also the core Startup Program of the command chain mode. for specific implementation, see the source code. the code is clearly written, so I will not repeat it here.
The class User and Test implement the interface IRunAction. Both Classes define a static private variable $ __actions, which is an array and contains operations that can be called by the runCommand function of the APP.
The following is the system running process:
APP startup
------- AddCommand: adds the class of the operation to be executed to the APP. if the added class is designed in single-piece mode. you can add addCommand (SingletonClass: Load () as follows ()). otherwise, you can call
AddCommand (new someClass)
------- RunCommand. operation. for example, there is an operation addUser in the User class. I can directly enable runCommand ($ acttion, $ args ). loop through the $ __commands array in the APP. If an instance of a class has this operation, the runAction function of the instance is called. if you have not added an instance of a class using addCommand, an undefined operation is prompted to exit.
RunAction in class User and class Test calls call_user_func, a very useful function. Calls the corresponding function in this class.
Tip: the explanations and examples are here. How do you understand and use this idea depends on your understanding. You must do everything yourself. (ps: it can be made into a single entry file in the framework. If MVC is not implemented, it depends on what you think .)
The actual running effect is as follows:
If you have any questions, contact me.
I will write some articles for you later.