PHP single-piece mode combined with command chain mode instructions _php tips

Source: Internet
Author: User
Tags exception handling php books
Maybe for some people, the article is too simple, this is for beginners tutorial, because the time is relatively tight (to accompany the wife shopping, hehe), where the design is not standardized, code writing is not standardized, bugs and so on 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:
Copy Code code as follows:

<?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);
}
}
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 with a single piece pattern, which is the core part of the system. I'm sure you'll see the code. The Load method is an instance of loading the app class, which is equivalent to the getinstance static method in some books. He has Addcommand,runcommand, Removecommand three public methods. The RunCommand is the core. It is also the core launcher of the command chain mode. See the source code for the specific implementation. The code has been written very clearly, and no longer repeat.
Class User,test implements the interface Irunaction, which defines a static private variable $__actions as an array containing operations that can be called by the app's RunCommand function.

The following is the system's running process:

App Startup
-------AddCommand, add the class to which the operation you want to run is added to the app. If you add a class that is designed in a single piece mode. You can add AddCommand (Singletonclass::load ()) as follows. Otherwise it can be lowered

AddCommand (New SomeClass)
-------RunCommand. Run the 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 one of the classes owns the operation, the Runaction function of the instance is invoked. If you do not add an instance of a class to the AddCommand, you are prompted for an undefined action and exit.
The runaction in class user and class Test invokes Call_user_func, a very useful function. Call the corresponding function in the class.

Hint: The explanation and the example part is here, how do you understand, and how to use the thought, depends on your own understanding, all things must be done by themselves. (PS: Can be made into a single entry in the framework of the file, the implementation of MVC does not really see what you think of it.)

The actual operation effect is as follows:

Limited to the language level, what do not understand can contact me.
Later, there is time to write some articles for everyone.

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.