Groovy学习记录06

一、Closure关于DSL练习——主要是针对Closure的delegate运用

1.1 示例一
package study.closure.demo1

/**
 * 闭包示例(this/owner/delegate)
 */
class Animal {
    String name
    int age

    void action(String action) {
        println "${action}很有难度"
    }


    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

package study.closure.demo1

/**
 * 闭包示例(this/owner/delegate)
 */
class Main {

    void action(String action) {
        println "${action}很容易啊"
    }

    def closure = {
        name = "老虎"
        age = 26
        action "游泳"
    }

    static void main(args) {
        Main main = new Main()
        Animal animal = new Animal(name: "鳄鱼", age: 12)
        println animal.toString() //Animal{name='鳄鱼', age=12}
        //将animal交给main.closure.delegate代理
        main.closure.delegate = animal
        main.closure.resolveStrategy=Closure.DELEGATE_FIRST
        //闭包运行,给代理对象赋值
        main.closure()
        //main.closure.resolveStrategy=Closure.DELEGATE_FIRST   游泳很有难度
        //OWNER_FIRST  resolveStrategy默认值  游泳很容易啊
        println animal.toString()//Animal{name='老虎', age=26}
    }
}
1.2 示例一
package study.closure.demo2

class View {
    private Closure onClickListener
    Timer timer

    View() {
        //定时器
        timer = new Timer()
        //定时器的调度
        timer.schedule(new TimerTask() {
            @Override
            void run() {
                preOnClick()
            }
            //延迟1s之后触发,每隔3s触发一次
        }, 1000, 3000)
    }

    /**
     * 设置事件的闭包
     * @param closure
     */
    void setOnClickListener(Closure closure) {
        this.onClickListener = closure
    }

    /**
     * 执行事件的回调
     */
    private void preOnClick() {
        if (onClickListener != null) {
            //说明闭包是有参数的
            onClickListener.call(this)
        }
    }

    @Override
    String toString() {
        return "this is View"
    }
}

package study.closure.demo2

class MainView {
    public static void main(String[] args) {
        View view = new View()
        view.setOnClickListener { println it.toString() }
    }
}

二、模仿AndroidStudio定义DSL

2.1 通过方法的方式
package study.closure.imitate_android

//android {
//    compileSdkVersion 25
//    buildToolsVersion "25.0.2"
//
//    defaultConfig {
//        minSdkVersion 15
//        targetSdkVersion 25
//        versionCode 1
//        versionName "1.0"
//    }
//}
/**
 * 模仿app/build.gradle的实现
 *
 */
class Android {
    private int mCompileSdkVersion
    private String mBuildToolsVersion
    private DefaultConfig defaultConfig

    Android() {
        this.defaultConfig = new DefaultConfig()
    }

    int getCompileSdkVersion() {
        return mCompileSdkVersion
    }

    void setCompileSdkVersion(int compileSdkVersion) {
        this.mCompileSdkVersion = compileSdkVersion
    }

    String getBuildToolsVersion() {
        return mBuildToolsVersion
    }

    void setBuildToolsVersion(String buildToolsVersion) {
        this.mBuildToolsVersion = buildToolsVersion
    }

    void setDefaultConfig(Closure closure) {
        closure.setDelegate(defaultConfig)
        closure.setResolveStrategy(Closure.DELEGATE_FIRST)
        closure.call()
    }


    @Override
    public String toString() {
        return "Android{" +
                "mCompileSdkVersion=" + mCompileSdkVersion +
                ", mBuildToolsVersion='" + mBuildToolsVersion + '\'' +
                ", defaultConfig=" + defaultConfig +
                '}';
    }
}

package study.closure.imitate_android

//android {
//    compileSdkVersion 25
//    buildToolsVersion "25.0.2"
//
//    defaultConfig {
//        minSdkVersion 15
//        targetSdkVersion 25
//        versionCode 1
//        versionName "1.0"
//    }
//}
class DefaultConfig {
    private int mMinSdkVersion
    private int mTargetSdkVersion
    private int mVersionCode
    private String mVersionName

