All Projects → SilenceDut → Hub

SilenceDut / Hub

Android 开发中通过接口获取实现类,可用于module之间的通信,通过注解解决module的依赖初始化问题,良好的多线程环境下的线程安全以及性能。a concise di library which can get implementation class for a interface easily in multiple module app , also avoid check null when want to invoke a implementation by interface.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Hub

Dnnextensions
One solution. Multiple extensions. No pain. A collection of DNN modules and other extensions.
Stars: ✭ 37 (-53.16%)
Mutual labels:  modules
Samples Module Loading Comparison
Some tests for comparing performance between bundling and unbundling JS
Stars: ✭ 54 (-31.65%)
Mutual labels:  modules
Screenadaptationkit
🎨iOS rapidScreen Compatible AdapterKit(Deprecate)
Stars: ✭ 70 (-11.39%)
Mutual labels:  frame
Phaser Node Kit
Rapid Game Development with PhaserJS and Node for Modern Browsers
Stars: ✭ 39 (-50.63%)
Mutual labels:  modules
Postcss Import
PostCSS plugin to inline @import rules content
Stars: ✭ 1,048 (+1226.58%)
Mutual labels:  modules
Webpack Node Externals
Easily exclude node modules in Webpack
Stars: ✭ 1,109 (+1303.8%)
Mutual labels:  modules
Source two
Open Source FPV Racing Frame
Stars: ✭ 35 (-55.7%)
Mutual labels:  frame
Postcss Modules Example
How to use postcss-modules plugin
Stars: ✭ 78 (-1.27%)
Mutual labels:  modules
Supreme
Generate UML diagrams of Shiny modules
Stars: ✭ 52 (-34.18%)
Mutual labels:  modules
Hub
Black Duck Docker Orchestration Files/Documentation
Stars: ✭ 70 (-11.39%)
Mutual labels:  hub
Html Modules Toolkit
Transforming HTML standards of the future into JavaScript standards of the past
Stars: ✭ 45 (-43.04%)
Mutual labels:  modules
Uhubctl
uhubctl - USB hub per-port power control
Stars: ✭ 1,036 (+1211.39%)
Mutual labels:  hub
Docker Ubuntu1804 Ansible
Ubuntu 18.04 LTS (Bionic) Docker container for Ansible playbook and role testing.
Stars: ✭ 61 (-22.78%)
Mutual labels:  hub
Gifmagic
💈 Gif maker and extractor in Swift
Stars: ✭ 38 (-51.9%)
Mutual labels:  frame
Docker Centos7 Ansible
CentOS 7 Docker container for Ansible playbook and role testing.
Stars: ✭ 72 (-8.86%)
Mutual labels:  hub
Gomodctl
Search, Check, and Update Go modules.
Stars: ✭ 37 (-53.16%)
Mutual labels:  modules
Mrthumb
【拇指先生】 a simple easy video thumbnail provider,顺滑的获取视频缩略图,支持本地和网络视频,有问题大胆提Issues
Stars: ✭ 60 (-24.05%)
Mutual labels:  frame
Flow Ui
Flow-UI is a highly customizable UI framework based Seajs/jQuery。
Stars: ✭ 79 (+0%)
Mutual labels:  modules
Modules Papers
A collection of papers on modules.
Stars: ✭ 74 (-6.33%)
Mutual labels:  modules
Flexicms
Flexible site management system Flexi CMS
Stars: ✭ 61 (-22.78%)
Mutual labels:  modules

Hub

a concise di library which can avoid check null when want to invoke a implementation by interface

Project

It had been used in project KnowWeather and diffadapter demo ,you can learn more.

同时已用在日活百万级别的线上项目中

Introduce

框架思想

