Adapter

From CDOT Wiki
Revision as of 23:50, 25 February 2007 by Rueen (talk | contribs) (Code Examples)
Jump to: navigation, search

Adapter Design Pattern

Adapter, a Structural Pattern and also referred to as a wrapper, is most commonly used when you want to reuse a class to work with a target class but are constrained by incompatible interfaces. The idea is to convert the interface of a reusable class into an interface that your classes expect.

A good analogy for the Adapter pattern would be the adapters that are commonly used to convert the voltage of Canadian/U.S made appliances to be used in different countries. In this case, the appliance (Target) is using the adapter (Adapter) to reuse the socket (Adaptee) already in place.

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.

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.

Classadapter diag rueen.GIF

Adapter UML Diagram

UML diagram example of the Adapter pattern. Specifically, the class adapter pattern

Adapter diag.gif

Image source: http://www.dofactory.com/Patterns/Diagrams/adapter.gif (Copyright 2001 - 2007 Data & Object Factory.)

Code Examples

FreeMind - GNU General Public License

ArrowLinkAdapter.java

/*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: ArrowLinkAdapter.java,v 1.4 2003/12/07 21:00:19 christianfoltin Exp $*/

package freemind.modes;
import freemind.modes.LinkAdapter;
import freemind.main.FreeMindMain;

import java.awt.Point;

public abstract class ArrowLinkAdapter extends LinkAdapter implements MindMapArrowLink {

    /** the zero is the start point of the line;*/
    protected Point startInclination;
    /** the zero is the start point of the line;*/
    protected Point endInclination;
    protected String startArrow;
    protected String endArrow;


    public ArrowLinkAdapter(MindMapNode source,MindMapNode target,FreeMindMain frame)  {
        super(source, target, frame, "standardlinkcolor", "standardlinkstyle");
        startArrow = "None";
        endArrow = "Default";
    }

    public Point getStartInclination() { return startInclination; }
    public Point getEndInclination() { return endInclination; }
    public String getStartArrow() { return startArrow; }
    public String getEndArrow() { return endArrow; }

    public void  setStartInclination(Point startInclination) {  this.startInclination=startInclination; }
    public void  setEndInclination(Point endInclination) {  this.endInclination=endInclination; }
    public void  setStartArrow(String startArrow) {
        if(startArrow == null || startArrow.toUpperCase().equals("NONE")) {
            this.startArrow = "None";
            return;
        } else if(startArrow.toUpperCase().equals("DEFAULT")) {
            this.startArrow = "Default";
            return;
        }
        // dont change:
        System.err.println("Cannot set the start arrow type to " + startArrow);
    }
    public void  setEndArrow(String endArrow) {
        if(endArrow == null || endArrow.toUpperCase().equals("NONE")) {
            this.endArrow = "None";
            return;
        } else if(endArrow.toUpperCase().equals("DEFAULT")) {
            this.endArrow = "Default";
            return;
        }
        // dont change:
        System.err.println("Cannot set the end arrow type to " + endArrow);
    }

    public Object clone() {
        ArrowLinkAdapter arrowLink = (ArrowLinkAdapter) super.clone();
        // now replace the points:
        arrowLink.startInclination = (startInclination==null)?null:new Point(startInclination.x, startInclination.y);
        arrowLink.endInclination = (endInclination==null)?null:new Point(endInclination.x, endInclination.y);
        arrowLink.startArrow = (startArrow==null)?null:new String(startArrow);
        arrowLink.endArrow = (endArrow==null)?null:new String(endArrow);
        return arrowLink;
    }

}

References

Other

Rueen Fiez
BTP600




--Rueen 19:41, 17 January 2007 (EST)