Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Adapter pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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!
===A further form of runtime adapter pattern=== ====Motivation from compile time solution==== It is desired for {{Java|classA}} to supply {{Java|classB}} with some data, let us suppose some {{Java|String}} data. A compile time solution is: <syntaxhighlight lang=Java> classB.setStringData(classA.getStringData()); </syntaxhighlight> However, suppose that the format of the string data must be varied. A compile time solution is to use inheritance: <syntaxhighlight lang=Java> public class Format1ClassA extends ClassA { @Override public String getStringData() { return format(toString()); } } </syntaxhighlight> and perhaps create the correctly "formatting" object at runtime by means of the [[factory pattern]]. ====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 -->
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Adapter pattern
(section)
Add topic