Adapter mode: 1. Substation type: Structural mode
2. Type
Two kinds:
Class Adapter: EXTENDS IMPLEMENTS
Object Adapter: Aggregation Implements
(Individual thinks there is also imports imports)
2. Conditions: a. Two unrequited class combinations, the first solution is to modify the interfaces of each class, but if we don't have source code, or we don't want to modify your respective interfaces for an application . What should I do? Use Adapter, create a hybrid interface (mixed-born) between the two interfaces .b. The other people who need to complete have completed similar, reuse, need to inherit new attributes.
3: Basic ideas: Connect two interfaces, first inherit one, then use aggregate, or abstract its excuse, implement two interfaces, and use the aggregation of these two interfaces. In short, Extends Aggregation
4: Example:
A: Publici // interface of public interface shape {// PUBLIC VOID BORDER ();
PUBLIC CLASS TEXT {/ II Private String Content; public text () {} public void setcontent (String Str) {content = str;} public string getcontent () {returnTent;}}
public class TextShapeClass extends Text implements Shape {// Class adapter // direct inheritance, reuse of Text method (extends), while achieving shape function (implements) public TextShapeClass () {} public void Draw () {System.out. Println ("Draw A Shap!");} public void border () {system.out.println ("set the border of the shap! impleelent shape interface!");} public static void main (String " ] args) {TextShapeClass myTextShapeClass = new TextShapeClass (); myTextShapeClass.Draw (); myTextShapeClass.Border (); myTextShapeClass.SetContent ( "! A test text"); System.out.println ( "The content in text Shape is: " mytextshapeclass.getContent ());}}
Public Class TextshapeObject Implements Shape {// Object Adapter // Aggregation Text is the aggregate object of the interface, and implements the ability of Shape (Implements)
Private text txt; public textshapeObject (text t) {txt = t;} public void draw () {system.out.println ("Draw A Shap! Impelement Shape Interface!");} public void border () {System.out .println ("SET the Border of THE SHAP!");} public void setContent (String str) {txt.setcontent (STR);} public string getcontent () {return txt.getcontent ();} public static void main (String [] args) {text myText = new text (); TextShapeObject myTextShapeObject = new TextShapeObject (myText); myTextShapeObject.Draw (); myTextShapeObject.Border (); myTextShapeObject.SetContent ( "! A test text") System.out.println ("The Content in text Shape IS:" mytextshapeObject.getContent ());}}
Link: http://www.jdon.com/designpatterns/adapter.htm