277294.ijbsn.asiaAbstractFactory.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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
* <code>createLabel()</code>.
*/
public interface AbstractFactory {
/**
* Creates factory-specific <code>JLabel</code> products.
*
* @return the factory-specific <code>JLabel</code>
*/
public JLabel createLabel();
/**
* Creates factory-specific <code>JButton</code> products.
*
* @return the factory-specific <code>JButton</code>
*/
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:00ZAbstractFactoryEnhancement.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>AbstractFactory</code> 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 <code>createLabel()</code> method.
*
* @return a regular <code>JLabel</code>
*/
public JLabel AbstractFactory.createLabel() {
return new JLabel("This Label was created by " +getName());
}
/**
* Represents a default implementation to all Abstract Factories
* for the <code>createButton()</code> method.
*
* @param a label for the new <code>JButton</code>
* @return a regular <code>JButton</code>
*/
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:00ZDisplay.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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("New GUI");
JLabel label = factory.createLabel();
JButton button = factory.createButton("OK");
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:00ZFramedFactory.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>Abstract Factory</code> interface to provide
* framed Swing GUI components.
*/
public class FramedFactory implements AbstractFactory {
/**
* Creates a framed <code>JLabel</code> object.
*
* @return the framed <code>JLabel</code>
*/
public JLabel createLabel() {
JLabel label = new JLabel("This Label was created by " +getName());
Border raisedbevel = BorderFactory.createRaisedBevelBorder();
Border loweredbevel = BorderFactory.createLoweredBevelBorder();
label.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
return label;
}
/**
* Creates framed <code>JButton</code> objects.
*
* @param the label for the new <code>JButton</code>
* @return the framed <code>JButton</code>
*/
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 "Framed Factory";
}
}</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:00ZMain.java2004-03-24T16:00:00Z2004-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:
* <blockquote>
* Provide an interface for creating families of related or dependent objects
* without specifying their concrete classes.
* </blockquote>
*
* As an example scenario, our abstract factory interface defines two
* factory methods <code>createLabel()</code> and <code>createButton()</code>
* that create related products (Swing GUI elements).
*
* The driver is a swing GUI that allows the user to choose between the two
* concrete factories <code>RegularFactory</code> and <code>FramedFactory
* </code>, and creates a new GUI with elements from the respective factory.
* <p>
*
* <code>RegularFactory</code> creates standard Swing GUI elements, while
* <code>FramedFactory</code> produces elements which are framed.
*
* <P><i>This is the Java implementation. </i><p>
*
* We decided to implement <code>AbstractFactory</code> as an interace,
* not an abstract class. This has the following advantages and liabilities:
* <UL>
* <LI> 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.
* <LI> 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 <code>name</code> field in this
* example.
* </UL>
*
*/
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 <code>JPanel</code> containing the GUI
*/
private static JPanel createGUI()
{
ActionListener radioListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("factory1")) factory = factory1;
else factory = factory2;
}
};
JPanel panel = new JPanel();
JRadioButton factoryButton1 = new JRadioButton("use Factory 1");
JRadioButton factoryButton2 = new JRadioButton("use Factory 2");
factoryButton1.setActionCommand("factory1");
factoryButton2.setActionCommand("factory2");
factoryButton1.addActionListener(radioListener);
factoryButton2.addActionListener(radioListener);
JButton create = new JButton("Create GUI");
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("Abstract Factory Demo");
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:00ZRegularFactory.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>Abstract Factory</code> 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 ("Regular Factory");
}
/**
* Creates a regular <code>JLabel</code> object.
*
* @return the regular <code>JLabel</code>
*/
public JLabel createLabel() {
return new JLabel("This Label was created by " +getName());
}
/**
* Creates regular <code>JButton</code> objects.
*
* @param the label for the new <code>JButton</code>
* @return the regular <code>JButton</code>
*/
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:00Zfiles.lst2004-03-24T16:00:00Z2004-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