The learning-responsibility chain model of PHP core learning-Design pattern

Source: Internet
Author: User
Tags stub

One, what is the responsibility chain model

The responsibility chain pattern is an object's behavior pattern. In the mode of responsibility chain, many objects are connected by each object's reference to their house to form a chain. The request is passed on this chain until an object on the chain decides to process the request. The client who issued the request does not know which object on the chain ultimately handles the request, which allows the system to dynamically rearrange and distribute responsibilities without affecting the client.
The main features of the responsibility chain model are:
1, there are multiple objects to work together on a task to deal with.
2. These objects use chained storage structures to form a chain in which each object knows its next object.
3, an object to process the task, you can add some action after the object passed to the next task. You can also end the processing of a task on this object and end the task.
3, the client is responsible for assembling chain-type structure, but the client does not need to care about who ultimately handles the task.
These explanations are the Java aspects of interpretation, different languages, in fact, the design pattern is still a little different, and now we are using the PHP implementation of the responsibility chain design pattern, we use a scene to bring into our design pattern learning, the use of responsibility chain simulation multi-step user registration process.

Second, Pattern code
User Registration Processor Excuse

Abstract class Userreg
{public

    $step = "Step1";/current state, this is important public
    $next =false;//the next node public
    $last = false;//the previous node public

    function Setnextstep ($object)
    {
        $this->next= $object;
        $object->last= $this;
    }

    The Public Function Stepnext ($user) {

        //will process

        if ($user->step== $this->step) only if user's state is equal to its own mystate /represents the state in which the current state is to be processed
        {
            //To determine if its next node exists, and if it exists, give control to the next node if
            ($this->next)
            {
                $user-> step= $this->next->step;//control return
                $user;
            }
        else
        {
            if ($this->next)
              return  $this->next->stepnext ($user);



}}}

User Registration First Step processing interface

Class UserInfo extends Userreg
{public
    $step = "Step1";
    Public Function Stepnext ($user)
    {

        //Suppose this step requires the user's initial information to be saved to the database. Here we save to session

        if ($user ==null)
        {
            $user =new userentity ();
            $user->user_name=$_post["user_name"];
            $user->user_pass=$_post["User_pass"];

            Simulate user warehousing
            $_session["user"]=json_encode ($user);
        }

         $get _user=parent::stepnext ($user); Todo:change the autogenerated stub
        if ($get _user)
        {
            //Simulate save user
            $_session["user"]=json_encode ($ Get_user);}}

User registers the second step processing interface

Class Userphone extends Userreg
{public
    $step = "Step2";
    Public Function Stepnext ($user)
    {
        $user->user_phone=$_post[' User_phone '];
        $get _user=parent::stepnext ($user); Todo:change the autogenerated stub
        if ($get _user)
        {
            $_session["user"]=json_encode ($get _user);
        }
}

User Registration Third Step interface

Class Usersuccess extends Userreg
{public
    $step = "Step3";
    Public Function Stepnext ($user)
    {
        //continue to perform business
        $get _user=parent::stepnext ($user);//Todo:change the Autogenerated stub
        if ($get _user)
            $_session["user"]=json_encode ($get _user);
    }

Client Calls

        $userInfo =new userInfo ();
        $userPhone =new Userphone ();
        $userSuccess =new usersuccess ();

        $userInfo->setnextstep ($userPhone);
        $userPhone->setnextstep ($userSuccess);

        $userInfo->stepnext (GetUser ());

When you perform to the second step out of the time, re-register, the system or from the previous exit status to continue to register, simple simulation is almost like this, only provide design mode ideas.

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.