Java Reference
In-Depth Information
Structural Pattern Code Examples
Adapter
In this example, the PIM uses an API provided by a foreign source. Two files represent the interface into a
purchased set of classes intended to represent contacts. The basic operations are defined in the interface called
Chovnatlh .
Example A.133 Chovnatlh.java
1. public interface Chovnatlh{
2. public String tlhapWa$DIchPong();
3. public String tlhapQavPong();
4. public String tlhapPatlh();
5. public String tlhapGhom();
6.
7. public void cherWa$DIchPong(String chu$wa$DIchPong);
8. public void cherQavPong(String chu$QavPong);
9. public void cherPatlh(String chu$patlh);
10. public void cherGhom(String chu$ghom);
11. }
The implementation for these methods is provided in the associated class, ChovnatlhImpl .
Example A.134 ChovnatlhImpl.java
1. // pong = name
2. // wa'DIch = first
3. // Qav = last
4. // patlh = rank (title)
5. // ghom = group (organization)
6. // tlhap = take (get)
7. // cher = set up (set)
8. // chu' = new
9. // chovnatlh = specimen (contact)
10.
11. public class ChovnatlhImpl implements Chovnatlh{
12. private String wa$DIchPong;
13. private String QavPong;
14. private String patlh;
15. private String ghom;
16.
17. public ChovnatlhImpl(){ }
18. public ChovnatlhImpl(String chu$wa$DIchPong, String chu$QavPong,
19. String chu$patlh, String chu$ghom){
20. wa$DIchPong = chu$wa$DIchPong;
21. QavPong = chu$QavPong;
22. patlh = chu$patlh;
23. ghom = chu$ghom;
24. }
25.
26. public String tlhapWa$DIchPong(){ return wa$DIchPong; }
27. public String tlhapQavPong(){ return QavPong; }
28. public String tlhapPatlh(){ return patlh; }
29. public String tlhapGhom(){ return ghom; }
30.
31. public void cherWa$DIchPong(String chu$wa$DIchPong){ wa$DIchPong = chu$wa$DIchPong; }
32. public void cherQavPong(String chu$QavPong){ QavPong = chu$QavPong; }
33. public void cherPatlh(String chu$patlh){ patlh = chu$patlh; }
34. public void cherGhom(String chu$ghom){ ghom = chu$ghom; }
35.
36. public String toString(){
37. return wa$DIchPong + " " + QavPong + ": " + patlh + ", " + ghom;
38. }
39. }
With help from a translator, it is possible to match the methods to those found in the Contact interface. The
ContactAdapter class performs this task by using a variable to hold an internal ChovnatlhImpl object. This
object manages the information required to hold the Contact information: name, title, and organization.
Example A.135 Contact.java
1. import java.io.Serializable;
2. public interface Contact extends Serializable{
 
 
Search WWH ::




Custom Search