345449.vhj5l3oj7.asia Button.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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.pattern. * * Contributor(s): */ import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * Implements a simple extension of JButton that supplies its own * ActionListener and calls its own &lt;code&gt;clicked()&lt;/code&gt; method * whenever the button is pressed. This method calls the &lt;code&gt; * executeCommand()&lt;/code&gt; method on the button's associate &lt;i&gt;Command&lt;/i&gt; * object. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 */ public class Button extends JButton { /** * the command object associated with this button */ protected Command command; /** * Creates a new button with the provided label * * @param name the label of the button */ public Button(String name) { super(name); this.setActionCommand(name); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { clicked(); } }); } /** * Calls &lt;code&gt;ececuteCommand()&lt;/code&gt; on the associated * command object. This method gets called whenever the * button is pressed. */ public void clicked() { if (command != null) { command.executeCommand(); } } /** * Sets the associated command object for this button * * @param command the new &lt;i&gt;Command&lt;/i&gt; object. */ public void setCommand(Command command) { this.command = command; } }</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 ButtonCommand.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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.pattern. * * Contributor(s): */ /** * Implements a sample command. This one prints a short message to * &lt;code&gt;System.out&lt;/code&gt; whenever it executes. The message is * &lt;quote&gt;&quot;ButtonCommand executed&quot;&lt;/quote&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 */ public class ButtonCommand implements Command { /** * Implements a sample command. This one prints a short message to * &lt;code&gt;System.out&lt;/code&gt; whenever it executes. The message is * &lt;quote&gt;&quot;ButtonCommand executed&quot;&lt;/quote&gt;. */ public void executeCommand() { System.out.println(&quot;ButtonCommand executed&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 ButtonCommand2.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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.pattern. * * Contributor(s): */ /** * Implements a sample command. This one prints a short message to * &lt;code&gt;System.out&lt;/code&gt; whenever it executes. The message is * &lt;quote&gt;&quot;ButtonCommand number 2 executed&quot;&lt;/quote&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 */ public class ButtonCommand2 implements Command { /** * Implements a sample command. This one prints a short message to * &lt;code&gt;System.out&lt;/code&gt; whenever it executes. The message is * &lt;quote&gt;&quot;ButtonCommand number 2 executed&quot;&lt;/quote&gt;. */ public void executeCommand() { System.out.println(&quot;ButtonCommand number 2 executed&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 Command.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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.pattern. * * Contributor(s): */ /** * Defines the interface for &lt;i&gt;Command&lt;/i&gt; objects. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 */ public interface Command { /** * Executes the command. */ public void executeCommand(); } </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.command.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.pattern. * * Contributor(s): */ import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * Implements the driver for the command design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Encapsulate a request as an object, thereby letting you * parameterize clients with different requests, queueu or log requests, * and support undoable operations.&lt;/i&gt;&lt;p&gt; * * Participatng objects are &lt;code&gt;Button&lt;/code&gt;s as &lt;i&gt;Invoker&lt;/i&gt;s, * and a &lt;code&gt;ButtonCommand&lt;/code&gt; and &lt;code&gt;ButtonCommand2&lt;/code&gt; as * two concrete &lt;i&gt;Command&lt;/i&gt;s. * * This example creates a simple GUI with three buttons. Each button has a * command associated with it that is executed when the button is pressed. * Button1 and button3 have the same command, button2 has a different one. * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * Both commands and invoker have to know of the pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Button * @see Command * @see ButtonCommand * @see Buttoncommand2 */ public class Main { /** * This example creates a simple GUI with three buttons. Each button has a * command associated with it that is executed when the button is pressed. * Button1 and button3 have the same command, button2 has a different one. */ public static void main(String[] args) { Button button1 = new Button(&quot;Button1&quot;); Button button2 = new Button(&quot;Button2&quot;); Button button3 = new Button(&quot;Button3&quot;); Command com1 = new ButtonCommand(); Command com2 = new ButtonCommand2(); JPanel pane = new JPanel(); pane.add(button1); button1.setCommand(com1); pane.add(button2); button2.setCommand(com2); pane.add(button3); button3.setCommand(com1); // Note: Can not have two commands. // That is within the pattern specification JFrame frame = new JFrame(&quot;Testing: Command Pattern&quot;); frame.getContentPane().add(pane); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); 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 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