FROM JAVA BUILDERS TO KOTLIN DSLS

FROM JAVA BUILDERS TO KOTLIN DSLS kotlinexpertise.com/java-builde… [翻譯中]java

Introduction DSLs – Domain Specific Languages – are an ever trending topic in Kotlin circles. They allow us to flex some of the most exciting language features while accomplishing more readable and maintainable solutions in our code.app

Today I’d like to show you how to implement a certain kind of DSL – we’re going to be wrapping an existing Java Builder in Kotlin. No doubt you’ve come across the builder pattern in Java before, for example if you’re an Android developer, you must’ve used an AlertDialog.Builder, an OkHttpClient.Builder, or a Retrofit.Builder at some point. Wrapping a builder like this is a good exercise in just pure DSL design. All you have to worry about is designing the API you provide with your wrapper, since all the implementation for whatever your DSL provides its users is already done inside the builder!ide

簡介

DSL – Domain Specific Language首字母縮寫,領域特定語言 – 在Kotlin的圈子曾經是個大熱點。它們容許咱們靈活使用一些最使人興奮的語言特性,同時在代碼中實現更具可讀性和可維護性的解決方案。 今天,我將像你展現如何實現一種DSL - 咱們將用Kotlin來包裝一個Java Builder.毫無疑問的是,你確定曾經用到過建造者模式。舉個例子,假如你是一個Android開發者,你確定曾經用到過AlertDialog.Builder,OkHttpClient.Builder或者Retrofit.Builder。若是咱們想練習設計一個簡單的DSL,包裝一個相似上邊提到的builder是最好的。你只須要關心的是如何更好提供API,由於全部的具體實現都在builder中實現了。flex

Our example case It just so happens that I’m the creator and maintainer of a library that does this very thing, and a small part of its implementation is what we’re going to be using as our example. The original library is the wonderful and hugely popular MaterialDrawer by Mike Penz, which allows you to create complex, good looking, and customized navigation drawers in your application, all via various Builder objects in Java, with no XML writing involved on your part. This is what my library, MaterialDrawerKt provides a convenient Kotlin DSL wrapper for.ui

咱們的樣例

剛巧我是一個開源庫的建立者和維護者,這個庫的一部分實現恰好能夠用做咱們此次的例子。這個庫來源於Mike Penz寫的一個很是強大的開源庫-MaterialDrawer。這個庫讓使用者在本身的應用中建立複雜,美觀,定製化的側邊導航。他由一些列Java寫的建造者對象來實現,不須要寫XML。而我建立和維護的這個庫-MaterialDrawerKt,爲其提供了方便使用的Kotlin DSL包裝。this

DrawerBuilder()
        .withActivity(this)
        .withTranslucentStatusBar(false)
        .withDrawerLayout(R.layout.material_drawer_fits_not)
        .addDrawerItems(
                PrimaryDrawerItem()
                        .withName("Home")
                        .withDescription("Get started here!"),
                PrimaryDrawerItem()
                        .withName("Settings")
                        .withDescription("Tinker around")
        )
        .withOnDrawerItemClickListener { view, position, drawerItem ->
            when(position) {
                0 -> toast("Home clicked")
                1 -> toast("Settings clicked")
            }
            true
        }
        .withOnDrawerListener(object: Drawer.OnDrawerListener {
            override fun onDrawerSlide(drawerView: View?, slideOffset: Float) {
                // Empty
            }

            override fun onDrawerClosed(drawerView: View?) {
                toast("Drawer closed")
            }

            override fun onDrawerOpened(drawerView: View?) {
                toast("Drawer opened")
            }
        })
        .build()

複製代碼
相關文章
相關標籤/搜索