The responsibility chain model of groovy exploration

Source: Internet
Author: User

The responsibility chain pattern has many corresponding examples in reality.

For example, a company has a,b,c,d four project teams, each adjacent to each other. One day, a customer called in to say that we have a certain project has a problem, please help solve. The phone was answered by a project team, and a group of people listened and said it was not the project we were responsible for and we gave it to the B project team. b The Group of people listen, also said, this is not our project team is responsible for, we give it to the C project team. C Project team to listen to, said, this is our responsibility for the project, we have to deal with it.

The above example is a very vivid representation of the process of a responsibility chain pattern: a project or process has multiple similar responsibilities, which are handled separately by the respective processing modules; Our solution is to form a chain of all of these processing modules, and then, when a responsibility occurs, it flows along the chain, If the current processing module does not handle the responsibility, the responsibility is transferred to the next processing module of the chain, and so on, until the responsibility finds the processing module.

We all love to read "Journey to the World" this novel, which has a plot is "Monkey King War Two Lang Shen", I believe we are very familiar with.

This is what the novel describes:

Wu empty see own Lair was destroyed, in the heart a panic, become sparrows want to run, two Lang God shake body became to catch Sparrow Eagle,

Shake the wings to peck the Sparrow, Wu empty hurried to become a big Luci bird, rushed to the sky, two lang God hurried into a sea crane,

Into the clouds to flutter, Wu empty a see swish flew into the water, into a fish.

Two lang God from the devotion saw Wu empty, become an osprey, waiting on the water, Wu empty See,

Quickly change a snake, flee to the shore, and then become a flower bustard, stand on the reed. The two Lang God saw him become too lowly, also not to ignore him,

Change back to the original appearance, take out the slingshot, toward the flower bustard on the dozen, the Wu empty dozen stand unsteady. "

This is one of the two of the changes in the war, the description is wonderful. Among them, the monkey King changes into "Sparrow", "Big Luci Bird", "fish", trying to escape the eyes of the two lang God, to escape the purpose. And the two lang God is also transformed into "Eagle", "Sea Crane", "Osprey" to the sun Wukong and the opposite.

From the above analysis, if the two lang God to deal with the Monkey King as a process, then the Monkey King's three changes is three different responsibilities, and the two lang God also through three different processing modules to deal with them.

Then, we can through the chain of responsibility model to simulate the two lang God and the Monkey King of this period of change war.

First of all, Erlang's first processing module is "Eagle":

class Hawk {

def next

def Hawk(next)
{
this.next = next
}

def fight(wukong)
{
if(wukong == 'sparrow')
{
println 'hawk eat sparrow!'
}
else
{
this.next.fight(wukong)
}
}

}

In this "Hawk" class, only one property is "next", pointing to its next processing module. When this class is initialized, determine what its next processing module is.

And then, we look at its "fight" approach, there is an incoming parameter "Wukong", represents the Monkey King's current changes, if its current changes are "sparrows", it is "Hawk" processing; if not, the "Hawk" class is passed to the next processing module to handle.

Of course, Erlang's second processing module, "The Sea Crane" function is similar to the first processing module:

class Crane {

def next

def Crane(next)
{
this.next = next
}

def fight(wukong)
{
if(wukong == 'cormorant')
{
println 'crane eat cormorant!'
}
else
{
this.next.fight(wukong)
}
}

}

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.