carfield.com.hk AbstractFactory.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.abstractFactory.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import javax.swing.JLabel; import javax.swing.JButton; /** * Defines the interface for creating products. The only factory method is * &lt;code&gt;createLabel()&lt;/code&gt;. */ public interface AbstractFactory { /** * Creates factory-specific &lt;code&gt;JLabel&lt;/code&gt; products. * * @return the factory-specific &lt;code&gt;JLabel&lt;/code&gt; */ public JLabel createLabel(); /** * Creates factory-specific &lt;code&gt;JButton&lt;/code&gt; products. * * @return the factory-specific &lt;code&gt;JButton&lt;/code&gt; */ public JButton createButton(String label); /** * Returns the name of the factory. * * @return the name of the factory */ public String getName(); }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z AbstractFactoryEnhancement.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.abstractFactory.aspectj; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import javax.swing.JLabel; import javax.swing.JButton; /** * Illustrates AspectJ's Open Classes: A default implementation of the * two factory methods defined in the &lt;code&gt;AbstractFactory&lt;/code&gt; interface * is attached to the interface. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public aspect AbstractFactoryEnhancement { /** * Represents a default implementation to all Abstract Factories * for the &lt;code&gt;createLabel()&lt;/code&gt; method. * * @return a regular &lt;code&gt;JLabel&lt;/code&gt; */ public JLabel AbstractFactory.createLabel() { return new JLabel(&quot;This Label was created by &quot; +getName()); } /** * Represents a default implementation to all Abstract Factories * for the &lt;code&gt;createButton()&lt;/code&gt; method. * * @param a label for the new &lt;code&gt;JButton&lt;/code&gt; * @return a regular &lt;code&gt;JButton&lt;/code&gt; */ public JButton AbstractFactory.createButton(String label) { return new JButton(label); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Display.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.abstractFactory.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * Sets up and displays a new GUI given a concrete factory. */ public class Display extends JFrame { /** * Sets up the frame with a label and a button created by the respective * concrete factory. Both button and frame receive their appropriate * listeners to close the frame when either the button is clicked or * the frame is closing. * * @param factory the factory to create GUI elements */ Display(AbstractFactory factory) { super(&quot;New GUI&quot;); JLabel label = factory.createLabel(); JButton button = factory.createButton(&quot;OK&quot;); button.addActionListener(new myActionListener(this)); JPanel panel = new JPanel(); panel.add(label); panel.add(button); this.getContentPane().add(panel); this.pack(); this.setVisible(true); this.addWindowListener(new myWindowListener(this)); } private class myWindowListener extends WindowAdapter { Display display = null; protected myWindowListener(Display display) { super(); this.display = display; } public void windowClosing(WindowEvent e) { display.setVisible(false); } } private class myActionListener implements ActionListener { Display display; protected myActionListener(Display display) { super(); this.display = display; } public void actionPerformed(ActionEvent e) { display.setVisible(false); } } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z FramedFactory.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.abstractFactory.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.BorderFactory; import javax.swing.JButton; /** * Implements the &lt;code&gt;Abstract Factory&lt;/code&gt; interface to provide * framed Swing GUI components. */ public class FramedFactory implements AbstractFactory { /** * Creates a framed &lt;code&gt;JLabel&lt;/code&gt; object. * * @return the framed &lt;code&gt;JLabel&lt;/code&gt; */ public JLabel createLabel() { JLabel label = new JLabel(&quot;This Label was created by &quot; +getName()); Border raisedbevel = BorderFactory.createRaisedBevelBorder(); Border loweredbevel = BorderFactory.createLoweredBevelBorder(); label.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel)); return label; } /** * Creates framed &lt;code&gt;JButton&lt;/code&gt; objects. * * @param the label for the new &lt;code&gt;JButton&lt;/code&gt; * @return the framed &lt;code&gt;JButton&lt;/code&gt; */ public JButton createButton(String label) { JButton button = new JButton(label); Border raisedbevel = BorderFactory.createRaisedBevelBorder(); Border loweredbevel = BorderFactory.createLoweredBevelBorder(); button.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel)); return button; } /** * Returns the name of the factory. * * @return the name of the factory */ public String getName() { return &quot;Framed Factory&quot;; } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Main.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.abstractFactory.java; import javax.swing.JFrame; import javax.swing.JRadioButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.ButtonGroup; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Implements a driver for the Abstract Factory design pattern example. * The pattern intent is: * &lt;blockquote&gt; * Provide an interface for creating families of related or dependent objects * without specifying their concrete classes. * &lt;/blockquote&gt; * * As an example scenario, our abstract factory interface defines two * factory methods &lt;code&gt;createLabel()&lt;/code&gt; and &lt;code&gt;createButton()&lt;/code&gt; * that create related products (Swing GUI elements). * * The driver is a swing GUI that allows the user to choose between the two * concrete factories &lt;code&gt;RegularFactory&lt;/code&gt; and &lt;code&gt;FramedFactory * &lt;/code&gt;, and creates a new GUI with elements from the respective factory. * &lt;p&gt; * * &lt;code&gt;RegularFactory&lt;/code&gt; creates standard Swing GUI elements, while * &lt;code&gt;FramedFactory&lt;/code&gt; produces elements which are framed. * * &lt;P&gt;&lt;i&gt;This is the Java implementation. &lt;/i&gt;&lt;p&gt; * * We decided to implement &lt;code&gt;AbstractFactory&lt;/code&gt; as an interace, * not an abstract class. This has the following advantages and liabilities: * &lt;UL&gt; * &lt;LI&gt; Concrete factories do not need to be subclasses of the abstract * factory. That way existing classes can be subclassed and just have * to implement the interface methods. This can be more flexible if * we want an existing class to become a factory (e.g. because we * want to make use of its functionality. * &lt;LI&gt; By defining the abstract factory as an interface we cannot attach * any (default) implementations to its methods, nor is it possible * to include fields (such as the &lt;code&gt;name&lt;/code&gt; field in this * example. * &lt;/UL&gt; * */ public class Main { /** * one of the alternative concrete factories */ private static AbstractFactory factory1 = new RegularFactory(); /** * the other of the alternative concrete factories */ private static AbstractFactory factory2 = new FramedFactory(); /** * stores the currently selected factory */ private static AbstractFactory factory = factory1; /** * Creates the initial GUI that allows the user to choose a factory * and generate a new GUI with the elements that the respective * factory provides. * * @return a &lt;code&gt;JPanel&lt;/code&gt; containing the GUI */ private static JPanel createGUI() { ActionListener radioListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(&quot;factory1&quot;)) factory = factory1; else factory = factory2; } }; JPanel panel = new JPanel(); JRadioButton factoryButton1 = new JRadioButton(&quot;use Factory 1&quot;); JRadioButton factoryButton2 = new JRadioButton(&quot;use Factory 2&quot;); factoryButton1.setActionCommand(&quot;factory1&quot;); factoryButton2.setActionCommand(&quot;factory2&quot;); factoryButton1.addActionListener(radioListener); factoryButton2.addActionListener(radioListener); JButton create = new JButton(&quot;Create GUI&quot;); ButtonGroup choices = new ButtonGroup(); choices.add(factoryButton1); choices.add(factoryButton2); create.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Display display = new Display(factory); } }); panel.add(factoryButton1); panel.add(factoryButton2); panel.add(create); return panel; } /** * Implements the driver for this design pattern example. It sets up * the initial GUI. */ public static void main(String[] args) { JFrame frame = new JFrame(&quot;Abstract Factory Demo&quot;); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); frame.getContentPane().add(createGUI()); frame.pack(); frame.setVisible(true); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z RegularFactory.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.abstractFactory.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import javax.swing.JLabel; import javax.swing.JButton; /** * Implements the &lt;code&gt;Abstract Factory&lt;/code&gt; interface to provide * regular Swing GUI components. */ public class RegularFactory implements AbstractFactory { /** * Returns the name of the factory. * * @return the name of the factory */ public String getName() { return (&quot;Regular Factory&quot;); } /** * Creates a regular &lt;code&gt;JLabel&lt;/code&gt; object. * * @return the regular &lt;code&gt;JLabel&lt;/code&gt; */ public JLabel createLabel() { return new JLabel(&quot;This Label was created by &quot; +getName()); } /** * Creates regular &lt;code&gt;JButton&lt;/code&gt; objects. * * @param the label for the new &lt;code&gt;JButton&lt;/code&gt; * @return the regular &lt;code&gt;JButton&lt;/code&gt; */ public JButton createButton(String label) { return new JButton(label); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z files.lst 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z