Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Adapter pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Run-time adapter solution==== A solution using "adapters" proceeds as follows: {{ordered list|list-style-type=lower-roman |Define an intermediary "provider" interface, and write an implementation of that provider interface that wraps the source of the data, {{Java|ClassA}} in this example, and outputs the data formatted as appropriate: <syntaxhighlight lang=Java> public interface StringProvider { public String getStringData(); } public class ClassAFormat1 implements StringProvider { private ClassA classA = null; public ClassAFormat1(final ClassA a) { classA = a; } public String getStringData() { return format(classA.getStringData()); } private String format(final String sourceValue) { // Manipulate the source string into a format required // by the object needing the source object's data return sourceValue.trim(); } } </syntaxhighlight> |Write an adapter class that returns the specific implementation of the provider: <syntaxhighlight lang=Java> public class ClassAFormat1Adapter extends Adapter { public Object adapt(final Object anObject) { return new ClassAFormat1((ClassA) anObject); } } </syntaxhighlight> |Register the {{Java|adapter}} with a global registry, so that the {{Java|adapter}} can be looked up at runtime: <syntaxhighlight lang=Java> AdapterFactory.getInstance().registerAdapter(ClassA.class, ClassAFormat1Adapter.class, "format1"); </syntaxhighlight> |In code, when wishing to transfer data from {{Java|ClassA}} to {{Java|ClassB}}, write: <syntaxhighlight lang=Java> Adapter adapter = AdapterFactory.getInstance() .getAdapterFromTo(ClassA.class, StringProvider.class, "format1"); StringProvider provider = (StringProvider) adapter.adapt(classA); String string = provider.getStringData(); classB.setStringData(string); </syntaxhighlight> or more concisely: <syntaxhighlight lang=Java> classB.setStringData( ((StringProvider) AdapterFactory.getInstance() .getAdapterFromTo(ClassA.class, StringProvider.class, "format1") .adapt(classA)) .getStringData()); </syntaxhighlight> |The advantage can be seen in that, if it is desired to transfer the data in a second format, then look up the different adapter/provider: <syntaxhighlight lang=Java> Adapter adapter = AdapterFactory.getInstance() .getAdapterFromTo(ClassA.class, StringProvider.class, "format2"); </syntaxhighlight> |And if it is desired to output the data from {{Java|ClassA}} as, say, image data in {{Java|Class C}}: <syntaxhighlight lang=Java> Adapter adapter = AdapterFactory.getInstance() .getAdapterFromTo(ClassA.class, ImageProvider.class, "format2"); ImageProvider provider = (ImageProvider) adapter.adapt(classA); classC.setImage(provider.getImage()); </syntaxhighlight> |In this way, the use of adapters and providers allows multiple "views" by {{Java|ClassB}} and {{Java|ClassC}} into {{Java|ClassA}} without having to alter the class hierarchy. In general, it permits a mechanism for arbitrary data flows between objects that can be retrofitted to an existing object hierarchy. }} <!-- Wikipedia is not a list of examples. Do not add examples from your favorite programming language here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language under the sun. Feel free to add examples here: http://en.wikibooks.org/wiki/Computer_Science/Design_Patterns/Adapter -->
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)