Design Pattern Basic Tutorial

adplus-dvertising
Command pattern
Previous Home Next

The command pattern is used to streamlize the objects by help of the interface to encapsulate the request of the object and make the interface which is implemented by the subclass in the order of the parameterized clients.

Benefit and use of the Command pattern
  1. By the command One action can be represented in many ways, this will be like buttons, drop-down menu and popup menu etc.
  2. The command pattern has a callback function, which is used for the register it somewhere to be called later.
  3. It is used to specify and execute the object request at different time.
  4. It is store the states of the action. if we want to undo the action then we are release the state.
  5. It is used the trigger for Decouple the object.

suppose that we are design a Command interface which contain an execute method like this:

public interface Command {
public void exe();
}

Then, we are design the multiple implementation classes and call the powerful the execute() method dynamically. in order we are design the window with the button commands, drop down menu and popup menu with command pattern. we know that the JMenuItem, JButton and JPopupMenu have constructors which is accept the Action type variable. the Action interface extends to the class ActionListener like this.

public interface EventLister {
...
}

public interface ActionListener extends EventListener {
...
}

public interface Action extends ActionListener {
...
}

Example

package r4r;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class ExitButton extends AbstractAction {
private Component target;
public ExitButton(String name, Icon icon, Component t){
putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue(Action.SHORT_DESCRIPTION, name + " the program");
target = t;
} 
public void actionPerformed(ActionEvent evt) {
int answer = JOptionPane.showConfirmDialog(target,
"Are you sure you want to exit? ", "Confirmation",
JOptionPane.YES_NO_OPTION);
if ( answer == JOptionPane.YES_OPTION) { 
System.exit(0);
} 
}
} 

class OKButton extends AbstractAction{
private Component target;
public OKButton(String name, Icon icon,Component t){
putValue(Action.NAME,name);
putValue(Action.SMALL_ICON,icon);
putValue(Action.SHORT_DESCRIPTION,"Click on Button"+name);
		
}
public void actionPerformed(ActionEvent ae){
int ans=JOptionPane.showConfirmDialog(target,
"you are click on the OK button","Conformation",
JOptionPane.YES_NO_CANCEL_OPTION);
if(ans==JOptionPane.YES_OPTION){
int a=JOptionPane.showConfirmDialog(target, "you are right");
}
else if(ans==JOptionPane.NO_OPTION){
int b=JOptionPane.showConfirmDialog(target, "Yes");
}
else
JOptionPane.showConfirmDialog(target, "exit from application");
System.exit(0);
}
		
}
class SubmitAction extends AbstractAction {
private Component target;
public SubmitAction(String name, Icon icon, Component t){
putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue(Action.SHORT_DESCRIPTION, name + " the program");
target = t;
} 
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(target, "submit action clicked ");
}
} 
public class commandtest extends JFrame {
commandtest() {
Action ea = new ExitButton("Exit", null, this);
Action sa = new SubmitAction("Submit", null, this);
Action ob = new OKButton("OK",null,this); 
JMenuBar jbr = new JMenuBar();
JMenu dropmenu= new JMenu("File"); 
JMenuItem submitmenu = new JMenuItem(sa);
JMenuItem exitmenu = new JMenuItem(ea);
dropmenu.add(submitmenu);
dropmenu.add(exitmenu);
jbr.add(dropmenu);
setJMenuBar(jbr);
  
final JPopupMenu pop = new JPopupMenu("PopMenu");
pop.add(sa);
pop.add(ea);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
showPopup(e);
}

public void mouseReleased(MouseEvent e) {
showPopup(e);
}

private void showPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
pop.show(e.getComponent(),
e.getX(), e.getY());
}
}

});
JPanel jp = new JPanel();
JButton subbtn = new JButton(sa);
JButton exitbtn = new JButton(ea);
JButton obtn=new JButton(ob);
jp.add(subbtn);
jp.add(exitbtn);
jp.add(obtn);
  
Container con = getContentPane();
con.add(jp, "South");
  
setTitle("Command pattern example");
setSize(600,400);
setVisible(true);
}
public static void main(String[] args) {
new commandtest(); 
}
}
Previous Home Next