MethodScope

MethodScope

项目地址:skydoves/MethodScope 

简介::dango: Reduce repetitive inheritance works in OOP world using @MethodScope.

更多:作者   提 Bug   

标签:

 

License Build Status

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.

methodscope

Including in your project

Download

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_MyScopeMyClass_HisScopeMyClass_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.

methodscope_android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值