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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
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
w__w__w.j_a___v_a_2___s___.___c_o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.