Abstract Factory Pattern- Example : Factory Pattern « Design Pattern « Java

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 Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Design Pattern » Factory PatternScreenshots 
Abstract Factory Pattern- Example

/*
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.SwingUtilities;
import javax.swing.UIManager;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

public class AutoSearchUI extends JFrame {
  public static final String newline = "\n";

  public static final String SEARCH = "Search";

  public static final String EXIT = "Exit";

  public static final String CAR = "Car";

  public static final String SUV = "SUV";

  private JComboBox cmbVehicleCategory, cmbVehicleType;

  private JLabel lblVehicleCategory, lblVehicleType, lblCarName,
      lblCarNameValue;

  public AutoSearchUI() {
    super("Abstract Factory - Example");

    cmbVehicleCategory = new JComboBox();
    cmbVehicleCategory.addItem(VehicleFactory.LUXURY_VEHICLE);
    cmbVehicleCategory.addItem(VehicleFactory.NON_LUXURY_VEHICLE);

    cmbVehicleType = new JComboBox();
    cmbVehicleType.addItem(AutoSearchUI.CAR);
    cmbVehicleType.addItem(AutoSearchUI.SUV);

    lblVehicleCategory = new JLabel("Vehicle Category:");
    lblVehicleType = new JLabel("VehicleType:");
    lblCarName = new JLabel("Search Result:");
    lblCarNameValue = new JLabel(" Please click on Search button");

    //Create the open button
    JButton openButton = new JButton(AutoSearchUI.SEARCH);
    openButton.setMnemonic(KeyEvent.VK_S);
    JButton exitButton = new JButton(AutoSearchUI.EXIT);
    exitButton.setMnemonic(KeyEvent.VK_X);
    ButtonHandler objButtonHandler = new ButtonHandler(this);

    openButton.addActionListener(objButtonHandler);
    exitButton.addActionListener(new ButtonHandler());

    JPanel buttonPanel = new JPanel();

    //****************************************************
    GridBagLayout gridbag = new GridBagLayout();
    buttonPanel.setLayout(gridbag);
    GridBagConstraints gbc = new GridBagConstraints();

    buttonPanel.add(lblVehicleCategory);
    buttonPanel.add(cmbVehicleCategory);
    buttonPanel.add(lblVehicleType);
    buttonPanel.add(cmbVehicleType);
    buttonPanel.add(lblCarName);
    buttonPanel.add(lblCarNameValue);

    buttonPanel.add(openButton);
    buttonPanel.add(exitButton);

    gbc.insets.top = 5;
    gbc.insets.bottom = 5;
    gbc.insets.left = 5;
    gbc.insets.right = 5;

    gbc.anchor = GridBagConstraints.EAST;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gridbag.setConstraints(lblVehicleCategory, gbc);
    gbc.anchor = GridBagConstraints.WEST;

    gbc.gridx = 1;
    gbc.gridy = 0;
    gridbag.setConstraints(cmbVehicleCategory, gbc);
    gbc.anchor = GridBagConstraints.EAST;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gridbag.setConstraints(lblVehicleType, gbc);
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gridbag.setConstraints(cmbVehicleType, gbc);
    gbc.anchor = GridBagConstraints.EAST;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gridbag.setConstraints(lblCarName, gbc);
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 1;
    gbc.gridy = 2;
    gridbag.setConstraints(lblCarNameValue, gbc);

    gbc.insets.left = 2;
    gbc.insets.right = 2;
    gbc.insets.top = 40;
    gbc.anchor = GridBagConstraints.EAST;

    gbc.gridx = 0;
    gbc.gridy = 5;
    gridbag.setConstraints(openButton, gbc);
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 1;
    gbc.gridy = 5;
    gridbag.setConstraints(exitButton, gbc);

    //****************************************************

    //Add the buttons and the log to the frame
    Container contentPane = getContentPane();

    contentPane.add(buttonPanel, BorderLayout.CENTER);
    try {
      UIManager.setLookAndFeel(new WindowsLookAndFeel());
      SwingUtilities.updateComponentTreeUI(AutoSearchUI.this);
    catch (Exception ex) {
      System.out.println(ex);
    }

  }

  public static void main(String[] args) {
    JFrame frame = new AutoSearchUI();

    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    //frame.pack();
    frame.setSize(400300);
    frame.setVisible(true);
  }

  public String getSelectedCategory() {
    return (StringcmbVehicleCategory.getSelectedItem();
  }

  public String getSelectedType() {
    return (StringcmbVehicleType.getSelectedItem();
  }

  public void setResult(String searchResult) {
    lblCarNameValue.setText(searchResult);
  }
// End of class AutoSearchUI

class ButtonHandler implements ActionListener {
  AutoSearchUI objAutoSearchUI;

  public void actionPerformed(ActionEvent e) {
    String searchResult = null;

    if (e.getActionCommand().equals(AutoSearchUI.EXIT)) {
      System.exit(1);
    }
    if (e.getActionCommand().equals(AutoSearchUI.SEARCH)) {
      //get input values
      String vhCategory = objAutoSearchUI.getSelectedCategory();
      String vhType = objAutoSearchUI.getSelectedType();

      //get one of Luxury or NonLuxury vehicle factories
      VehicleFactory vf = VehicleFactory.getVehicleFactory(vhCategory);

      if (vhType.equals(AutoSearchUI.CAR)) {
        Car c = vf.getCar();
        searchResult = "Name: " + c.getCarName() "  Features: "
            + c.getCarFeatures();
      }
      if (vhType.equals(AutoSearchUI.SUV)) {
        SUV s = vf.getSUV();
        searchResult = "Name: " + s.getSUVName() "  Features: "
            + s.getSUVFeatures();
      }
      objAutoSearchUI.setResult(searchResult);
    }

  }

  public ButtonHandler() {
  }

  public ButtonHandler(AutoSearchUI inObjAutoSearchUI) {
    objAutoSearchUI = inObjAutoSearchUI;
  }

// End of class ButtonHandler

abstract class VehicleFactory {
  public static final String LUXURY_VEHICLE = "Luxury";

  public static final String NON_LUXURY_VEHICLE = "Non-Luxury";

  public abstract Car getCar();

  public abstract SUV getSUV();

  public static VehicleFactory getVehicleFactory(String type) {
    if (type.equals(VehicleFactory.LUXURY_VEHICLE))
      return new LuxuryVehicleFactory();
    if (type.equals(VehicleFactory.NON_LUXURY_VEHICLE))
      return new NonLuxuryVehicleFactory();

    return new LuxuryVehicleFactory();
  }

// End of class

interface Car {
  public String getCarName();

  public String getCarFeatures();

// End of class

interface SUV {
  public String getSUVName();

  public String getSUVFeatures();

// End of class

class LuxuryCar implements Car {
  private String name;

  public LuxuryCar(String cName) {
    name = cName;
  }

  public String getCarName() {
    return name;
  }

  public String getCarFeatures() {
    return "Luxury Car Features ";
  };

// End of class

class LuxurySUV implements SUV {
  private String name;

  public LuxurySUV(String sName) {
    name = sName;
  }

  public String getSUVName() {
    return name;
  }

  public String getSUVFeatures() {
    return "Luxury SUV Features ";
  };

// End of class

class LuxuryVehicleFactory extends VehicleFactory {

  public Car getCar() {
    return new LuxuryCar("L-C");
  }

  public SUV getSUV() {
    return new LuxurySUV("L-S");
  }
// End of class

class NonLuxuryCar implements Car {
  private String name;

  public NonLuxuryCar(String cName) {
    name = cName;
  }

  public String getCarName() {
    return name;
  }

  public String getCarFeatures() {
    return "Non-Luxury Car Features ";
  };

// End of class

class NonLuxurySUV implements SUV {
  private String name;

  public NonLuxurySUV(String sName) {
    name = sName;
  }

  public String getSUVName() {
    return name;
  }

  public String getSUVFeatures() {
    return "Non-Luxury SUV Features ";
  };

// End of class

class NonLuxuryVehicleFactory extends VehicleFactory {

  public Car getCar() {
    return new NonLuxuryCar("NL-C");
  }

  public SUV getSUV() {
    return new NonLuxurySUV("NL-S");
  }
// End of class



           
       
Related examples in the same category
1. Abstract Factory Pattern in Java 2
2. Factory Method Pattern in Java
3. Illustrates use of Abstract Factory patternIllustrates use of Abstract Factory pattern
4. Fly weight FactoryFly weight Factory
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.