java callback pattern

A simple sample callback pattern in java

String ・ CamelCase → snake_case ・ Lowercase → Uppercase Let's take the process of displaying after

When not using a callback pattern

NoCallbackPattern.java


public class NoCallbackPattern {
    /**
     *Snake from CamelCase_Convert to case
     */
    private String camelToSnake(String input) {
        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, input);
    }

    /**
     *Convert from lowercase to uppercase
     */
    private String capitalize(String input) {
        return input.toUpperCase();
    }

    /**
     *Convert and display
     */
    public void print(String input) {
        System.out.println(capitalize(camelToSnake(input)));
    }
}

Main.java


public class Main {
    public static void main(String[] args) {
        NoCallbackPattern noCallbackPattern = new NoCallbackPattern();
        noCallbackPattern.print("replaceString");
    }
}

When written with a callback pattern

CallbackPattern.java


public class CallbackPattern {
    private List<MyFilter> myFilters;

    public CallbackPattern(List<MyFilter> myFilters) {
        this.myFilters = myFilters;
    }

    public void print(String input) {
        String output = input;
        for (MyFilter myFilter : myFilters) {
            output = myFilter.replace(output);
        }
        System.out.println(output);
    }
}

MyFilter.java


public interface MyFilter {
    String replace(String input);
}

CamelToSnake.java


public class CamelToSnake implements MyFilter {
    @Override
    public String replace(String input) {
        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, input);
    }
}

Capitalize.java


public class Capitalize implements MyFilter {
    @Override
    public String replace(String input) {
        return input.toUpperCase();
    }
}

Main.java


public class Main {
    public static void main(String[] args) {
        List<MyFilter> filters = Arrays.asList(new CamelToSnake(), new Capitalize());
        CallbackPattern callbackPattern = new CallbackPattern(filters);
        callbackPattern.print("replaceString");
    }
}

** If you write using the callback pattern, you can see that the CallbackPattern class does not need to be modified when adding conversion processing because the print method depends only on the MyFilter interface **

Recommended Posts

java callback pattern
[Java] Strategy pattern
Java design pattern
[Java] Singleton pattern
[Java] Adapter pattern
Java pattern memo
My DAO pattern (Java)
Builder pattern (Effective Java)
Java Lambda Command Pattern
Java design pattern summary
[Design pattern] Java core library
Java
Enum Strategy pattern in Java
[Java] Draw a simple pattern
Java
A simple sample callback in Java
JNA (Java Native Access) pattern collection
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Java learning (0)
Studying Java ―― 3
Java protected
Memento Pattern
[Java] Annotation
[Java] Module
Java array
Mediator pattern
Iterator pattern
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Composite pattern
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
Observer Pattern
java (override)
Builder pattern
Java Day 2018
Java string
Bridge Pattern
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Command Pattern
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
Builder Pattern
java reflexes
java (interface)
Java memorandum
Strategy pattern
☾ Java / Collection
Java array