Php single-piece mode combined with command chain mode instructions

Source: Internet
Author: User
Beginners must have a lot to understand about the design pattern. just this weekend, I took some time to write a single-piece pattern and the command chain pattern to create the core of the system. for some people, the content of this article is too simple. this is a tutorial for beginners, because it is relatively time-consuming (to accompany his wife to go shopping, haha), where the design is not standardized and the code writing is not standard, bug and so on. we hope all the heroes can point out this to help everyone make 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:

The code is as follows:


/*
* @ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.