SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 30 day free trial to unlock unlimited reading.
5.
Copyright (C) 2014 NTT Software Corp.
traitの定義と使⽤用
• trait T { ... }
• Traitは直接インスタンス化できない
• def x = new T() // ×
• Traitを静的に実装するクラスをインスタンス化
class C implements T {} // like interface
def y = new C() // OK
• Traitを実装するプロキシを動的に⽣生成
def z = new String() as T
def z2 = new String().withTraits(T1,T2)
assert ! (z instanceof String)
assert z instanceof T
14年6月21日土曜日
6.
Copyright (C) 2014 NTT Software Corp.
traitの定義と使⽤用
• trait T { ... }
• Traitは直接インスタンス化できない
• def x = new T() // ×
• Traitを静的に実装するクラスをインスタンス化
class C implements T {} // like interface
def y = new C() // OK
• Traitを実装するプロキシを動的に⽣生成
def z = new String() as T
def z2 = new String().withTraits(T1,T2)
assert ! (z instanceof String)
assert z instanceof T
「as Interface」でプロキシ⽣生成す
るのはGroovyにもとからある機能
14年6月21日土曜日
7.
Copyright (C) 2014 NTT Software Corp.
サンプルコード
trait
Helloable
{//
helloを表示する能力
void
hello()
{
println
"hello
"+this.toString()
}
void
hello2()
{
println
"hello
"+proxyTarget.toString()
}
}
class
World
implements
Helloable
{
String
toString(){
"world"}
}
x
=
new
World()
x.hello()
//
==>
hello
world
y
=
"abc"
as
Helloable
y.hello()
//
==>
hello
String1_groovyProxy@5560a7b1
z
=
"def"
as
Helloable
z.hello2()
//
==>
hello
def
Traitを静的に実装するクラ
スをインスタンス化
Traitを実装するプロキシを動的に⽣生成。インスタンスへの動的メソッド追加ぽいことが
できる。状態も持てる。ただしStringそのものではなくプロキシ経由。
14年6月21日土曜日
8.
Copyright (C) 2014 NTT Software Corp.
proxyTarget
• 動的に⽣生成されたTraitを実装するプロキシのみが保持す
るプロパティ
• プロキシしている元のオブジェクトを指す
• def x = new String()
def y = x.withTraits(T1,T2)
assert x == y.proxyTarget
assert y.proxyTarget instanceof String
http://jira.codehaus.org/browse/GROOVY-6692
14年6月21日土曜日
29.
Copyright (C) 2014 NTT Software Corp.
コード例例 Wikiエンジン(2)
class
WikiEngine1
extends
HtmlConverter
implements
Bold,
Heading,
UL
{}
class
WikiEngine2
extends
HtmlConverter
implements
UL,
Bold,
Heading
{}
println
new
WikiEngine1().convert(text)
println
new
WikiEngine2().convert(text)
println
new
HtmlConverter().withTraits(Bold,
Heading,
UL).convert(text)
println
new
HtmlConverter().withTraits(UL,
Bold,
Heading).convert(text)
##this
is
Heading1
#this
is
Heading2
This
is
sample
document
for
wiki
engine.
If
you
wan
to
**emphasize**,
use
**This**
markup.
#This
is
heading3
This
is
sample
document
for
wiki
engine.
*
list1
*
list2
*
list3
This
is
end.
**hoge**
<html>
<h2>this
is
Heading1</h2>
<h1>this
is
Heading2</h1>
This
is
sample
document
for
wiki
engine.
If
you
wan
to
<b>emphasize</b>,
use
<b>This</b>
markup.
<h1>This
is
heading3</h1>
This
is
sample
document
for
wiki
engine.
<ul>
<li>list1</li>
<li>list2</li>
<li>list3</li>
</ul>
This
is
end.
<b>hoge</b>
</html>
<ul>
<li>*hoge**</li>
</ul>
14年6月21日土曜日