Adapter Pattern in Java 2 : Adapter Pattern « Design Pattern « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Adapter Pattern 




Adapter Pattern in Java 2

//[C] 2002 Sun Microsystems, Inc.---
public class RunAdapterPattern {
    public static void main(String [] arguments){
        System.out.println("Example for the Adapter pattern");
        System.out.println();
        System.out.println("This example will demonstrate the Adapter by using the");
        System.out.println(" class ContactAdapter to translate from classes written");
        System.out.println(" in a foreign language (Chovnatlh and ChovnatlhImpl),");
        System.out.println(" enabling their code to satisfy the Contact interface.");
        System.out.println();
        
        System.out.println("Creating a new ContactAdapter. This will, by extension,");
        System.out.println(" create an instance of ChovnatlhImpl which will provide");
        System.out.println(" the underlying Contact implementation.");
        Contact contact = new ContactAdapter();
        System.out.println();
        
        System.out.println("ContactAdapter created. Setting contact data.");
        contact.setFirstName("Thomas");
        contact.setLastName("Williamson");
        contact.setTitle("Science Officer");
        contact.setOrganization("W3C");
        System.out.println();
        
        System.out.println("ContactAdapter data has been set. Printing out Contact data.");
        System.out.println();
        System.out.println(contact.toString());
    }
}
interface Contact{
    public String getFirstName();
    public String getLastName();
    public String getTitle();
    public String getOrganization();
    
    public void setContact(Chovnatlh newContact);
    public void setFirstName(String newFirstName);
    public void setLastName(String newLastName);
    public void setTitle(String newTitle);
    public void setOrganization(String newOrganization);

}
class ContactAdapter implements Contact{
    private Chovnatlh contact;
    
    public ContactAdapter(){
        contact = new ChovnatlhImpl();
    }
    public ContactAdapter(Chovnatlh newContact){
        contact = newContact;
    }
    
    public String getFirstName(){
        return contact.tlhapWa$DIchPong();
    }
    public String getLastName(){
        return contact.tlhapQavPong();
    }
    public String getTitle(){
        return contact.tlhapPatlh();
    }
    public String getOrganization(){
        return contact.tlhapGhom();
    }
    
    public void setContact(Chovnatlh newContact){
        contact = newContact;
    }
    public void setFirstName(String newFirstName){
        contact.cherWa$DIchPong(newFirstName);
    }
    public void setLastName(String newLastName){
        contact.cherQavPong(newLastName);
    }
    public void setTitle(String newTitle){
        contact.cherPatlh(newTitle);
    }
    public void setOrganization(String newOrganization){
        contact.cherGhom(newOrganization);
    }
    
    public String toString(){
        return contact.toString();
    }
}

interface Chovnatlh {
  public String tlhapWa$DIchPong();

  public String tlhapQavPong();

  public String tlhapPatlh();

  public String tlhapGhom();

  public void cherWa$DIchPong(String chu$wa$DIchPong);

  public void cherQavPong(String chu$QavPong);

  public void cherPatlh(String chu$patlh);

  public void cherGhom(String chu$ghom);
}
//pong = name
//wa'DIch = first
//Qav = last
//patlh = rank (title)
//ghom = group (organization)
//tlhap = take (get)
//cher = set up (set)
//chu' = new
//chovnatlh = specimen (contact)

class ChovnatlhImpl implements Chovnatlh {
  private String wa$DIchPong;

  private String QavPong;

  private String patlh;

  private String ghom;

  public ChovnatlhImpl() {
  }

  public ChovnatlhImpl(String chu$wa$DIchPong, String chu$QavPong,
      String chu$patlh, String chu$ghom) {
    wa$DIchPong = chu$wa$DIchPong;
    QavPong = chu$QavPong;
    patlh = chu$patlh;
    ghom = chu$ghom;
  }

  public String tlhapWa$DIchPong() {
    return wa$DIchPong;
  }

  public String tlhapQavPong() {
    return QavPong;
  }

  public String tlhapPatlh() {
    return patlh;
  }

  public String tlhapGhom() {
    return ghom;
  }

  public void cherWa$DIchPong(String chu$wa$DIchPong) {
    wa$DIchPong = chu$wa$DIchPong;
  }

  public void cherQavPong(String chu$QavPong) {
    QavPong = chu$QavPong;
  }

  public void cherPatlh(String chu$patlh) {
    patlh = chu$patlh;
  }

  public void cherGhom(String chu$ghom) {
    ghom = chu$ghom;
  }

  public String toString() {
    return wa$DIchPong + " " + QavPong + ": " + patlh + ", " + ghom;
  }
}


           
       














Related examples in the same category
1. Adapter Pattern - Example
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.