MethodScope
项目地址:skydoves/MethodScope
简介::dango: Reduce repetitive inheritance works in OOP world using @MethodScope.
标签:
Methodscope automatically generates classes that perform similar function tasks on a per-scope basis for a class.
When similar and repetitive classes need to be created, the work what repetitive inheritance can be decreased.
Including in your project
Gradle
And add below two dependencies code on your module's build.gradle
file.
dependencies {
implementation "com.github.skydoves:methodscope:1.0.1"
annotationProcessor "com.github.skydoves:methodscope-processor:1.0.1"
}
Usage
@MethodScope
Create a custom scope annotation using @MethodScope
annotation.
@MethodScope
public @interface MyScope {
}
Here is the kotlin code example.
@MethodScope
annotation class MyScope
Attach the custom scope annotation on top of a class.
All of the public methods are the base method for the scoped like the init()
method.
And designate scoping method using @Scoped
annotation with the naming rule what startWith the base method's name.
@MyScope
public class MyClass {
private String scope;
public void init() { // this is the base method of the scoped methods.
this.scope = "initialized";
}
@Scoped(MyScope.class)
public void initMyScope() { // this is a scoped method for @MyScope.
this.scope = "initialized by @MyScope";
}
}
After build the project, MyClass_MyScope
class will be auto-generated.
MyClass_MyScope myClass_myScope = new MyClass_MyScope();
myClass_myScope.init(); // calls init() and initMyScope() methods.
myClass_myScope.initMyScope(); // calls only initMyScope() method.
Multiple Scoping
Here is how to create multiple scoped class.
@MyScope
@HisScope
@YourScope
public class MyClass {
private String scope;
public void init() { // this is the base method of the scoped methods.
this.scope = "initialized";
}
@Scoped(MyScope.class)
public void initMyScope() { // this is a scoped method for @MyScope.
this.scope = "initialized by @MyScope";
}
@Scoped(YourScope.class)
public void initMineScopeNotYours() { // this is a scoped method for @YourScope.
this.scope = "initialized by @YourScope";
}
}
After build the project, MyClass_MyScope
, MyClass_HisScope
, MyClass_YourScope
classes will be auto-generated.
Scoping with a return type and parameters
Methods that have a return type and parameters can be scoped method.
The base method's return type and parameters are must be the same as the scoped method's one.
@MyScope
abstract public class MyClass {
private String scope;
public String init(String text) { // this is the base method of the scoped methods.
this.scope += text;
return this.scope;
}
@Scoped(MyScope.class)
public String initMyScope(String text) { // this is a scoped method for @MyScope.
this.scope += text;
return this.scope;
}
Abstract class Scoping
MethodScope supports scopping for the abstract class.
This is more clear because the base method of the scoped methods is being explicitly abstract.
@MyScope
@YourScope
abstract public class MyClass {
private String scope;
abstract void init();
@Scoped(MyScope.class)
public void initMyScope() { // this is a scoped method for @MyScope.
this.scope = "initialized by @MyScope";
}
@Scoped(YourScope.class)
public void initMineScopeNotYours() { // this is a scoped method for @YourScope.
this.scope = "initialized by @YourScope";
}
Android Project Usage
MethodScope is useful to the Android project for creating similar screens.