Share
Embed
Transcript
18 slides
-
Copyright © Sansan, Inc. All rights reserved. > Kotlinのclass delegation
Jumpei Yamamoto 2016.2.26 Kotlin birthday party #jkug -
Copyright © Sansan, Inc. All rights reserved. > ⾃自⼰己紹介 -
⼭山本純平 - Sansan株式会社 Eight事業部 - EightのAndroidアプリの開発 - twitter: @boohbah - github: https://github.com/yamamotoj -
Copyright © Sansan, Inc. All rights reserved. > Class delegationとは
interface SampleInterface { fun printStr(str: String): Unit } class SampleImpl : SampleInterface { override fun printStr(str: String) { print(str) } } class SampleClass(impl: SampleImpl) : SampleInterface by impl { // printStr()͕͢Ͱʹ࣮͞Ε͍ͯΔ } -
Copyright © Sansan, Inc. All rights reserved. > Javaで考えると public
class SampleClass implements SampleInterface { private final SampleImpl impl; SampleClass(SampleImpl impl) { this.impl = impl; } @Override public void printStr(@NotNull String str) { impl.printStr(str); } } このメソッドを⾃自動的に⽣生成してくれている -
Copyright © Sansan, Inc. All rights reserved. > Subscriptionの管理理 class
SomeActivity : Activity() { val compositeSubscription = AndroidCompositeSubscription() fun onClick() { loadData() .lift(OperatorAddToCompositeSubscription<Strin g>(compositeSubscription)) .subscribe { // Do something } } override fun onPause() { super.onPause() compositeSubscription.unsubscribe() } } com.cookpad.android.rxt4aをつかった実装 -
Copyright © Sansan, Inc. All rights reserved. > Subscriptionの管理理 class
SomeActivity : Activity() { val compositeSubscription = AndroidCompositeSubscription() fun onClick() { loadData() .addSubscription() .subscribe { // Do something } } override fun onPause() { super.onPause() compositeSubscription.unsubscribe() } fun <T>Observable.addSubscription():Observable<T> = lift(OperatorAddToCompositeSubscription<T>(compositeSubscrip tion)) } 拡張関数で置き換え -
Copyright © Sansan, Inc. All rights reserved. > UnsubscribeDelegateを定義 interface
UnsubscribeDelegate{ fun <T>Observable.addSubscription():Observable<T> fun unsubscribe() } class UnsubscribeDelegateImpl : UnsubscribeDelegate{ val compositeSubscription = AndroidCompositeSubscription() override fun <T>Observable.addSubscription():Observable<T> = lift(OperatorAddToCompositeSubscription<T>(compositeSubscri ption)) override fun unsubscribe() = compositeSubscription.unsubscribe() } -
Copyright © Sansan, Inc. All rights reserved. > Subscriptionの管理理 class
SomeActivity : Activity(), UnsubscribeDelegate by UnsubscribeDelegateImpl { fun onClick() { loadData() .addSubscription() .subscribe { // Do something } } override fun onPause() { super.onPause() unsubscribe() } } -
Copyright © Sansan, Inc. All rights reserved. > Presenter classでViewとのやりとりを⾏行行いたい
class SomePresenter( val rootView:View, val model:SomeModel){ val button = rootView.findById(R.id.button) init{ // onPause()Ͱunsubscribe()͢Δඞཁ͕͋Δ button.setOnClick = model .loadData() .subscribe{ update(it) } } } class SomeActivity : Activity(){ override fun onCreate(bundle:Bundle){ setContentView(R.layout.activity) val rootView = findViewById(android.R.id.content) val presenter = SomePresenter(rootView, model) } } -
Copyright © Sansan, Inc. All rights reserved. > UnsubscribeDelegateに階層構造を持たせる。 interface
UnsubscribeDelegate{ fun <T>Observable.addSubscription():Observable<T> fun unsubscribe() fun addChildDelegate(child:UnsubscribeDelegate) } class UnsubscribeDelegateImpl : UnsubscribeDelegate{ val compositeSubscription = AndroidCompositeSubscription() val children = ArrayList<UnsubscribeDelegate> override fun <T>Observable.addSubscription():Observable<T> = lift(OperatorAddToCompositeSubscription<T>(compositeSubscription)) override fun unsubscribe() { compositeSubscription.unsubscribe() children.forEach{ it. unsubscribe() } } override fun addChildDelegate(child:UnsubscribeDelegate) = children.add(child) } -
Copyright © Sansan, Inc. All rights reserved. > Presenter側 class
SomePresenter : UnsubscribeDelegate by UnsubscribeDelegateImpl( val rootView:View, val model:SomeModel){ val button = rootView.findById(R.id.button) init{ // onPause()Ͱunsubscribe()͢Δඞཁ͕͋Δ button.setOnClick = model .loadData() .addSubscription() .subscribe{ update(it) } } } -
Copyright © Sansan, Inc. All rights reserved. > Activity側 class
SomeActivity : Activity(), UnsubscribeDelegate by UnsubscribeDelegateImpl { override fun onCreate(bundle:Bundle){ setContentView(R.layout.activity) val rootView = findViewById(android.R.id.content) val presenter = SomePresenter(rootView, model) addChildDelegate(presenter) } override fun onPause(){ unsubscribe() } } -
Copyright © Sansan, Inc. All rights reserved. > unsubscribe()の伝播 SomeActivity
SomePresenter UnsubscribeDelegate UnsubscribeDelegate unsubscribe() unsubscribe() child Unsubscribeの処理理だけをツリー構造で 管理理して伝播させる -
Copyright © Sansan, Inc. All rights reserved. > まとめ -
class delegation便便利利 - class delegationを利利⽤用したフレームワークでアプリの開 発はもっと変わりそう