Groovy Explore custom range three custom range and responsibility chain mode

Source: Internet
Author: User

The responsibility chain model is also one of the more common patterns that I have explored in the "Groovy Quest for Responsibility chain model". We can also search on the Internet, there should be a lot of articles in this regard.

Here, we will use the custom range class to implement the responsibility chain pattern, and the example used is the episode of "Monkey King vs. Lang Shen", as discussed in the article "The magic of the responsibility chain pattern" in groovy exploration. In this way, we can combine these two pieces of text to look, so that we can compare the two development methods. Allows us to gain an in-depth understanding of the use of custom range classes.

In the episode of "Monkey King vs. the two lang gods", the focus is on the two people's changing wars: Among them, the Monkey King changed into "Sparrow", "Big Luci Bird" and "rain", tried to escape the two Lang God's pursuit, and the two lang God is changed into "Eagle", "Sea Crane" and "osprey" to eat the Sun Wukong's corresponding changes. The original episode can be seen in the "Groovy Quest for Responsibility chain model".

This scenario is easy to understand using the responsibility chain pattern, but is implemented using a custom range class, with a more groovy language programming style.

Now let's take a look at the specific code that is implemented using the custom range class.

First, we're going to implement the custom range class:

package range.chain;

class Base implements Comparable{

static protected types = ['cormorant','sparrow','fish']
protected int index = 0
protected type
protected getIndex()
{
this.index = this.types.indexOf(type)
}
def next()
{
Factory.getObject(types[(index+1)%types.size()])
}
def previous()
{
Factory.getObject(types[index-1])
}
int compareTo(Object other)
{
index<=>other.index
}
}

This base class is no different from the base class implemented in the groovy Explore custom range class and decorate mode, except that the Index property is not assigned the same value.

This reminds us if we can make a generic custom range class?

followed by the factory class:

package range.chain;

class Factory {
static def getObject(type)
{
if(type == 'cormorant')
{
return new Crane()
}
else if(type == 'sparrow')
{
return new Hawk()
}
else if(type == 'fish')
{
return new Osprey()
}
}
}

This factory class is not the same as the previous factory class, because the change of the sun Wukong of our type input, and each responsibility module is the change of the two-Lang God, which leads to the difficulty of our custom range on the expansion.

Then we need to implement the various responsibility modules, and of course, these classes will inherit the base class without exception. Let's take a look at the Crane class:

package range.chain;

class Crane extends Base{
def Crane()
{
this.type = 'cormorant'
this.getIndex()
}
def fight()
{
println 'crane eat cormorant!'
}
}

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.