https://github.com/iluwatar/java-design-patterns/issues/1021 - decrease number of checkstyle errors in callback pattern (#1053)
This commit is contained in:
parent
91a085d3d1
commit
fca7e9c8c7
@ -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);
|
||||||
|
@ -24,9 +24,9 @@
|
|||||||
package com.iluwatar.callback;
|
package com.iluwatar.callback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Callback interface
|
* Callback interface
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface Callback {
|
public interface Callback {
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
@ -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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
package com.iluwatar.callback;
|
Loading…
x
Reference in New Issue
Block a user