Sharing a design pattern idea: Reflective Delegate Pattern

Posted by Mysticatly@reddit | programming | View on Reddit | 1 comments

So when I was coding, I wanted a simpler, more organized way to handle responsibilities and make the contract between components clearer. Patterns like Strategy or Facade work fine in theory, but trying to manage multiple responsibilities often felt messy and fragile.

That’s when I started experimenting with what I now call the Reflective Delegate Pattern. After reading feedback and thinking back on my previous post, I consider this a definitive version of the idea.

It’s a bit philosophical and experimental, and not always easy to show clearly in code. Some strict SOLID advocates might disagree, but I find it a helpful way to think about modularity, responsibility management, and runtime organization in a slightly unconventional way.

I call this approach the Reflective Delegate Pattern.


Core idea


Why it works

Cliients only interact with the interfaces, never directly with the logic.
The entity itself doesn’t “own” the logic or data; it simply mirrors the API and forwards calls to its delegates.
This provides modularity, polymorphism, and clean decoupling.

It’s like a Facade + Strategy, but here the Facade implements the same interfaces as its delegates, effectively reflecting their behavior.

Essentially, it’s a specialized form of the Delegate Pattern: instead of a single delegate, the entity can handle multiple responsibilities dynamically, while keeping its API clean and fully polymorphic.


Here’s an example:

Reflective Delegate Pattern
https://github.com/unrays

// Interfaces
interface IPrintable { void print(String msg); }
interface ISavable { void save(String msg); }

// Delegates 
class Printer implements IPrintable { 
    @Override public void print(String msg) { System.out.println("Printing: " + msg); }
}

class Saver implements ISavable {
     @Override public void save(String msg) { System.out.println("Saving: " + msg); } 
}

// Entity reflecting multiple interfaces
class DocumentService implements IPrintable, ISavable {    
    IPrintable printDelegate;
    ISavable saveDelegate;

    @Override public void print(String msg) { printDelegate.print(msg); }
    @Override public void save(String msg) { saveDelegate.save(msg); }  
}

// Usage
public class Main {
    public static void main(String[] args) {
        DocumentService docService = new DocumentService();

        docService.printDelegate = new Printer();
        docService.saveDelegate = new Saver();

        docService.print("Project Plan");
        docService.save("Project Plan");

        docService.printDelegate = (msg) -> System.out.println("Mock printing: " + msg);
        docService.print("Test Document");
    }
}

Key takeaways

Tags: #ReflectorPattern #DelegatePattern #SoftwareArchitecture #DesignPatterns #CleanArchitecture #SOLIDPrinciples #ModularDesign #RuntimePolymorphism #HotSwap #DynamicDelegation #Programming #CodeDesign #CodingIsLife