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

Groovy探索之Adapater模式

发布时间:2020-12-14 16:59:54 所属栏目:大数据 来源:网络整理
导读:??????????????????????????? Groovy探索之Adapater模式 ? ? 我们知道,Adpater模式的一个作用体现在对旧代码的改造上。比如,像我在前面的文字中讲到的控制灯开关的问题。 有这么一个灯的类: class Light { ? ?????? def turnOn() ?????? { ??? ?????? pri
??????????????????????????? Groovy探索之Adapater模式
?
?
我们知道,Adpater模式的一个作用体现在对旧代码的改造上。比如,像我在前面的文字中讲到的控制灯开关的问题。
有这么一个灯的类:
class Light {
?
?????? def turnOn()
?????? {
??? ?????? println 'The light is on,you can do anything……'
?????? }
?????? def turnOff()
?????? {
??? ?????? println 'Be careful!the light is power off……'
?????? }
?
}
?
?
很简单的一个对象,需要注意的是,这个对象存在于我们的一个引用包里,比如某个JAR包。这使得我们不能对这个类就进行改造。
现在的需要是,我们要创建一个Button类,希望它既能控制Light类对象的开关。同时,也可以控制其他类,如我们自己创建的风扇类(Fan),冰箱类,电视机类等等的开关。
很明显,此类问题的解决方案是:首先创建一个接口,让灯类啊,风扇类、冰箱类等等都实现这个接口,然后把这个接口的实现注入给Button类,就可以实现上面的功能。
这个思路是对的,但现在的难题是我们不能修改Light类,使它实现我们的接口。怎么办?这时候,我们的Adapter模式就派上用场了。
在Java语言的解决方案中,我们使用Adapter模式来解决上述问题,首先要创建一个接口:
public interface Switch
{
??????? public void turnOn();
??????? public void turnOff();
}
?
?
然后需要一个中间类来实现这个接口,并且把它的功能委派给Light类:
public class LightAdapter implements Switch
{
??????? private Light light = new Light();
??????? public void turnOn()
??????? {
??????? ?????? light.turnOn();
}
public void turnOff()
{
?????? light.turnOff();
}
}
?
?
最后来实现Button类:
public class Button
{
??????? private Switch switch;
??????? public Button(Switch switch)
??????? {
??????? ?????? this.switch = switch;
}
public void pressOn()
{
?????? this.switch.trunOn();
}
public void pressOff()
{
?????? this.switch.turnOff();
}
}
?
?
这样,我们的Button类通过控制 LightAdapter 类间接控制了 Light 类,而其他的风扇类、空调类等等,只是它们实现了 Switch 接口,就都能被 Button 类控制。
上面就是 Java 语言对于 Adapter 模式的一个解决方案。虽然解决方案很精巧,但绕了一些圈子,使用了一些冗余代码。
通过上面的代码,我们可以看到,本例对于 Adapter 模式的使用是通过两个委派动作来完成的。即 LightAdapter 类把动作委派给 Light 类,而 Button 类又把动作委派给 LightAdapter 类等各类。
我们知道, Java 语言受语言的限制,对委派的使用很低级。我们可以这么说,正是由于 Java 语言不能很好的使用委派,才导致了实现 Adapter 模式的难度。
闲话少说,我们来看看 Groovy 语言是如何实现上述的 Adapter 模式的:
class Button {
???
??? def delegate
???
??? def Button(delegate)
??? {
?????? this .delegate = delegate
??? }
???
??? def turnOn()
??? {
?????? try
?????? {
?????????? this .delegate.turnOn()
?????? }
?????? catch (MissingMethodException e)
?????? {
?????????? println "Your class: ${this.delegate.class.name} need to implement turnOn and turnOff function!"
?????? }
??? }
???
??? def turnOff()
??? {
?????? try
?????? {
?????????? this .delegate.turnOff()
?????? }
?????? catch (MissingMethodException e)
?????? {
?????????? println "Your class: ${this.delegate.class.name} need to implement turnOn and turnOff function!"
?????? }
??? }
?
}
?
可以看到,在 Groovy 语言中,实现上述的 Adapter 模式根本不需要 Switch 接口,也不需要 LightAdapter 类,直接在 Button 类里把功能委派给各个类对象就行了。当然,我们能在 Groovy 语言中实现这样的委派,是由 Groovy 语言的动态性决定的。
当然,使用 Button 类的前提是要有一个契约:各个功能类,如 Light Fan 等等,都要实现 turnOn turnOff 方法,不然就提出警告信息。
现在,我们来测试一下 Groovy 语言的 Button 类:
??? ? def button = new Button( new Light())
??? ?
??? ?button.turnOn()
??? ?
?button.turnOff()
?
?
运行结果为:
The light is on,you can do anything……
Be careful!the light is power off……
?
?
如果我们的某一个类没有实现turnOn和turnOff方法,如下:
?????? def button = new Button( new Fan())
?
??? button.turnOn()
?
运行结果为:
Your class: Fan need to implement turnOn and turnOff function!

(编辑:李大同)

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

    推荐文章
      热点阅读