8. 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…
?
?
9. 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)…
?
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
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);
}
?
12. 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()
…
?
13. 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
?
14. 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)];
?
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 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…
?
17. 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();
?
18. 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 …
?
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 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
?
21. Q / A
twitter: @openvalue
email: benedikt@openvalue.de
twitter: @miragemiko
github:@mirage22
email: miro@openvalue.de
Thank YOU !