Open main menu

CDOT Wiki β

Changes

Adapter

2,600 bytes added, 16:11, 27 March 2007
Object Adapter Pattern
There are two specific types of adapter patterns - ''object adapter pattern'' and ''class adapter pattern''. The latter uses multiple inheritance whereas the former uses an instance of the reuseable class it wants to adapt. Between the two, object adapter pattern is the favored one due to the fact that popular languages such as Java do not support multiple inheritance.
== Object Adapter Pattern ==
This adapter pattern uses an instance of the class it wraps. By using an instance, methods belonging to the wrapped object can be invoked.
[[Image:objectadapter_diag_rueen2.GIF]]
== Class Adapter Pattern ==
This adapter pattern uses multiple inheritance as a means to wrap the reusable class and use its functionality.
'''Java'''
FreeMind - GNU General Public License. In this example, an ArrowLinkAdapter is created to be used to adapt different arrow looking links in FreeMind's MindMap area.
FreeMind's [http://www.google.com/codesearch?hl=en&q=show:SSjMlfyYK2U:RCv_23t_s7k:tpy_iiX4ncI&sa=N&ct=rd&cs_p=http://gentoo.osuosl.org/distfiles/freemind-src-0_7_1.tar.gz&cs_f=freemind/freemind/modes/ArrowLinkAdapter.java File Repository]
 
 
Explanation:
 
In this example, an ArrowLinkAdapter is created to be used to adapt different arrow shaped links in FreeMind's MindMap area. ArrowLinkAdapter.java is the '''Adapter''' and LinkAdapter.java is the '''Adaptee'''. Even though LinkAdapter is an adapter itself, it is infact the adaptee in this case.
 
 
'''ArrowLinkAdapter.java'''
return arrowLink;
}
 
}
 
</pre>
 
'''LinkAdapter'''
 
<pre>
/*FreeMind - A Program for creating and viewing Mindmaps
*Copyright (C) 2000-2001 Joerg Mueller <joergmueller@bigfoot.com>
*See COPYING for Details
*
*This program is free software; you can redistribute it and/or
*modify it under the terms of the GNU General Public License
*as published by the Free Software Foundation; either version 2
*of the License, or (at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program; if not, write to the Free Software
*Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*$Id: LinkAdapter.java,v 1.3 2003/11/29 17:12:33 christianfoltin Exp $*/
 
package freemind.modes;
import freemind.modes.LineAdapter;
import freemind.main.FreeMindMain;
 
public abstract class LinkAdapter extends LineAdapter implements MindMapLink {
 
String destinationLabel;
String referenceText;
MindMapNode source;
 
public LinkAdapter(MindMapNode source,MindMapNode target,FreeMindMain frame) {
this(source,target, frame, "standardlinkcolor", "standardlinkstyle");
}
 
/** For derived classes.*/
protected LinkAdapter(MindMapNode source,MindMapNode target,FreeMindMain frame, String standardColorPropertyString, String standardStylePropertyString) {
super(target, frame, standardColorPropertyString, standardStylePropertyString);
this.source=source;
destinationLabel = null;
referenceText = null;
}
 
public String getDestinationLabel() { return destinationLabel; }
public String getReferenceText(){ return referenceText; }
public MindMapNode getSource() { return source;}
 
public void setSource(MindMapNode source) {this.source=source;}
public void setDestinationLabel(String destinationLabel) { this.destinationLabel = destinationLabel; }
public void setReferenceText(String referenceText) { this.referenceText = referenceText; }
 
// public Object clone() {
// try {
// return super.clone();
// } catch(java.lang.CloneNotSupportedException e) {
// return null;
// }
// }
 
}
1
edit