Miroslav Wengner
New Java features
Simplified Design Patterns
Benedikt Neumayr
Safe Harbour Statement
All what you will hear can be di
ff
erent, this presentation is for
motivational purposes …
Miroslav Wengner
• Husband, Father, Software Engineer, Author, Blogger, Technology Enthusiast
• JCP: Executive Committee Board Member, OpenJDK Committer
• Java Mission Control Project, Open-Source projects
• Co-Author of Robo4J Project (Duke Award)
• Java Champion, JavaOne RockStar
• Principal Engineer at OpenValue
Benedikt Neumayr
• Human
• Managing Director at OpenValue
• Software Architect
• Expert for Project Consolidation
• Java Enthusiast
• Group Of Talented and Motivated Individuals
• Netherlands, Germany, Switzerland, Austria…
• Fun, Development, Trainings, Migration and
more…
Agenda
• Java Platform and OpenJDK
• SOLID thinking about design patterns
• Simplifying objects creation
• Thinking structural
• Enforcing behaviour at runtime
• Touching concurrency
• Conclusion
• Q/A
Java Platform and OpenJDK
• Compiler => byte-code
• JIT => instructions
• JVM Interpreter => EXPECTED RESULT
• Byte code optimisation: inlining, elimination, scalarization
static factory_method.Car produce(java.lang.String);
descriptor: (Ljava/lang/String;)Lfactory_method/Car;
fl
ags: (0x0008) ACC_STATIC
Code:
stack=7, locals=5, args_size=1
0: new #7 // class factory_method/FactoryMethodEvent
3: dup
…
19: aload_3
20: invokevirtual #17 // Method java/lang/String.hashCode:()I
23: lookupswitch { // 2
3135580: 48
3556498: 63
default: 75
}
48: aload_3
49: ldc #23 // String fast
51: invokevirtual #25 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
SOLID thinking about design patterns
• SOLID principles:
• Single responsibility, Open-Close, Liskov Substitution
• Interface Segregation, Dependency Inversion
• Do NOT Repeat Yourself, Separation of Concerns, CAP
• Programming languages and technology agnostic
• Micro-services Architecture, Distributed systems
• Domain Driven Design, Data streaming (LM, AI)
• Extendability, Maintainability or Security
split?
More pieces there…
?
?
SOLID thinking about design patterns
• Design Pattern Types: Creational, Structural, Behavioural => Concurrency (services, runtime)
• Important projects
• Project Valhalla: performance gains through generic API, primitives and value types, ML and BigData apps
• Hidden Classes(JEP-371), Warnings for Value-Based Classes (JEP-390)…
• Project Panama: interconnecting JVM and well de
fi
ned non-java APIs, easier access for I/O apps
• Foreign Function & Memory API (JEP-424), Vector API (JEP-426)…
• Project Amber: improving productivity through Java language evolution
• Local-Variable Type inference (JEP-286), Switch Extension (JEP-361),
• TextBlocks (JEP-378), Records (JEP-395), Pattern Matching for instanceof (JEP-394)
• Sealed Classes (JEP-409), UTF-8 by Default(JEP-400), Record Patterns(JEP-405) …
• Project Loom: easy to use, hight-throughput, lightweight concurrency and programming approaches
• Virtual Threads (JEP-425), Structured Concurrency(JEP-428)…
?
Simplifying objects creation
• Candidates: Factory or Builder, what is the di
ff
erence ?
• wrapping or step by step till the end, complement, testable…
• Others:
Abstract Factory, Prototype, Singleton, Object pool
Lazy initialisation, Dependency Injection
?
Simplifying objects creation
Builder in JDK: StringBuilder or
public class Thread implements Runnable { //JDK19(Preview)
public sealed interface Builder
permits Builder.OfPlatform,
Builder.OfVirtual,
ThreadBuilders.BaseThreadBuilder {..}}
Factory in JDK: Java Collection framework: List, Set or Map
static <E> List<E> of(E e1, E e2, E e3) {
return ImmutableCollections.listFromTrustedArray(e1, e2, e3);
}
static <K, V> Map<K, V> of(K k1, V v) {
return new ImmutableCollections.Map1<>(k1, v1);
}
?
Simplifying objects creation
record FastCar(String type) implements Car {…
sealed interface Factory permits CarFactory {…
final class CarFactory {
static Vehicle produce(String type) {
var result = switch (type) {
case "fast" -> {
…
yield new FastCar(“super”);
}
case String s
when s.length() > 10 -> new SlowCar()
…
?
Thinking structural
• How to organise a code (instructions) around instantiated objects
• Candidates: Adapter, Flyweight
• Others: Composite, Decorator, Facade, Filter, Module, Front-
Module, Controller, Marker, Proxy, Twin
?
Thinking structural
Adapter in JDK:
public final class Spliterators {…
public static<T> Iterator<T> iterator(Spliterator<? extends T> spliterator) {
Objects.requireNonNull(spliterator);
class Adapter implements Iterator<T>, Consumer<T> {…}}}
public Collections {…
public static <T> ArrayList<T> list(Enumeration<T> e) {…}
Flyweight in JDK:
wrapper classes Integer, Byte, Character and valueOf(…) method uses Cache.
example: return IntegerCache.cache[i + (-IntegerCache.low)];
?
Thinking structural
?
var engine = counter++ % 2 == 0 ? new DieselEngine() : new ElectricEngine();
var fastCar = new FastCar(engine);
class FastCar {
…
FastCar(Engine engine){…}
public void drive(){
if (engine instanceof ElectricEngine ee) {
ee.checkBatteries();
}
engine.run();
…
sealed interface Engine {
void run();
}
Enforcing behaviour at runtime
• What about runtime?
• maintain information exchange between the objects
• Flexibility and maintainability: JVM and codebase
• Candidates: Chain of Responsibility, Command, Caching
• Others: State, Strategy , Interpreter, Iterator, Mediator, Memento,
Null Object, Observer, Pipeline, Template method, Visitor…
?
Enforcing behaviour at runtime
Chain of Responsibility in JDK
public class Logger {
public void log (Level level, Supplier<String> msgSupplier)
//overloaded method, Levels: OFF, SEVERE, WARNING …
Command in JDK:
java.lang.Runnable, java.lang.Callable
Caching in JDK
java.util.Locale
private static class Cache extends LocaleObjectCache<Object, Locale> {
private static final Cache LOCALECACHE = new Cache();
?
Touching concurrency
• What about code running in parallel?
• Multithreaded nature of the problem
• Design patterns combination…
• Candidate: Thread Pool Pattern
• Others: Active Object, Async Method Invocation, Balking,
Double-Checked Locking, Read-Write Lock, Scheduler …
?
Touching concurrency
?
var threadPerTaskExecutor = Executors.newThreadPerTaskExecutor(threadFactory);
var executor = Executors.newVirtualThreadPerTaskExecutor();
threadPerTaskExecutor.execute(() -> {
while (active.get()){
executor.submit(new ComputableTask(counter));
}
})
package dependency (JMC 8.3)
Conclusion
• Design Patterns and new JDK features helps to:
• improve code clarity: Sealed Classes, Pattern Matching
• reduce verbosity: Switch Statement improvements, Records
• enforce maintainability: Project Amber, Project Loom
• simpli
fi
ed composition: VirtualThreads, StructuredConcurrency, Switch, Local-Variable Type
• observability, pro
fi
ling, debugging and understanding
• Java as the 1st language for the JVM
?
Q / A
twitter: @openvalue
email: benedikt@openvalue.de
twitter: @miragemiko
github:@mirage22
email: miro@openvalue.de
Thank YOU !
References:
• Project Amber: https://openjdk.org/projects/amber/
• Project Valhalla: https://openjdk.org/projects/valhalla/
• Project Panama: https://openjdk.org/projects/panama/
• JFR Project: https://github.com/openjdk/jmc
• OpenJDK: https://openjdk.org/
• foojay.io: https://foojay.io/today/author/miro-wengner/
• OpenValue Blog: https://openvalue.blog/
Book In Progress: Practical Design Patterns for Java Developers [PACKT]

New Java features: Simplified Design Patterns[LIT3826]

  • 1.
    Miroslav Wengner New Javafeatures Simplified Design Patterns Benedikt Neumayr
  • 2.
    Safe Harbour Statement Allwhat you will hear can be di ff erent, this presentation is for motivational purposes …
  • 3.
    Miroslav Wengner • Husband,Father, Software Engineer, Author, Blogger, Technology Enthusiast • JCP: Executive Committee Board Member, OpenJDK Committer • Java Mission Control Project, Open-Source projects • Co-Author of Robo4J Project (Duke Award) • Java Champion, JavaOne RockStar • Principal Engineer at OpenValue
  • 4.
    Benedikt Neumayr • Human •Managing Director at OpenValue • Software Architect • Expert for Project Consolidation • Java Enthusiast
  • 5.
    • Group OfTalented and Motivated Individuals • Netherlands, Germany, Switzerland, Austria… • Fun, Development, Trainings, Migration and more…
  • 6.
    Agenda • Java Platformand OpenJDK • SOLID thinking about design patterns • Simplifying objects creation • Thinking structural • Enforcing behaviour at runtime • Touching concurrency • Conclusion • Q/A
  • 7.
    Java Platform andOpenJDK • Compiler => byte-code • JIT => instructions • JVM Interpreter => EXPECTED RESULT • Byte code optimisation: inlining, elimination, scalarization static factory_method.Car produce(java.lang.String); descriptor: (Ljava/lang/String;)Lfactory_method/Car; fl ags: (0x0008) ACC_STATIC Code: stack=7, locals=5, args_size=1 0: new #7 // class factory_method/FactoryMethodEvent 3: dup … 19: aload_3 20: invokevirtual #17 // Method java/lang/String.hashCode:()I 23: lookupswitch { // 2 3135580: 48 3556498: 63 default: 75 } 48: aload_3 49: ldc #23 // String fast 51: invokevirtual #25 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
  • 8.
    SOLID thinking aboutdesign patterns • SOLID principles: • Single responsibility, Open-Close, Liskov Substitution • Interface Segregation, Dependency Inversion • Do NOT Repeat Yourself, Separation of Concerns, CAP • Programming languages and technology agnostic • Micro-services Architecture, Distributed systems • Domain Driven Design, Data streaming (LM, AI) • Extendability, Maintainability or Security split? More pieces there… ? ?
  • 9.
    SOLID thinking aboutdesign patterns • Design Pattern Types: Creational, Structural, Behavioural => Concurrency (services, runtime) • Important projects • Project Valhalla: performance gains through generic API, primitives and value types, ML and BigData apps • Hidden Classes(JEP-371), Warnings for Value-Based Classes (JEP-390)… • Project Panama: interconnecting JVM and well de fi ned non-java APIs, easier access for I/O apps • Foreign Function & Memory API (JEP-424), Vector API (JEP-426)… • Project Amber: improving productivity through Java language evolution • Local-Variable Type inference (JEP-286), Switch Extension (JEP-361), • TextBlocks (JEP-378), Records (JEP-395), Pattern Matching for instanceof (JEP-394) • Sealed Classes (JEP-409), UTF-8 by Default(JEP-400), Record Patterns(JEP-405) … • Project Loom: easy to use, hight-throughput, lightweight concurrency and programming approaches • Virtual Threads (JEP-425), Structured Concurrency(JEP-428)… ?
  • 10.
    Simplifying objects creation •Candidates: Factory or Builder, what is the di ff erence ? • wrapping or step by step till the end, complement, testable… • Others: Abstract Factory, Prototype, Singleton, Object pool Lazy initialisation, Dependency Injection ?
  • 11.
    Simplifying objects creation Builderin JDK: StringBuilder or public class Thread implements Runnable { //JDK19(Preview) public sealed interface Builder permits Builder.OfPlatform, Builder.OfVirtual, ThreadBuilders.BaseThreadBuilder {..}} Factory in JDK: Java Collection framework: List, Set or Map static <E> List<E> of(E e1, E e2, E e3) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3); } static <K, V> Map<K, V> of(K k1, V v) { return new ImmutableCollections.Map1<>(k1, v1); } ?
  • 12.
    Simplifying objects creation recordFastCar(String type) implements Car {… sealed interface Factory permits CarFactory {… final class CarFactory { static Vehicle produce(String type) { var result = switch (type) { case "fast" -> { … yield new FastCar(“super”); } case String s when s.length() > 10 -> new SlowCar() … ?
  • 13.
    Thinking structural • Howto organise a code (instructions) around instantiated objects • Candidates: Adapter, Flyweight • Others: Composite, Decorator, Facade, Filter, Module, Front- Module, Controller, Marker, Proxy, Twin ?
  • 14.
    Thinking structural Adapter inJDK: public final class Spliterators {… public static<T> Iterator<T> iterator(Spliterator<? extends T> spliterator) { Objects.requireNonNull(spliterator); class Adapter implements Iterator<T>, Consumer<T> {…}}} public Collections {… public static <T> ArrayList<T> list(Enumeration<T> e) {…} Flyweight in JDK: wrapper classes Integer, Byte, Character and valueOf(…) method uses Cache. example: return IntegerCache.cache[i + (-IntegerCache.low)]; ?
  • 15.
    Thinking structural ? var engine= counter++ % 2 == 0 ? new DieselEngine() : new ElectricEngine(); var fastCar = new FastCar(engine); class FastCar { … FastCar(Engine engine){…} public void drive(){ if (engine instanceof ElectricEngine ee) { ee.checkBatteries(); } engine.run(); … sealed interface Engine { void run(); }
  • 16.
    Enforcing behaviour atruntime • What about runtime? • maintain information exchange between the objects • Flexibility and maintainability: JVM and codebase • Candidates: Chain of Responsibility, Command, Caching • Others: State, Strategy , Interpreter, Iterator, Mediator, Memento, Null Object, Observer, Pipeline, Template method, Visitor… ?
  • 17.
    Enforcing behaviour atruntime Chain of Responsibility in JDK public class Logger { public void log (Level level, Supplier<String> msgSupplier) //overloaded method, Levels: OFF, SEVERE, WARNING … Command in JDK: java.lang.Runnable, java.lang.Callable Caching in JDK java.util.Locale private static class Cache extends LocaleObjectCache<Object, Locale> { private static final Cache LOCALECACHE = new Cache(); ?
  • 18.
    Touching concurrency • Whatabout code running in parallel? • Multithreaded nature of the problem • Design patterns combination… • Candidate: Thread Pool Pattern • Others: Active Object, Async Method Invocation, Balking, Double-Checked Locking, Read-Write Lock, Scheduler … ?
  • 19.
    Touching concurrency ? var threadPerTaskExecutor= Executors.newThreadPerTaskExecutor(threadFactory); var executor = Executors.newVirtualThreadPerTaskExecutor(); threadPerTaskExecutor.execute(() -> { while (active.get()){ executor.submit(new ComputableTask(counter)); } }) package dependency (JMC 8.3)
  • 20.
    Conclusion • Design Patternsand new JDK features helps to: • improve code clarity: Sealed Classes, Pattern Matching • reduce verbosity: Switch Statement improvements, Records • enforce maintainability: Project Amber, Project Loom • simpli fi ed composition: VirtualThreads, StructuredConcurrency, Switch, Local-Variable Type • observability, pro fi ling, debugging and understanding • Java as the 1st language for the JVM ?
  • 21.
    Q / A twitter:@openvalue email: benedikt@openvalue.de twitter: @miragemiko github:@mirage22 email: miro@openvalue.de Thank YOU !
  • 22.
    References: • Project Amber:https://openjdk.org/projects/amber/ • Project Valhalla: https://openjdk.org/projects/valhalla/ • Project Panama: https://openjdk.org/projects/panama/ • JFR Project: https://github.com/openjdk/jmc • OpenJDK: https://openjdk.org/ • foojay.io: https://foojay.io/today/author/miro-wengner/ • OpenValue Blog: https://openvalue.blog/ Book In Progress: Practical Design Patterns for Java Developers [PACKT]