Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Guilherme Henrique Guilherme-HRamos

🏠
Working from home
View GitHub Profile
@Guilherme-HRamos
Guilherme-HRamos / OpenFileHelper.java
Last active Sep 16, 2020
Medium - Android/Java Interfaces like a Boss! - Default Functionalities lll
View OpenFileHelper.java
public class OpenFileHelper implements LoggerComponent {
public OpenFileHelper() {
printMethodCall()
init();
}
private void init() {
printMethodCall()
@Guilherme-HRamos
Guilherme-HRamos / LoggerComponent.java
Last active Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Default Functionalities ll
View LoggerComponent.java
package com.myapp.components.logger;
public interface LoggerComponent {
default void printMessage(final String message) {
LoggerComponentPrivate.printJustMessage(message);
}
default void printMethodCall() {
final StackTraceElement element = Thread.currentThread().getStackTrace()[4];
@Guilherme-HRamos
Guilherme-HRamos / LoggerComponent.java
Last active Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Default Functionalities l
View LoggerComponent.java
public interface LoggerComponent {
String TAG = "LoggerComponent";
default void printJustMessage(final String inf) {
if (BuildConfig.DEBUG)
Log.d(TAG, inf);
}
default void printMethodCall(final StackTraceElement thread) {
@Guilherme-HRamos
Guilherme-HRamos / BaseViewContract.java
Created Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Backwards Compatibility New Method ll
View BaseViewContract.java
public interface BaseViewContract {
void onShowLoading();
void onHideLoading();
void onShowError(String error);
default void onShowToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
@Guilherme-HRamos
Guilherme-HRamos / BaseViewContract.java
Last active Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Backwards Compatibility New Method
View BaseViewContract.java
public interface BaseViewContract {
void onShowLoading();
void onHideLoading();
void onShowError(String error);
default void onShowToast(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
@Guilherme-HRamos
Guilherme-HRamos / BaseViewContract.java
Created Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Backwards Compatibility
View BaseViewContract.java
public interface BaseViewContract {
void onShowLoading();
void onHideLoading();
void onShowError(String error);
}
@Guilherme-HRamos
Guilherme-HRamos / OpenFileHelper.java
Created Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Lambda x Anonymous class
View OpenFileHelper.java
public class OpenFileHelper {
public void openFile() {
// using anonymous class
sendLog(new Logger() {
@Override
public void logMessage(String message) {
Log.i("OpenFileHelper", message);
}
@Guilherme-HRamos
Guilherme-HRamos / Logger.java
Created Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Functional Interface
View Logger.java
@FunctionalInterface
public interface Logger {
void logMessage(String message);
}
@Guilherme-HRamos
Guilherme-HRamos / OpenFileHelper.java
Last active Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Markers (Usage)
View OpenFileHelper.java
public class OpenFileHelper {
private final Logger myLogger;
public OpenFileHelper() {
// logs strategies
SystemsLogger systemsLogger = new SystemsLoggerImpl();
LogCatLogger logCatLogger = new LogCatLoggerImpl("OpenFileHelper");
// log manager
@Guilherme-HRamos
Guilherme-HRamos / LoggerDispatcher.java
Created Jul 28, 2019
Medium - Android/Java Interfaces like a Boss! - Markers (Facade)
View LoggerDispatcher.java
public class LoggerDispatcher implements Logger {
private final List<Logger> loggerList = new ArrayList<>();
public LoggerDispatcher(Logger... loggers) {
Collections.addAll(loggerList, loggers);
}
@Override
public void logMessage(String message) {