一、Closure关于DSL练习——主要是针对Closure的delegate运用
1.1 示例一
package study. closure. demo1
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
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 ( )
main. closure. delegate = animal
main. closure. resolveStrategy= Closure. DELEGATE_FIRST
main. closure ( )
println animal. toString ( )
}
}
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 ( )
}
} , 1000 , 3000 )
}
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
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
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 ( )
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 ( )
println bean. toString ( )
}
}
参考文章