Feature

  1. 通过控制反转实现module间服务提供、Activity跳转,Activity支持参数自动处理传递的参数,不需要繁琐的注解标注
  2. 多接口支持。优点在于不必要暴露所有接口,只需将需要的接口暴露,比如一个服务可能支持多个功能,但是有些功能只需要再module内使用,有些需要提供给其他module,这样就可以抽离出多个接口,只需要将需要暴露的放到基础module里。
  3. 接口化的通信方式类似于“SDK”+数据结构,面向接口编程,更清晰直观, 对IDE更友好(可在IDE中直接跳转),协议变化直接反映在编译上,维护接口也简单3
  4. 多线程同时获取为初始话的服务的实现类时,不会因锁导致最后调用的初始化时间过长
  5. Less is more, simple is better!使用简单,功能强大

对比ARouter在多线程中的初始化耗时表现

ServerA ServerB ServerC
对应实现类单独初始化耗时 500ms 300ms 200ms

如果不同线程同时区获取ServerA,ServerB,ServerC,那么ARouter完全初始化三个服务的时间是1000ms,而Hub的完全初始化时间是500ms。这种情形在优化应用启动的时候,尤其一个应用有很多的服务实现类,极端情况下就可能退化到了单线程的执行。

Using

简单的使用方式,坚持 Less is more, simple is better! 的理念,接口定义、调用的方式都很是常规的方式,尽可能的减少注解来标注,使配置最小化。

服务提供

向其他module提供的接口(在common module里定义)

public interface IFunctionOuter extends IHub{
	void testOut();
}

不需要暴露的接口(A module里定义)

public interface IFunctionInner extends IHub{
	void testInner();
}

实现功能( A module里实现)

@HubInject(api = {IFunctionInner.class, IFunctionOuter})
class FunctionImpl implements IFunctionInner , IFunctionOuter{
    @Override
    public void testInner(){
    ...
    }
    
    @Override
    public void testOut(){
    ...
    }
}

不需要判空、强转,直接调用(任何module里都可以调用)

Hub.getImpl(IFunctionOuter.class). testOut();

Activity跳转

在Application里初始化,这里只是传入Application引用做缓存,主要是为了后面跳转Activity时减少传入Context的过程,只是一个赋值操作,没有做任何耗时的操作,如果不需要Activity跳转,只需要服务提供,这个初始化就不要添加

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Hub.init(this);
    }
}

如果需要判断是否存在相应的页面来做一些提示之类的,可以将函数返回值的类型设置为boolean

public interface IActivityTest extends IHubActivity{
    boolean anyMethodName(List<Map<String,Integer>> a, int b); 
    void activity2(); // no need use at present
}

在需要调转的Activity上加上HubActivity注解,通过methodName也指定当前Activity对应的接口的方法,如果需要传参数,参数名和类型和接口里的保持一致,不需要注解,跳转完成后,Activity的参数会自动被解析(支持跨进程的Activity)

@HubActivity(activityApi = IActivityTest.class,methodName = "anyMethodName")
public class SecondActivity extends AppCompatActivity {
    List<Map<String,Integer>> a; // 参数名和类型保持一致
    int b; // 参数名和类型保持一致
    ...
}

调用

boolean found = Hub.getActivity(IActivityTest.class).activitySecond(mapList,9);

如果需要activityResult 

Hub.getActivityWithExpand(IActivityTest.class).withResult(MainActivity.this,10).build().activitySecond(mapList,9);

Import

Step1.Add it in your root add abuild.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step2. Add the dependency:

dependencies {
    implementation 'com.github.silencedut.hub:hub:latestVersion'
    annotationProcessor 'com.github.silencedut.hub:hub-compiler:latestVersion'
}

ProGuard

-keep class * implements com.silencedut.hub_annotation.IFindImplClz {*;}
-keep class * implements com.silencedut.hub_annotation.IFindActivity {*;}


-keepnames interface * extends com.silencedut.hub.IHub
-keepnames interface * extends com.silencedut.hub.IHubActivity
-keep interface * extends com.silencedut.hub.IHubActivity {<methods>;}

-dontwarn com.alibaba.fastjson.**
-keepattributes Signature
-keepattributes *Annotation*

License

Copyright 2017-2018 SilenceDut

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].