Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Design Patterns
|
Adapter
References
Example
The
Adaptee
The
Adapter
A simple class to test the
Adapter
design pattern
References
Definition: (
source:
http://en.wikipedia.org/wiki/Adapter_pattern
)
An adapter helps two incompatible interfaces to work together.
Class diagram: (
source:
http://en.wikipedia.org/wiki/Adapter_pattern
)
Example
The
Adaptee
:
public class CustomOperation { public int customAdd(final String a, final String b) { return Integer.valueOf(a) + Integer.valueOf(b); } }
The
Adapter
:
public class Operation { private CustomOperation adaptee; Operation(final CustomOperation adaptee) { this.adaptee = adaptee; } public int add(final int a, final int b) { return adaptee.customAdd(String.valueOf(a), String.valueOf(b)); } }
A simple class to test the
Adapter
design pattern:
public class AdapterPatternTest { public static void main(String[] args) { final CustomOperation adaptee = new CustomOperation(); final Operation adapter = new Operation(adaptee); System.out.println(adapter.add(2, 3)); } }
© 2010-2022
mti
tek