https://github.com/iluwatar/java-design-patterns/issues/1021 - decrease number of checkstyle errors in callback pattern (#1053)

This commit is contained in:
adamski.pro 2019-10-30 07:19:33 +01:00 committed by Ilkka Seppälä
parent 91a085d3d1
commit fca7e9c8c7
6 changed files with 36 additions and 26 deletions

View File

@ -24,23 +24,27 @@
package com.iluwatar.callback; package com.iluwatar.callback;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.slf4j.LoggerFactory.getLogger;
/** /**
* *
* Callback pattern is more native for functional languages where functions are treated as * Callback pattern is more native for functional languages where functions are
* first-class citizens. Prior to Java 8 callbacks can be simulated using simple (alike command) * treated as first-class citizens. Prior to Java 8 callbacks can be simulated
* interfaces. * using simple (alike command) interfaces.
* *
*/ */
public class App { public final class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final Logger LOGGER = getLogger(App.class);
private App() {
}
/** /**
* Program entry point * Program entry point
*/ */
public static void main(String[] args) { public static void main(final String[] args) {
Task task = new SimpleTask(); Task task = new SimpleTask();
Callback callback = () -> LOGGER.info("I'm done now."); Callback callback = () -> LOGGER.info("I'm done now.");
task.executeWith(callback); task.executeWith(callback);

View File

@ -24,9 +24,9 @@
package com.iluwatar.callback; package com.iluwatar.callback;
/** /**
* *
* Callback interface * Callback interface
* *
*/ */
public interface Callback { public interface Callback {

View File

@ -24,22 +24,25 @@
package com.iluwatar.callback; package com.iluwatar.callback;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.slf4j.LoggerFactory.getLogger;
/** /**
* *
* This example generates the exact same output as {@link App} however the callback has been * This example generates the exact same output as {@link App} however the
* defined as a Lambdas expression. * callback has been defined as a Lambdas expression.
* *
*/ */
public class LambdasApp { public final class LambdasApp {
private static final Logger LOGGER = LoggerFactory.getLogger(LambdasApp.class); private static final Logger LOGGER = getLogger(LambdasApp.class);
private LambdasApp() { }
/** /**
* Program entry point * Program entry point
*/ */
public static void main(String[] args) { public static void main(final String[] args) {
Task task = new SimpleTask(); Task task = new SimpleTask();
Callback c = () -> LOGGER.info("I'm done now."); Callback c = () -> LOGGER.info("I'm done now.");
task.executeWith(c); task.executeWith(c);

View File

@ -24,19 +24,21 @@
package com.iluwatar.callback; package com.iluwatar.callback;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.slf4j.LoggerFactory.getLogger;
/** /**
* *
* Implementation of task that need to be executed * Implementation of task that need to be executed
* *
*/ */
public class SimpleTask extends Task { public final class SimpleTask extends Task {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTask.class); private static final Logger LOGGER = getLogger(SimpleTask.class);
@Override @Override
public void execute() { public void execute() {
LOGGER.info("Perform some important activity and after call the callback method."); LOGGER.info("Perform some important activity and after call the"
+ " callback method.");
} }
} }

View File

@ -24,16 +24,16 @@
package com.iluwatar.callback; package com.iluwatar.callback;
/** /**
* *
* Template-method class for callback hook execution * Template-method class for callback hook execution
* *
*/ */
public abstract class Task { public abstract class Task {
/** /**
* Execute with callback * Execute with callback
*/ */
public final void executeWith(Callback callback) { final void executeWith(final Callback callback) {
execute(); execute();
if (callback != null) { if (callback != null) {
callback.call(); callback.call();

View File

@ -0,0 +1 @@
package com.iluwatar.callback;