https://github.com/iluwatar/java-design-patterns/issues/1021 - decrease number of checkstyle errors in callback pattern (#1053)
This commit is contained in:
		
				
					committed by
					
						
						Ilkka Seppälä
					
				
			
			
				
	
			
			
			
						parent
						
							91a085d3d1
						
					
				
				
					commit
					fca7e9c8c7
				
			@@ -24,23 +24,27 @@
 | 
			
		||||
package com.iluwatar.callback;
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 * first-class citizens. Prior to Java 8 callbacks can be simulated using simple (alike command)
 | 
			
		||||
 * interfaces.
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 * Callback pattern is more native for functional languages where functions are
 | 
			
		||||
 * treated as first-class citizens. Prior to Java 8 callbacks can be simulated
 | 
			
		||||
 * 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
 | 
			
		||||
   */
 | 
			
		||||
  public static void main(String[] args) {
 | 
			
		||||
  public static void main(final String[] args) {
 | 
			
		||||
    Task task = new SimpleTask();
 | 
			
		||||
    Callback callback = () -> LOGGER.info("I'm done now.");
 | 
			
		||||
    task.executeWith(callback);
 | 
			
		||||
 
 | 
			
		||||
@@ -24,9 +24,9 @@
 | 
			
		||||
package com.iluwatar.callback;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 * Callback interface
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public interface Callback {
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,22 +24,25 @@
 | 
			
		||||
package com.iluwatar.callback;
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 * defined as a Lambdas expression.
 | 
			
		||||
 * This example generates the exact same output as {@link App} however the
 | 
			
		||||
 * 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
 | 
			
		||||
   */
 | 
			
		||||
  public static void main(String[] args) {
 | 
			
		||||
  public static void main(final String[] args) {
 | 
			
		||||
    Task task = new SimpleTask();
 | 
			
		||||
    Callback c = () -> LOGGER.info("I'm done now.");
 | 
			
		||||
    task.executeWith(c);
 | 
			
		||||
 
 | 
			
		||||
@@ -24,19 +24,21 @@
 | 
			
		||||
package com.iluwatar.callback;
 | 
			
		||||
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import static org.slf4j.LoggerFactory.getLogger;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 * 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
 | 
			
		||||
  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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 * Template-method class for callback hook execution
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public abstract class Task {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Execute with callback
 | 
			
		||||
   */
 | 
			
		||||
  public final void executeWith(Callback callback) {
 | 
			
		||||
  final void executeWith(final Callback callback) {
 | 
			
		||||
    execute();
 | 
			
		||||
    if (callback != null) {
 | 
			
		||||
      callback.call();
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1 @@
 | 
			
		||||
package com.iluwatar.callback;
 | 
			
		||||
		Reference in New Issue
	
	Block a user