Java/Design Pattern/Adapter Pattern

Материал из Java эксперт
Перейти к: навигация, поиск

Adapter Pattern - Example

   <source lang="java">

/* Software Architecture Design Patterns in Java by Partha Kuchana Auerbach Publications

  • /

import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import com.sun.java.swing.plaf.windows.WindowsLookAndFeel; public class AddressManager extends JFrame {

 public static final String newline = "\n";
 public static final String VALIDATE = "Validate";
 public static final String EXIT = "Exit";
 private JTextField txtCustomerName, txtAddress, txtZip, txtState;
 private JLabel lblCustomerName, lblAddress;
 private JLabel lblZip, lblState, lblType;
 private JComboBox cmbAddressType;
 private JLabel lblResult, lblResultValue;
 public AddressManager() {
   super(" Adapter Pattern - Example ");
   txtCustomerName = new JTextField(20);
   txtAddress = new JTextField(20);
   txtZip = new JTextField(10);
   txtState = new JTextField(10);
   cmbAddressType = new JComboBox();
   cmbAddressType.addItem(Customer.US);
   cmbAddressType.addItem(Customer.CANADA);
   lblCustomerName = new JLabel("Customer Name:");
   lblAddress = new JLabel("Address:");
   lblZip = new JLabel("Zip/PostalCode:");
   lblState = new JLabel("State/Province:");
   lblType = new JLabel("Address Type:");
   lblResult = new JLabel("Result:");
   lblResultValue = new JLabel("Click the Validate Button");
   //Create the open button
   JButton validateButton = new JButton(AddressManager.VALIDATE);
   validateButton.setMnemonic(KeyEvent.VK_V);
   JButton exitButton = new JButton(AddressManager.EXIT);
   exitButton.setMnemonic(KeyEvent.VK_X);
   ButtonHandler objButtonHandler = new ButtonHandler(this);
   validateButton.addActionListener(objButtonHandler);
   exitButton.addActionListener(new ButtonHandler());
   JPanel captionPanel = new JPanel();
   String captionText = "<html>"
       + "Adapter Pattern Example</html>";
   JLabel captionLabel = new JLabel(captionText);
   captionPanel.add(captionLabel);
   JPanel UIPanel = new JPanel();
   //****************************************************
   GridBagLayout gridbag = new GridBagLayout();
   UIPanel.setLayout(gridbag);
   GridBagConstraints gbc = new GridBagConstraints();
   UIPanel.add(lblCustomerName);
   UIPanel.add(txtCustomerName);
   UIPanel.add(lblAddress);
   UIPanel.add(txtAddress);
   UIPanel.add(lblZip);
   UIPanel.add(txtZip);
   UIPanel.add(lblState);
   UIPanel.add(txtState);
   UIPanel.add(lblType);
   UIPanel.add(cmbAddressType);
   UIPanel.add(lblResult);
   UIPanel.add(lblResultValue);
   UIPanel.add(validateButton);
   UIPanel.add(exitButton);
   gbc.insets.top = 5;
   gbc.insets.bottom = 5;
   gbc.insets.left = 5;
   gbc.insets.right = 5;
   gbc.anchor = GridBagConstraints.WEST;
   gbc.gridx = 0;
   gbc.gridy = 0;
   gridbag.setConstraints(lblCustomerName, gbc);
   gbc.gridx = 1;
   gbc.gridy = 0;
   gridbag.setConstraints(txtCustomerName, gbc);
   gbc.gridx = 0;
   gbc.gridy = 1;
   gridbag.setConstraints(lblAddress, gbc);
   gbc.gridx = 1;
   gbc.gridy = 1;
   gridbag.setConstraints(txtAddress, gbc);
   gbc.gridx = 0;
   gbc.gridy = 2;
   gridbag.setConstraints(lblZip, gbc);
   gbc.gridx = 1;
   gbc.gridy = 2;
   gridbag.setConstraints(txtZip, gbc);
   gbc.gridx = 0;
   gbc.gridy = 3;
   gridbag.setConstraints(lblState, gbc);
   gbc.gridx = 1;
   gbc.gridy = 3;
   gridbag.setConstraints(txtState, gbc);
   gbc.gridx = 0;
   gbc.gridy = 4;
   gridbag.setConstraints(lblType, gbc);
   gbc.gridx = 1;
   gbc.gridy = 4;
   gridbag.setConstraints(cmbAddressType, gbc);
   gbc.gridx = 0;
   gbc.gridy = 5;
   gridbag.setConstraints(lblResult, gbc);
   gbc.gridx = 1;
   gbc.gridy = 5;
   gridbag.setConstraints(lblResultValue, gbc);
   //gbc.anchor = GridBagConstraints.CENTER;
   gbc.insets.left = 2;
   gbc.insets.right = 2;
   gbc.insets.top = 40;
   JPanel buttonPanel = new JPanel();
   buttonPanel.add(validateButton);
   buttonPanel.add(exitButton);
   UIPanel.add(buttonPanel);
   gbc.gridx = 1;
   gbc.gridy = 6;
   gridbag.setConstraints(buttonPanel, gbc);
   //
   //Add the buttons and the log to the frame
   Container contentPane = getContentPane();
   contentPane.add(UIPanel, BorderLayout.CENTER);
   try {
     UIManager.setLookAndFeel(new WindowsLookAndFeel());
     SwingUtilities.updateComponentTreeUI(AddressManager.this);
   } catch (Exception ex) {
     System.out.println(ex);
   }
 }
 public static void main(String[] args) {
   JFrame frame = new AddressManager();
   frame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e) {
       System.exit(0);
     }
   });
   //frame.pack();
   frame.setSize(350, 300);
   frame.setVisible(true);
 }
 public String getCustomerName() {
   return txtCustomerName.getText();
 }
 public String getAddress() {
   return txtAddress.getText();
 }
 public String getZip() {
   return txtZip.getText();
 }
 public String getAddrState() {
   return txtState.getText();
 }
 public String getAddressType() {
   return (String) cmbAddressType.getSelectedItem();
 }
 public void setResultValue(String str) {
   lblResultValue.setText(str);
 }

} // End of class AddressManager class ButtonHandler implements ActionListener {

 AddressManager objAddressManager;
 public void actionPerformed(ActionEvent e) {
   String validationResult = null;
   //System.out.println(e.getActionCommand());
   if (e.getActionCommand().equals(AddressManager.EXIT)) {
     System.exit(1);
   }
   if (e.getActionCommand().equals(AddressManager.VALIDATE)) {
     String custName = objAddressManager.getCustomerName();
     String address = objAddressManager.getAddress();
     String zip = objAddressManager.getZip();
     String state = objAddressManager.getAddrState();
     String addressType = objAddressManager.getAddressType();
     //Create a customer object
     Customer objCustomer = new Customer(custName, address, zip, state,
         addressType);
     //Check if the address is valid
     if (objCustomer.isValidCustomerAddress()) {
       validationResult = "Valid customer data";
     } else {
       validationResult = "Invalid customer data";
     }
     objAddressManager.setResultValue(validationResult);
   }
 }
 public ButtonHandler() {
 }
 public ButtonHandler(AddressManager inObjAddressManager) {
   objAddressManager = inObjAddressManager;
 }

} // End of class ButtonHandler class USAddress extends AddressValidator {

 public boolean isValidAddress(String inp_address, String inp_zip,
     String inp_state) {
   if (inp_address.trim().length() < 10)
     return false;
   if (inp_zip.trim().length() < 5)
     return false;
   if (inp_zip.trim().length() > 10)
     return false;
   if (inp_state.trim().length() != 2)
     return false;
   return true;
 }

}// end of class class Customer {

 public static final String US = "US";
 public static final String CANADA = "Canada";
 private String address;
 private String name;
 private String zip, state, type;
 public boolean isValidCustomerAddress() {
   if (type.equals(Customer.US)) {
     USAddress us = new USAddress();
     return us.isValidAddress(address, zip, state);
   }
   if (type.equals(Customer.CANADA)) {
     CAAddress objCAAddress = new CAAddress();
     CAAddressAdapter adapter = new CAAddressAdapter(objCAAddress);
     return adapter.isValidAddress(address, zip, state);
   }
   return true;
 }
 public Customer(String inp_name, String inp_address, String inp_zip,
     String inp_state, String inp_type) {
   name = inp_name;
   address = inp_address;
   zip = inp_zip;
   state = inp_state;
   type = inp_type;
 }

}// end of class class CAAddressAdapter extends AddressValidator {

 private CAAddress objCAAddress;
 public CAAddressAdapter(CAAddress address) {
   objCAAddress = address;
 }
 public boolean isValidAddress(String inp_address, String inp_zip,
     String inp_state) {
   return objCAAddress
       .isValidCanadianAddr(inp_address, inp_zip, inp_state);
 }

}// end of class class CAAddress {

 public boolean isValidCanadianAddr(String inp_address, String inp_pcode,
     String inp_prvnc) {
   if (inp_address.trim().length() < 15)
     return false;
   if (inp_pcode.trim().length() != 6)
     return false;
   if (inp_prvnc.trim().length() < 6)
     return false;
   return true;
 }

}// end of class abstract class AddressValidator {

 public abstract boolean isValidAddress(String inp_address, String inp_zip,
     String inp_state);

}// end of class


      </source>
   
  
 
  



Adapter Pattern in Java 2

   <source lang="java">

//[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;
 }

}


      </source>