Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @jiffle jiffle revised this gist Mar 27, 2017. 1 changed file with 78 additions and 74 deletions.
    152 changes: 78 additions & 74 deletions Java 8 Patterns Cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -8,128 +8,132 @@ These patterns are implemented using Java 8 Lambdas (together with Project Lombo

    Enum class:
    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( Circle::new),
    RECTANGLE( Rectangle::new);
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( Circle::new),
    RECTANGLE( Rectangle::new);

    private final Supplier<Shape> constructor;
    }
    private final Supplier<Shape> constructor;
    }
    ```

    Factory method:
    ``` java
    public static Shape createShape( ShapeType type) {
    return type.getConstructor().get();
    }
    public static Shape createShape( ShapeType type) {
    return type.getConstructor().get();
    }
    ```
    ### Factory II : Parameterised Constructor

    Enum class:
    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( s, t -> new Circle( s, t)),
    RECTANGLE( s, t -> new Rectangle( s, t));
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( s, t -> new Circle( s, t)),
    RECTANGLE( s, t -> new Rectangle( s, t));

    private final ShapeConstructor constructor;
    }
    private final ShapeConstructor constructor;
    }
    ```
    Functional Interface:
    ``` java
    @FunctionalInterface
    public interface ShapeConstructor {
    Shape create( String s, int t);
    }
    @FunctionalInterface
    public interface ShapeConstructor {
    Shape create( String s, int t);
    }
    ```
    Factory method:
    ``` java
    public static Shape createShape( ShapeType type, String name, int line) {
    return type.getConstructor().create( name, line);
    }
    public static Shape createShape( ShapeType type, String name, int line) {
    return type.getConstructor().create( name, line);
    }
    ```
    ### Factory III : Constructor Function
    ### Factory III : Factory Function

    Enum class:
    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( s, t -> new Circle( s, t)),
    RECTANGLE( s, t -> new Rectangle( s, t));
    @RequiredArgsConstructor
    @Getter
    public enum ShapeType {
    CIRCLE( s, t -> new Circle( s, t)),
    RECTANGLE( s, t -> new Rectangle( s, t));

    private final ShapeConstructor constructor;
    }
    private final ShapeConstructor constructor;
    }
    ```
    Functional Interface:
    ``` java
    @FunctionalInterface
    public interface ShapeConstructor {
    Shape create( String s, int t);
    }
    @FunctionalInterface
    public interface ShapeFactory {
    Shape create( String s, int t);
    }
    ```
    Factory method:
    ``` java
    public static ShapeFactory createFactory( ShapeType type) {
    return type.getConstructor();
    }
    ```
    Factory use (version 1 - static factory replacement):
    ``` java
    public static ShapeConstructor createConstructor( ShapeType type) {
    return type.getConstructor();
    }
    ShapeFactory.createFactory( CIRCLE).create( Color.RED, 2)
    ```
    Constructor use:
    Factory use (version 2 - factory instance replacement):
    ``` java
    ShapeConstructor ctor = ShapeFactory.createConstructor( CIRCLE);
    ...
    Shape newShape = ctor.create( String name, int line);
    ShapeFactory factory = ShapeFactory.createFactory( RECTANGLE);
    ...
    Shape newShape = shapeFactory.create( Color.YELLOW, 1);
    ```
    ## Strategy Pattern
    Strategy Interface:
    ``` java
    public interface TextFormatStrategy {
    Predicate<String> getFilter();
    UnaryOperator<String> getFormatter();
    Predicate<String> getFilter();
    UnaryOperator<String> getFormatter();
    }
    ```
    Enum class:
    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum StrategyType implements TextFormatStrategy {
    PLAIN_TEXT( t -> true,
    t -> t),
    ERROR_TEXT( t -> t.startsWith( "ERROR" ),
    t -> t.toUpperCase()),
    SHORT_TEXT( t -> t.length() < 20 ,
    t -> t.toLowerCase());
    @RequiredArgsConstructor
    @Getter
    public static enum StrategyType implements TextFormatStrategy {
    PLAIN_TEXT( t -> true,
    t -> t),
    ERROR_TEXT( t -> t.startsWith( "ERROR" ),
    t -> t.toUpperCase()),
    SHORT_TEXT( t -> t.length() < 20 ,
    t -> t.toLowerCase());

    private final Predicate<String> filter;
    private final UnaryOperator<String> formatter;
    }
    private final Predicate<String> filter;
    private final UnaryOperator<String> formatter;
    }
    ```

    ### Usage Pattern Version I
    Usage - passing all the data into the recipient method:
    ```java
    public static void publishText(String text, TextFormatStrategy formatStrategy) {
    if ( formatStrategy.getFilter().test( text )) {
    System.out.println( formatStrategy.getFormatter().apply( text ) );
    }
    }
    ...
    List<String> messages = Arrays.asList( "DEBUG - I'm here", "ERROR - something bad happened");
    messages.forEach( t -> publishText( t, TextFormatStrategy.StrategyType.PLAIN_TEXT));
    public static void publishText(String text, TextFormatStrategy formatStrategy) {
    if ( formatStrategy.getFilter().test( text )) {
    System.out.println( formatStrategy.getFormatter().apply( text ) );
    }
    }
    ...
    List<String> messages = Arrays.asList( "DEBUG - I'm here", "ERROR - something bad happened");
    messages.forEach( t -> publishText( t, TextFormatStrategy.StrategyType.PLAIN_TEXT));
    ```

    ### Usage Pattern Version II
    Usage - passing just the strategy and returning a functional method to receive the parameters:
    ```java
    public static Consumer<String> publishText( TextFormatStrategy formatStrategy) {
    return t -> {
    if ( formatStrategy.getFilter().test( t )) {
    System.out.println( formatStrategy.getFormatter().apply( t) );
    }};
    }
    ...
    List<String> messages = Arrays.asList( "DEBUG - I'm here", "ERROR - something bad happened");
    messages.forEach( publishText( TextFormatStrategy.StrategyType.PLAIN_TEXT)::accept);
    public static Consumer<String> publishText( TextFormatStrategy formatStrategy) {
    return t -> {
    if ( formatStrategy.getFilter().test( t )) {
    System.out.println( formatStrategy.getFormatter().apply( t) );
    }};
    }
    ...
    List<String> messages = Arrays.asList( "DEBUG - I'm here", "ERROR - something bad happened");
    messages.forEach( publishText( TextFormatStrategy.StrategyType.PLAIN_TEXT)::accept);
    ```
  2. @jiffle jiffle revised this gist Mar 27, 2017. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions Java 8 Patterns Cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -82,5 +82,54 @@ Constructor use:
    ...
    Shape newShape = ctor.create( String name, int line);
    ```
    ## Strategy Pattern
    Strategy Interface:
    ``` java
    public interface TextFormatStrategy {
    Predicate<String> getFilter();
    UnaryOperator<String> getFormatter();
    }
    ```
    Enum class:
    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum StrategyType implements TextFormatStrategy {
    PLAIN_TEXT( t -> true,
    t -> t),
    ERROR_TEXT( t -> t.startsWith( "ERROR" ),
    t -> t.toUpperCase()),
    SHORT_TEXT( t -> t.length() < 20 ,
    t -> t.toLowerCase());

    private final Predicate<String> filter;
    private final UnaryOperator<String> formatter;
    }
    ```

    ### Usage Pattern Version I
    Usage - passing all the data into the recipient method:
    ```java
    public static void publishText(String text, TextFormatStrategy formatStrategy) {
    if ( formatStrategy.getFilter().test( text )) {
    System.out.println( formatStrategy.getFormatter().apply( text ) );
    }
    }
    ...
    List<String> messages = Arrays.asList( "DEBUG - I'm here", "ERROR - something bad happened");
    messages.forEach( t -> publishText( t, TextFormatStrategy.StrategyType.PLAIN_TEXT));
    ```

    ### Usage Pattern Version II
    Usage - passing just the strategy and returning a functional method to receive the parameters:
    ```java
    public static Consumer<String> publishText( TextFormatStrategy formatStrategy) {
    return t -> {
    if ( formatStrategy.getFilter().test( t )) {
    System.out.println( formatStrategy.getFormatter().apply( t) );
    }};
    }
    ...
    List<String> messages = Arrays.asList( "DEBUG - I'm here", "ERROR - something bad happened");
    messages.forEach( publishText( TextFormatStrategy.StrategyType.PLAIN_TEXT)::accept);
    ```
  3. @jiffle jiffle revised this gist Mar 27, 2017. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions Java 8 Patterns Cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ These patterns are implemented using Java 8 Lambdas (together with Project Lombo
    ### Factory I : No Parameter Constructor

    Enum class:

    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    @@ -16,18 +16,18 @@ Enum class:

    private final Supplier<Shape> constructor;
    }

    ```

    Factory method:

    ``` java
    public static Shape createShape( ShapeType type) {
    return type.getConstructor().get();
    }

    ```
    ### Factory II : Parameterised Constructor

    Enum class:

    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    @@ -36,24 +36,24 @@ Enum class:

    private final ShapeConstructor constructor;
    }

    ```
    Functional Interface:

    ``` java
    @FunctionalInterface
    public interface ShapeConstructor {
    Shape create( String s, int t);
    }

    ```
    Factory method:

    ``` java
    public static Shape createShape( ShapeType type, String name, int line) {
    return type.getConstructor().create( name, line);
    }

    ```
    ### Factory III : Constructor Function

    Enum class:

    ``` java
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    @@ -62,25 +62,25 @@ Enum class:

    private final ShapeConstructor constructor;
    }

    ```
    Functional Interface:

    ``` java
    @FunctionalInterface
    public interface ShapeConstructor {
    Shape create( String s, int t);
    }

    ```
    Factory method:

    ``` java
    public static ShapeConstructor createConstructor( ShapeType type) {
    return type.getConstructor();
    }

    ```
    Constructor use:

    ``` java
    ShapeConstructor ctor = ShapeFactory.createConstructor( CIRCLE);
    ...
    Shape newShape = ctor.create( String name, int line);

    ```


  4. @jiffle jiffle revised this gist Mar 14, 2017. 1 changed file with 64 additions and 1 deletion.
    65 changes: 64 additions & 1 deletion Java 8 Patterns Cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    # Design Patterns implemented in Java 8

    ## Factory
    These patterns are implemented using Java 8 Lambdas (together with Project Lombok annotations).

    ## Factory Pattern

    ### Factory I : No Parameter Constructor

    Enum class:

    @@ -20,4 +24,63 @@ Factory method:
    return type.getConstructor().get();
    }

    ### Factory II : Parameterised Constructor

    Enum class:

    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( s, t -> new Circle( s, t)),
    RECTANGLE( s, t -> new Rectangle( s, t));

    private final ShapeConstructor constructor;
    }

    Functional Interface:

    @FunctionalInterface
    public interface ShapeConstructor {
    Shape create( String s, int t);
    }

    Factory method:

    public static Shape createShape( ShapeType type, String name, int line) {
    return type.getConstructor().create( name, line);
    }

    ### Factory III : Constructor Function

    Enum class:

    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( s, t -> new Circle( s, t)),
    RECTANGLE( s, t -> new Rectangle( s, t));

    private final ShapeConstructor constructor;
    }

    Functional Interface:

    @FunctionalInterface
    public interface ShapeConstructor {
    Shape create( String s, int t);
    }

    Factory method:

    public static ShapeConstructor createConstructor( ShapeType type) {
    return type.getConstructor();
    }

    Constructor use:

    ShapeConstructor ctor = ShapeFactory.createConstructor( CIRCLE);
    ...
    Shape newShape = ctor.create( String name, int line);



  5. @jiffle jiffle revised this gist Mar 10, 2017. 1 changed file with 15 additions and 12 deletions.
    27 changes: 15 additions & 12 deletions Java 8 Patterns Cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,23 @@
    # Design Patterns implemented in Java 8

    ## Factory
    Factory implementation outline:

    Enum class:
    ```@RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( Circle::new),
    RECTANGLE( Rectangle::new);

    private final Supplier<Shape> constructor;
    }```
    @RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( Circle::new),
    RECTANGLE( Rectangle::new);

    private final Supplier<Shape> constructor;
    }


    Factory method:
    ```public static Shape createShape( ShapeType type) {
    return type.getConstructor().get();
    }
    ```

    public static Shape createShape( ShapeType type) {
    return type.getConstructor().get();
    }


  6. @jiffle jiffle renamed this gist Mar 10, 2017. 1 changed file with 0 additions and 0 deletions.
  7. @jiffle jiffle revised this gist Mar 10, 2017. No changes.
  8. @jiffle jiffle revised this gist Mar 10, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Java 8 Patterns Cheatsheet
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ public static enum ShapeType {
    CIRCLE( Circle::new),
    RECTANGLE( Rectangle::new);

    private final Supplier<PageParser> constructor;
    private final Supplier<Shape> constructor;
    }```

    Factory method:
  9. @jiffle jiffle created this gist Mar 10, 2017.
    20 changes: 20 additions & 0 deletions Java 8 Patterns Cheatsheet
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # Design Patterns implemented in Java 8

    ## Factory
    Factory implementation outline:
    Enum class:
    ```@RequiredArgsConstructor
    @Getter
    public static enum ShapeType {
    CIRCLE( Circle::new),
    RECTANGLE( Rectangle::new);

    private final Supplier<PageParser> constructor;
    }```

    Factory method:
    ```public static Shape createShape( ShapeType type) {
    return type.getConstructor().get();
    }
    ```