<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Command on Shaaf's blog</title><link>https://shaaf.dev/tags/command/</link><description>Recent content in Command on Shaaf's blog</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Mon, 17 Nov 2008 21:17:24 +0100</lastBuildDate><atom:link href="https://shaaf.dev/tags/command/index.xml" rel="self" type="application/rss+xml"/><item><title>Command, Singleton, JMenuItem, JButton, AbstractButton - One Listener for the app</title><link>https://shaaf.dev/post/2008-11-17-command-singleton-jmenuitem-jbutton-abstractbutton-one-listener-for-the-app/</link><pubDate>Mon, 17 Nov 2008 21:17:24 +0100</pubDate><guid>https://shaaf.dev/post/2008-11-17-command-singleton-jmenuitem-jbutton-abstractbutton-one-listener-for-the-app/</guid><description>&lt;p>Here I would like to demonstrate a simple use of JMenuItems being used with Single Listener for the entire system.
A simple sample of use would probably be SingleInstance Desktop Application.&lt;/p>
&lt;p>Lets see how that is done here.&lt;/p>
&lt;ol>
&lt;li>First lets create a OneListener class that should be able to listen to ActionEvents and also be able to add Commands to itself. Please refer to my previous post on Command,Singleton if you would like to see more about this patterns and there usage.&lt;/li>
&lt;/ol>
&lt;pre tabindex="0">&lt;code>	package com.shaafshah.jmenus;

	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.util.ArrayList;
	import javax.swing.AbstractButton;

	// Implements the ActionListener and is a Singleton also.

	public class OneListener implements ActionListener{

		private static OneListener oneListener = null;

		// Holds the list of all commands registered to this listener
		private ArrayList&amp;lt;Command&amp;gt; commandList = null;

		// A private constructor
		private OneListener(){
			commandList = new ArrayList&amp;lt;Command&amp;gt;();
		}

		// Ensuring one instance.
		public static OneListener getInstance(){
			if(oneListener != null)
				return oneListener;
			else return oneListener = new OneListener();
		}

		// Add the command and add myself as the listener
		public void addCommand(Command command){
			commandList.add(command);
		 ((AbstractButton)command).addActionListener(this);
		}


		// All Events hit here.
		@Override
		public void actionPerformed(ActionEvent e) {
			((Command)e.getSource()).execute();
		}

	}&lt;/code>&lt;/pre>&lt;p>In the above code, the addCommand method adds the command Object and adds a listener to it.
Now how is that possible.
Basically because I am categorizing my UI objects as Commands with in the system having some UI. And I am also assuming that these commands are Currently AbstractButton i.e. JMenuItem, JButton. Lets have a look at the Command Interface and its Implementation.&lt;/p></description></item><item><title>Command</title><link>https://shaaf.dev/post/2008-10-31-command/</link><pubDate>Fri, 31 Oct 2008 20:45:04 +0100</pubDate><guid>https://shaaf.dev/post/2008-10-31-command/</guid><description>&lt;p>By using the command pattern you are seperating the operation from the invoking object. And just because of that it becomes easier to change the command without chagning the caller/s.
This means that you could use Command pattern when you might have the following situation&lt;/p>
&lt;p>You want to parameterize objects to perform an action
You want to specify, execute and queue requests at different times.&lt;/p>
&lt;p>Just to quickly start you need a command object, An interface will keep it easy going in this case, thus providing you with the option of extending other classes e.g. Swing MenuItem or Button.
Below the execute Method is the one invoked to do something when this command is called or asked to do its stuff.
Where as the getCommandName is assumed as a unique name how ever I am sure we can always come up with a better implementation for uniqueness.&lt;/p></description></item></channel></rss>