O'Reilly logo
live online training icon Live Online training

Dynamic Proxies in Java

Topic: Software Development
Heinz Kabutz

Don't repeat yourself. The mantra of the effective Java programmer. However, often we need to create similar looking classes for slightly different interfaces in Java. In some cases this can amount to thousands of lines of code. In this course, we learn how to use dynamic proxies to create classes dynamically. We show how thousands of lines of similar looking code is transformed into a few dozen, using the techniques in this course. We study four different variants of this pattern: dynamic proxy, dynamic object adapter, dynamic filter and dynamic composite. The course has hands-on exercises for each pattern that are carefully thought out to maximize learning. The course is based on a book, a PDF of which is included in the course handouts.

What you'll learn-and how you can apply it

  • Introduction to some of the related patterns: proxy, adapter, decorator and filter
  • How to write proxies by hand
  • How to create dynamic proxies
  • How the adapter, decorator and composite can be created dynamically
  • How the Java Platform Module System (JPMS) interacts with dynamic proxies
  • How to match between methods in different classes quickly and efficiently
  • In one of our exercise, we will build a complete JDBC driver that we can use for measuring how long each database operation takes

This training course is for you because...

  • This training is for intermediate to advanced Java programmers


  • Should be very comfortable with at least Java 8 code. Java 11+ is an advantage. Should also have experience working with reflection and data structures.

Course Set-up

Please install the following before the class begins: - JDK 11 or later - IntelliJ 2020.2 or later - Git

Further set-up instructions will be provided at the start of the class.

Recommended Preparation

  • Live Online Training: Data Structures in Java, by Dr Heinz M. Kabutz (search the O’Reilly Learning Platform for an upcoming date)
  • Live Online Training: Design Patterns in Java: Deep Dive, by Dr Heinz M. Kabutz (search the O’Reilly Learning Platform for an upcoming date)
  • Self-Study Class: Data Structures in Java by Dr Heinz M. Kabutz
  • Self-Study Class: Design Patterns in Java by Dr Heinz M. Kabutz
  • Read some of Dr Heinz M. Kabutz newsletters: https://www.javaspecialists.eu/archive
  • Study the design patterns proxy, adapter, decorator, composite, visitor, and chain of responsibility

Recommended Follow-up

About your instructor

  • Heinz Kabutz is the author of “The Java Specialists’ Newsletter”, a publication enjoyed by tens of thousands of Java experts in over 145 countries. His book “Dynamic Proxies (in German)” was #1 Bestseller on Amazon.de in Fachbücher für Informatik for about five minutes until Amazon fixed their algorithm. Thanks to a supportive mother, he has now sold 5 copies.

    Heinz’s Java Specialists’ newsletter is filled with amusing anecdotes of life on the Island of Crete. He is a popular speaker at all the best Java conferences around the world, and also at some of the worst. He teaches Java courses in classrooms around the world, where his prime objective is to make absolutely sure that none of his students fall asleep. He is not always successful.


The timeframes are only estimates and may vary according to how the class is progressing

Day 1

Segment 0: Welcome (40 mins)

  • Welcome
  • History of dynamic proxies
  • Big Win
  • Infrastructure Code
  • Risk of dynamic proxies
  • What will we learn?
  • Java version
  • Shallow vs deep reflection
  • Book sample code
  • How code is modularized

Segment 1: Design Patterns Cousins (40 mins)

  • Proxy Intent
  • Adapter Intent
  • Decorator Intent
  • Composite Intent
  • Chain of Responsibility Intent

Segment 2: Handcrafted Proxies (1 hour, 45 mins)

  • Virtual Proxy
  • Remote Proxy
  • Protection proxy
  • Cascaded Proxies
  • equals() in proxies
  • Exercise/activity description: We will create a JDBC driver by hand for monitoring how long database calls take.

Segment 3: Dynamic Proxy (1 hour, 45 mins)

  • Proxy.newProxyInstance()
  • LoggingInvocationHandler
  • Dissecting a Dynamic Proxy
  • Recasted Exceptions
  • Proxies Facade
  • Virtual Dynamic Proxy
  • Synchronized Dynamic Proxy
  • Cascading Dynamic Proxies
  • EnhancedStream
  • Dynamic Proxy Restrictions
  • Performance
  • Exercise/activity description: We will now change our JDBC driver to use dynamic proxies, thus reducing the amount of code by 30x (Note: Since the exercises take a bit of time, some of the course might spill into day 2)

Day 2

Segment 4: Dynamic Decorator (2 hours)

  • Filtering for Immutability
  • FilterHandler
  • VTable
  • ChainedInvocationHandler
  • Exercise/activity description: Fun exercises involving collections

Segment 5: Dynamic Object Adapter (1 hour)

  • Better Collections
  • Dynamic Object Adapter Restrictions
  • Performance
  • list text hereExercise/activity description: Fun exercise involving singers and rappers, writing own ChainedInvocationHandler

Segment 6: Dynamic Composite (1 hour)

  • Mailing List
  • CompositeHandler
  • TeeAppendables
  • AppendableFlushableCloseable
  • Composing Appendable Dynamically
  • Exercise/activity description: Fun exercise combining the visitor with composite

Segment 7: Conclusion (5 mins)

  • Course wrap-up and next steps