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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Design Pattern » Adapter PatternScreenshots 
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.