    int getMinSdkVersion() {
        return mMinSdkVersion
    }

    void setMinSdkVersion(int minSdkVersion) {
        this.mMinSdkVersion = minSdkVersion
    }

    int getTargetSdkVersion() {
        return mTargetSdkVersion
    }

    void setTargetSdkVersion(int targetSdkVersion) {
        this.mTargetSdkVersion = targetSdkVersion
    }

    int getVersionCode() {
        return mVersionCode
    }

    void setVersionCode(int versionCode) {
        this.mVersionCode = versionCode
    }

    String getVersionName() {
        return mVersionName
    }

    void setVersionName(String versionName) {
        this.mVersionName = versionName
    }




    @Override
    public String toString() {
        return "DefaultConfig{" +
                "mMinSdkVersion=" + mMinSdkVersion +
                ", mTargetSdkVersion=" + mTargetSdkVersion +
                ", mVersionCode=" + mVersionCode +
                ", mVersionName='" + mVersionName + '\'' +
                '}';
    }
}

2.1 通过属性的方式
package study.closure.imitate_android

class FieldAndroid {
    int compileSdkVersion
    String buildToolsVersion
    FieldDefaultConfig defaultConfig

    FieldAndroid() {
        this.defaultConfig = new FieldDefaultConfig()
    }


    void defaultConfig(Closure closure) {
        closure.setDelegate(defaultConfig)
        closure.setResolveStrategy(Closure.DELEGATE_FIRST)
        closure.call()
    }


    @Override
    public String toString() {
        return "FieldAndroid{" +
                "compileSdkVersion=" + compileSdkVersion +
                ", buildToolsVersion='" + buildToolsVersion + '\'' +
                ", defaultConfig=" + defaultConfig +
                '}';
    }
}
package study.closure.imitate_android

class FieldDefaultConfig {
    int minSdkVersion
    int targetSdkVersion
    int versionCode
    String versionName


    @Override
    public String toString() {
        return "FieldDefaultConfig{" +
                "minSdkVersion=" + minSdkVersion +
                ", targetSdkVersion=" + targetSdkVersion +
                ", versionCode=" + versionCode +
                ", versionName='" + versionName + '\'' +
                '}';
    }
}
package study.closure.imitate_android

class MainAndroid {
    public static void main(String[] args) {
        methodByField()
    }

    private static void methodByField() {
//闭包定义,属性要用"="
        def android = {
            compileSdkVersion = 25
            buildToolsVersion = "25.0.2"
            defaultConfig {
                minSdkVersion = 15
                targetSdkVersion = 25
                versionCode = 1
                versionName = "1.0"
            }
        }

//调用
        FieldAndroid bean = new FieldAndroid()
        android.delegate = bean
        //去对应对象中找到相应属性进行赋值
        android.call()
        //FieldAndroid{compileSdkVersion=25, buildToolsVersion='25.0.2', defaultConfig=FieldDefaultConfig{minSdkVersion=15, targetSdkVersion=25, versionCode=1, versionName='1.0'}}
        println bean.toString()
    }

    private static void methodByMethod() {
//闭包定义,方法可以直接调用。方法直接调用时,如果只有一个参数,那么则可以省略括号
        def android = {
            setCompileSdkVersion 25
            setBuildToolsVersion "25.0.2"
            setDefaultConfig {
                setMinSdkVersion 15
                setTargetSdkVersion 25
                setVersionCode 1
                setVersionName "1.0"
            }
        }

//调用
        Android bean = new Android()
        android.delegate = bean
        //去对应对象中找到相应方法进行赋值
        android.call()
        //Android{mCompileSdkVersion=25, mBuildToolsVersion='25.0.2', defaultConfig=DefaultConfig{mMinSdkVersion=15, mTargetSdkVersion=25, mVersionCode=1, mVersionName='1.0'}}
        println bean.toString()
    }
}

参考文章

[Android Gradle] 搞定Groovy闭包这一篇就够了

Groovy学习记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值