Groovy探索之Decorate模式
Decorate模式是Java编程中比较常用的一种模式,有关Decorate模式的基本知识,如概念、使用场合以及如何使用,请大家查看相关文档。本文所要关注如何在Groovy语言中实现Decorate模式,以及这种实现与Java语言实现的区别,使得我们更加深入的理解Groovy语言的动态性,同时学会在Groovy语言中使用Decorate模式。
说到Decorate模式,一个最最常用的例子是咖啡的例子。下面我们来看看在Java语言中怎么实现这个例子的。
首先是一个接口,接口是Java语言面向对象的基础:
public
interface
Coffee {
public
void
descript();
}
public
class
OriginCoffee
implements
Coffee {
public
void
descript() {
}
}
接着,我们来实现加冰的咖啡:
public
class
IceCoffee
implements
Coffee {
private
Coffee
coffee
;
public
IceCoffee(Coffee coffee)
{
this
.
coffee
= coffee;
}
public
void
descript() {
this
.
coffee
.descript();
}
}
下面是加奶咖啡的实现:
public
class
MilkCoffee
implements
Coffee {
private
Coffee
coffee
;
public
MilkCoffee(Coffee coffee)
{
this
.
coffee
= coffee;
}
public
void
descript() {
this
.
coffee
.descript();
}
}
下面,我们来看看
Decorate
模式的客户端实现,首先我们要加奶咖啡:
Coffee coffee =
new
MilkCoffee(
new
OriginCoffee());
coffee.descript();
运行结果为:
Coffee,with Milk
如果我们要加奶又加冰的咖啡:
Coffee coffee =
new
MilkCoffee(
new
IceCoffee(
new
OriginCoffee()));
coffee.descript();
运行结果为
:
Coffee,with ice,with Milk
我们知道,基于
Groovy
语言的动态性,在很多时候都可以省略接口,在实现
Decorate
模式的时候也一样。
首先来看我们的原咖啡的实现:
class
OriginCoffee {
public
descript()
{
print
'Coffee'
}
}
class
IceCoffee {
private
delegate;
public
IceCoffee(delegate)
{
this
.delegate = delegate
}
public
descript()
{
this
.delegate.descript()
print
',with Ice'
}
}
同理来实现加奶咖啡:
class
MilkCoffee {
private
delegate;
public
MilkCoffee(delegate)
{
this
.delegate = delegate
}
public
descript()
{
this
.delegate.descript()
print
',with Milk'
}
}
客户端的使用和
Java
语言一样:
def
coffee =
new
MilkCoffee(
new
IceCoffee(
new
OriginCoffee()))
coffee.descript()
运行结果为:
Coffee,with Ice,with Milk
class
OriginCoffee {
float
payMount
public
pay()
{
this
.payMount =
2
return
payMount
}
public
printPay()
{
println
"Total pay mount: ${this.payMount}"
}
public
descript()
{
print
'Coffee'
}
}
当然,我们依然可以沿用上面的方法,其实现如下:
class
IceCoffee {
float
payMount
private
delegate;
public
IceCoffee(delegate)
{
this
.delegate = delegate
}
public
pay()
{
this
.payMount =
this
.delegate.pay()+
0
.
5
return
this
.payMount
}
public
printPay()
{
println
this
.payMount
}
public
descript()
{
this
.delegate.descript()
print
',with Ice'
}
}
这次实现
Decorate
模式的一般实现模式。我曾经说过,设计模式通过解耦来取得的代码的扩展性,同时也带来了编码的繁琐性。如上面的
IceCoffee
类,需要同时实现三个方法,随着代码的扩展,后面的
MilkCoffee
类、
SugarCoffee
类,都需要同时实现以上的三个方法,十分的繁琐。
class
IceCoffee {
float
payMount
private
delegate;
public
IceCoffee(delegate)
{
this
.delegate = delegate
}
def
invokeMethod(String name,args)
{
if
(name ==
'pay'
)
{
this
.payMount =
this
.delegate.pay()+
0
.
5
return
this
.payMount
}
else
if
(name ==
'descript'
)
{
this
.delegate.descript()
print
',with Ice'
}
else
{
println
this
.payMount
}
}
}
class
MilkCoffee {
float
payMount
private
delegate;
public
MilkCoffee(delegate)
{
this
.delegate = delegate
}
def
invokeMethod(String name,args)
{
if
(name ==
'pay'
)
{
this
.payMount =
this
.delegate.pay()+
1
return
this
.payMount
}
else
if
(name ==
'descript'
)
{
this
.delegate.descript()
print
',with Milk'
}
else
{
println
this
.payMount
}
}
}
最后,我们来看看客户端:
def
coffee =
new
MilkCoffee(
new
IceCoffee(
new
OriginCoffee()))
coffee.descript()
println
''
coffee.pay()
coffee.printPay()
打印结果为:
Coffee,with Milk
3.5
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。