Created
October 22, 2025 16:19
-
-
Save dhsrocha/2145d465725db04153b6f5a873b58d87 to your computer and use it in GitHub Desktop.
An example of the Delegate design pattern in Java.
This file contains hidden or 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 characters
| /** An example of the Delegate design pattern in Java. */ | |
| interface DelegateExample { | |
| /** Main method to demonstrate the delegate pattern. */ | |
| static void main(String[] args) { | |
| Contract implementation = new Implementation(); | |
| Contract delegator = new Delegator(implementation); | |
| delegator.performAction("Test Input"); | |
| } | |
| /** The contract interface defining the method to be implemented. */ | |
| interface Contract { | |
| /** Performs an action with the given input. */ | |
| void performAction(String input); | |
| } | |
| /** The concrete implementation of the contract. */ | |
| final class Implementation implements Contract { | |
| @Override | |
| public void performAction(String input) { | |
| System.out.println("Action performed with input: " + input); | |
| } | |
| } | |
| /** Forwards calls to the delegate implementation. */ | |
| final class Delegator implements Contract { | |
| private final Contract delegate; | |
| Delegator(final Contract delegate) { | |
| this.delegate = delegate; | |
| } | |
| @Override | |
| public void performAction(String input) { | |
| System.out.println("Delegating action..."); | |
| delegate.performAction(input); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment