Share
Embed
Dependency Injection
Talk about dependency injection, not frameworks, just things around dependency injection.
Examples -> https://github.com/la-haus/la-haus-android
Daniel
April 02, 2020
Transcript
33 slides
-
8 class CallLogger def initialize(logger:) @logger = logger end def
create(call) @logger.info("Creating call...") end end -
9 class CallLogger def initialize(logger:) @logger = logger end def
create(call) @logger.info("Creating call...") end end class CallLogger def create(call) Rails.logger.info("Creating call...") end end -
10 class CallLogger attr_reader :logger def initialize(logger:) @logger = logger
end def create(call) @logger.info("Creating call...") end end class CallLogger def create(call) Rails.logger.info("Creating call...") end def update(call) Rails.logger.info("Update call...") end def delete(call) Rails.logger.info("Delete call...") end def get(call) Rails.logger.info("Get call...") end end -
11 class CallLogger attr_reader :logger def initialize(logger:) @logger = logger
end def create(call) @logger.info("Creating call...") end end -
15 interface Cache { fun put(key: String, value: String) fun
get(key: String): String fun clear() } class AppCache: Cache { override fun put(key: String, value: String) { // usamos SharedPreference } override fun get(key: String): String { return // usamos SharedPreference } override fun clear() { // usamos SharedPreference } } class MemoryCache: Cache { val hashMap: HashMap<String, String> = hashMapOf() override fun put(key: String, value: String) { hashMap[key] = value } override fun get(key: String): String { return hashMap[key]!! } override fun clear() { hashMap.clear() } } -
16 interface Cache { fun put(key: String, value: String) fun
get(key: String): String fun clear() } class LoginView(val context: Context) { val cache: Cache = AndroidCache(context) fun login(username: String) { cache.put("username", username) } } -
17 interface Cache { fun put(key: String, value: String) fun
get(key: String): String fun clear() } class LoginView(val cache: Cache) { fun login(username: String) { cache.put("username", username) } } -
18 class LoginView(val cache: Cache) { fun login(username: String) {
cache.put("username", username) } } class LoginView(val context: Context) { val cache: Cache = AndroidCache(context) fun login(username: String) { cache.put("username", username) } } -
19 class LoginView(val cache: Cache) { fun login(username: String) {
cache.put("username", username) } } val view = LoginView(AppCache()) view.login("[email protected]") -
20 class LoginView(val cache: Cache) { fun login(username: String) {
cache.put("username", username) } } val cache = MemoryCache() val view = LoginView(cache) view.login("[email protected]") assert(cache.get("username"), "[email protected]") -
23 class LoginView(val cache: Cache) { fun login(username: String) {
cache.put("username", username) ... } } class SignUpView(val cache: Cache) { fun signup(username: String) { cache.put("username", username) ... } } class HomeView(val cache: Cache) { fun start(username: String) { cache.put("username", username) ... } -
24 class SqlCache: Cache { override fun put(key: String, value:
String) { // usamos Sql } override fun get(key: String): String { return // usamos Sql } override fun clear() { // usamos Sql } } -
25 class LoginView(val cache: Cache) { fun login(username: String) {
cache.put("username", username) ... } } class SignUpView(val cache: Cache) { fun signup(username: String) { cache.put("username", username) ... } } class HomeView(val cache: Cache) { fun start(username: String) { cache.put("username", username) ... } -
28 class UploadCallService(val callRepository: CallRepository) { fun handleRingingEvent(adviserId: String, event:
CallEvent) { callRepository.create(CallUpdate(adviserId, event)) } fun handleHangUpEvent(adviserId: String, event: CallEvent) { callRepository.update(CallUpdate(adviserId, event)) } } class CallRepository { fun create(call: CallUpdate): ApiResponse { // call web service } fun update(call: CallUpdate): ApiResponse { // call web service } } -
29 interface CallRepository { fun create(call: CallUpdate): ApiResponse fun update(call:
CallUpdate): ApiResponse } class QueueCallRepository() : CallRepository { override fun create(call: CallUpdate) { return sendMessage(call, CallQueueElement.Status.NEW) } override fun update(call: CallUpdate): Flowable<ApiResponse<Any>> { return sendMessage(call, CallQueueElement.Status.UPDATE) } private fun sendMessage(call: CallUpdate, status: CallQueueElement.Status) return sendCallLogMessage(CallQueueElement(call, status)) } catch (exception: Exception) { return getThrowableFailure(exception) } } private fun getThrowableFailure(exception: Exception): ApiResponse { return when (exception) { is AmazonClientException -> BadRequestFailure(exception) is AmazonServiceException -> InternalServerFailure(exception) else -> ThrowableFailure(exception) } } // fun sendCallLogMessage()... -
30 class UploadCallService(val callRepository: CallRepository) { fun handleRingingEvent(adviserId: String, event:
CallEvent) { callRepository.create(CallUpdate(adviserId, event)) } fun handleHangUpEvent(adviserId: String, event: CallEvent) { callRepository.update(CallUpdate(adviserId, event)) } } -
www.lahaus.com Mi Haus es Tu Haus Daniel Gomez Rico
Senior Android Developer - La Haus [email protected]