277294.ijbsn.asiaButton.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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.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 <code>clicked()</code> method
* whenever the button is pressed.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
*/
public class Button extends JButton {
/**
* 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();
}
});
}
/**
* Stub method that is called whenever the button is pressed.
*/
public void clicked() {}
}</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:00ZButtonCommand.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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.pattern.
*
* Contributor(s):
*/
import ca.ubc.cs.spl.pattern.library.Command;
import ca.ubc.cs.spl.pattern.library.CommandReceiver;
/**
* Implements a sample command. This one prints a short message to
* <code>System.out</code> whenever it executes. The message is
* <quote>"ButtonCommand executed"</quote>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.1, 10/29/02
*/
public class ButtonCommand implements Command {
private Printer printer = new Printer();
/**
* Implements a sample command. This one prints a short message to
* <code>System.out</code> whenever it executes. The message is
* <quote>"ButtonCommand executed"</quote>.
*/
public void executeCommand(CommandReceiver receiver ) {
printer.println("ButtonCommand executed");
}
}
</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:00ZButtonCommand2.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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.pattern.
*
* Contributor(s):
*/
/**
* Implements a sample clas that becomes a command in the pattern context.
* Instead of implementing the <i>Command</i> interface directly, the pattern
* aspect assigns that role to this class and fills in the <code>
* executeCommand()</code> method.<p>
*
* This illustrates that any exisiting class can be turned into a <i>Command
* </i> without having to change the class itself.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*/
public class ButtonCommand2 {}
</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:00ZButtonCommanding.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.aspectj;
import ca.ubc.cs.spl.pattern.library.CommandProtocol;
import ca.ubc.cs.spl.pattern.library.Command;
import ca.ubc.cs.spl.pattern.library.CommandInvoker;
import ca.ubc.cs.spl.pattern.library.CommandReceiver;
import java.io.PrintStream;
public aspect ButtonCommanding extends CommandProtocol {
declare parents: Button implements CommandInvoker;
declare parents: Printer implements CommandReceiver;
declare parents: ButtonCommand implements Command; // Unneccessay
declare parents: ButtonCommand2 implements Command; // "Making" a class
// a Command
/**
* Implements a sample command. This one prints a short message to
* <code>System.out</code> whenever it executes. The message is
* <quote>"ButtonCommand number 2 executed"</quote>.
*/
public void ButtonCommand2.executeCommand(CommandReceiver receiver) {
((Printer) receiver).println("ButtonCommand number 2 executed");
}
/**
* The join points after which to execute the command.
* This replaces the normally scattered myCommand.execute() calls.
* In this example, a call to <code>Button.clicked()</code> triggers
* the execution of the command.
*
* @param invoker the object invoking the command
*/
protected pointcut commandTrigger(CommandInvoker invoker):
call(void Button.clicked()) && target(invoker);
}
</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.command.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.pattern.
*
* Contributor(s):
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import ca.ubc.cs.spl.pattern.library.Command;
/**
* Implements the driver for the command design pattern example.<p>
*
* Intent: <i>Encapsulate a request as an object, thereby letting you
* parameterize clients with different requests, queueu or log requests,
* and support undoable operations.</i><p>
*
* Participatng objects are <code>Button</code>s as <i>Invoker</i>s,
* and a <code>ButtonCommand</code> and <code>ButtonCommand2</code> as
* two concrete <i>Command</i>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.
*
* <p><i>This is the Java version.</i><p>
*
* This version of the pattern lets the developer specify what should trigger
* a call to <code>executeCommand()</code>, without changing the <i>Invoker
* </i> code.
*
* Neither <i>Commands</i> nor <i>Invoker</i> have to know of their
* involvement in the pattern and can actually act as both.
* <code>ButtonCommanding2</code> is an example of a <i>Command</i> that
* is unaware of its role. In such cases, the concrete pattern instance
* aspect assigns the role and defines the <i>Command</i>'s behavior.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Button
* @see ButtonCommand
* @see Buttoncommand2
*/
public class Main extends JFrame {
/**
* 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("Button1");
Button button2 = new Button("Button2");
Button button3 = new Button("Button3");
Command com1 = new ButtonCommand();
Command com2 = new ButtonCommand2();
JPanel pane = new JPanel();
pane.add(button1);
ButtonCommanding.aspectOf().setCommand(button1, com1);
pane.add(button2);
ButtonCommanding.aspectOf().setCommand(button2, com2);
ButtonCommanding.aspectOf().setReceiver(com2, new Printer());
pane.add(button3);
ButtonCommanding.aspectOf().setCommand(button3, com1);
JFrame frame = new JFrame("Testing: Command Pattern");
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:00ZPrinter.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.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):
*/
/**
* Helper class that is used as a receiver for the Command pattern.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.1, 10/29/02
*
*/
public class Printer {
public void println(String s) {
System.out.println(s);
}
}
</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