Forked from jiffle/Java 8 Patterns Cheatsheet.md
Created
May 3, 2022 15:48
Revisions
-
jiffle revised this gist
Mar 27, 2017 . 1 changed file with 78 additions and 74 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); 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 { CIRCLE( s, t -> new Circle( s, t)), RECTANGLE( s, t -> new Rectangle( s, t)); 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 : Factory Function Enum class: ``` java @RequiredArgsConstructor @Getter public enum ShapeType { CIRCLE( s, t -> new Circle( s, t)), RECTANGLE( s, t -> new Rectangle( s, t)); private final ShapeConstructor constructor; } ``` Functional Interface: ``` java @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 ShapeFactory.createFactory( CIRCLE).create( Color.RED, 2) ``` Factory use (version 2 - factory instance replacement): ``` java 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(); } ``` 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); ``` -
jiffle revised this gist
Mar 27, 2017 . 1 changed file with 49 additions and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); ``` -
jiffle revised this gist
Mar 27, 2017 . 1 changed file with 18 additions and 18 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); ``` -
jiffle revised this gist
Mar 14, 2017 . 1 changed file with 64 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,10 @@ # Design Patterns implemented in Java 8 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); -
jiffle revised this gist
Mar 10, 2017 . 1 changed file with 15 additions and 12 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,23 @@ # Design Patterns implemented in Java 8 ## Factory Enum class: @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(); } -
jiffle renamed this gist
Mar 10, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jiffle revised this gist
Mar 10, 2017 . No changes.There are no files selected for viewing
-
jiffle revised this gist
Mar 10, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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<Shape> constructor; }``` Factory method: -
jiffle created this gist
Mar 10, 2017 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); } ```