Giter VIP home page Giter VIP logo

servicelocator's Introduction

ServiceLocator

一个简单的服务定位器模式实现。

依赖注入(Dependency Injection)和服务定位器(Service Locator)是实现控制反转(Inversion of Control)的两种主要手段。

Android的主流依赖注入框架有:Dagger 和 Kion

如果觉得这些依赖注入框架太重,可以试试这个 ServiceLocator

Gradle

repositories {
    maven { url "https://gitee.com/ezy/repo/raw/cosmo/"}
}
dependencies {
    implementation "me.reezy.cosmo:servicelocator:0.7.0"
}

API

// 获取实例
inline fun <reified T> resolve(name: String = T::class.java.name): T?
// 注入实例
inline fun <reified T> inject(name: String = T::class.java.name): Lazy<T>
// 注册为单例
inline fun <reified T> singleton(name: String = T::class.java.name, crossinline block: () -> T)
// 注册为工厂
inline fun <reified T> factory(name: String = T::class.java.name, crossinline block: () -> T)

使用

单例,每次resolve获得的都是同一实例

class SomeService {
    fun doSomething() {
    }
}

// 注册 
singleton {
    SomeService()
}

// 获取
val service = resolve<SomeService>()

// 注入
class TheContext {
    val service: SomeService by inject()
}

具名单例

class NamedService(val name: String) {
    fun doSomething() {
    }
}

// 注册 
singleton("a") {
    NamedService("aaa")
}
singleton("b") {
    NamedService("bbb")
}

// 获取 
val serviceA = resolve<NamedService>("a")
val serviceB = resolve<NamedService>("b")

工厂,每次resolve都会产生新实例

class SomeService {
    fun doSomething() {
    }
}

// 注册 
factory {
    SomeService()
}

// 获取,每次resolve都会产生新实例
val service1 = resolve<SomeService>() 
val service2 = resolve<SomeService>() 

具名工厂

class NamedService(val name: String) {
    fun doSomething() {
    }
}

// 注册 
factory("a") {
    NamedService("aaa")
}
factory("b") {
    NamedService("bbb")
}

// 获取
// A1 与 A2 是使用同一工厂产生的不同实例
// A1 与 B1 是使用不同工厂产生的不同实例
val serviceA1 = resolve<NamedService>("a")
val serviceA2 = resolve<NamedService>("a")
val serviceB1 = resolve<NamedService>("b")
val serviceB2 = resolve<NamedService>("b")

LICENSE

The Component is open-sourced software licensed under the Apache license.

servicelocator's People

Contributors

czy1121 avatar

Stargazers

李冬冬 avatar Night avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

balao7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.