加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy探索 自定义Range 三 自定义Range与责任链模式

发布时间:2020-12-14 17:08:54 所属栏目:大数据 来源:网络整理
导读:??????? Groovy探索 自定义Range 三 自定义Range与责任链模式 ? ? 责任链模式也是我们比较常用的一种模式,我在《Groovy探索之责任链模式》中有个探索。大家也可以在网上搜索,应该有很多这方面的文章。 在这里,我们将使用自定义的Range类来实现责任链模式

??????? Groovy探索 自定义Range 三 自定义Range与责任链模式

?

?

责任链模式也是我们比较常用的一种模式,我在《Groovy探索之责任链模式》中有个探索。大家也可以在网上搜索,应该有很多这方面的文章。

在这里,我们将使用自定义的Range类来实现责任链模式,使用的例子还是在《Groovy探索之责任链模式》一文中所谈到的"孙悟空大战二郎神"的这个情节。这样,我们可以把这两篇的文字结合起来看,使得我们能够对比这两种开发方式。使得我们能够深入的理解自定义Range类的使用。

在"孙悟空大战二郎神"这个情节里,重点讲述的是两人的变幻大战:其中,孙悟空分别变幻成"麻雀"、"大鹚鸟"和"雨",企图躲过二郎神的追杀;而二郎神则分别变成"鹰"、"大海鹤"和"鱼鹰"来吃掉孙悟空的相应的变幻。原文情节可以在《Groovy探索之责任链模式》看到。

这个情节使用责任链模式来实现是易于理解的,但是使用自定义的Range类来实现,则更有Groovy语言的编程风格。

现在,我们来看看使用自定义的Range类来实现的具体代码。

首先,我们要实现的还是自定义的Range类:

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

??? }

???

?

}

?

这个Base类和《Groovy探索 自定义Range 二 自定义Range类与Decorate模式》中实现的Base类没有什么区别,除了index属性的赋值不同以外。

这就提醒我们,是否可以做一个通用的自定义Range类?

接着是Factory类:

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()

??? ? }

? }

?

}

?

?

这个Factory类跟前面所实现的Factory类就不一样了,因为我们的type输入的孙悟空的变幻,而各个责任模块却是二郎神的变幻,这就导致了我们的这个自定义Range在扩展上的困难。

?

然后,我们需要实现各个责任模块了,当然了,这些类无一例外的都要继承Base类了。我们先来看看Crane类:

package range.chain;

?

?

class Crane extends Base{

???

??? def Crane()

??? {

?????? this.type = 'cormorant'

?????? this.getIndex()

??? }

???

??? def fight()

??? {

?????? println 'crane eat cormorant!'

??? }

?

}

?

这个类跟责任链模式所对应的Crane类比较起来,就更为简单一些,这也是我们增加了一个自定义Base类后得到的一点点好处。同样,我们来看看Hawk类:

package range.chain;

?

?

class Hawk extends Base{

???

??? def Hawk()

??? {

?????? this.type = 'sparrow'

?????? this.getIndex()

??? }

???

??? def fight()

??? {

?????? println 'hawk eat sparrow!'

??? }

?

}

?

?

跟Crane类的实现一模一样,最后是Osprey类的实现:

package range.chain;

?

?

class Osprey extends Base{

???

??? def Osprey()

??? {

?????? this.type = 'fish'

?????? this.getIndex()

??? }

???

??? def fight()

??? {

?????? println 'osprey eat fish'

??? }

?

}

?

?

现在,我们就可以来测试这种实现方式了:

?

??? ? def crane = new Crane()

??? ?

??? ? def osprey = new Osprey()

??? ?

??? ? def wukong = 'fish'

??? ?

??? ? (crane..osprey).find{

?????? ? Base.types[it.index] == wukong

?????? ?

??? ? }.fight()

???

?

运行结果为:

osprey eat fish

?

到现在为止,我们已经完全的实现了"孙悟空大战二郎神"的情节。比较使用责任链模式的实现和使用自定义Range类的实现,我们感觉使用自定义的Range类的实现在扩展性上要比使用责任链模式的实现要差。因为我们要扩展使用自定义的Range类的实现,我们不得不修改Factory类,而使用责任链模式的实现则不需要这样做。

但是,在使用自定义的Range类的实现中,我们的各个责任模块则更为简单,不需要往下传递责任,这是自定义Range类的功能。

除此之外,如果各个责任模块有多个方法。比如,我们的"Crane"类、"Hawk"类和"Osprey"类各自比上面的例子多了一个"descrition"方法。

则使用责任链模式的实现的客户端为:

?

??? ? def wukong = 'fish'

??? ?

??? ? def erlangshen = new Hawk(new Osprey(new Crane(new Other())))

??? ?

??? ? erlangshen.fight(wukong)

???

?? erlangshen. descrition(wukong)

?

这里的两个方法的调用,则责任需要传递两次,而自定义Range类的实现的客户端为:

?

??? ? def crane = new Crane()

??? ?

??? ? def osprey = new Osprey()

??? ?

??? ? def wukong = 'fish'

??? ?

??? ? def erlangshen = (crane..osprey).find{

?????? ? Base.types[it.index] == wukong

?????? ?

??? ? }

??? ?

??? ? erlangshen.fight()

??? ?

??? ? erlangshen.description()

???

?

而用自定义Range类的实现则只需要传递一次,似乎在算法上更为有效率。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读