Java Design Patterns .

Java Design Patterns Command

Command (aka Action or Transaction) Overview

An object encapsulates everything needed to execute a method in another object.


DvdName.java - the Receiver

public class DvdName {  
   private String titleName;  
    
   public DvdName(String titleName) {
       this.setTitleName(titleName);
   }    
  
   public final void setTitleName(String titleNameIn) {
       this.titleName = titleNameIn;
   }
   public final String getTitleName() {
       return this.titleName;
   }
   
   public void setNameStarsOn() {
       this.setTitleName(this.getTitleName().replace(' ','*'));
   }
   public void setNameStarsOff() {
       this.setTitleName(this.getTitleName().replace('*',' '));
   }   
   
   public String toString() {
       return ("DVD: " + this.getTitleName());
   }
}
To download source right-click here and "Save As...".


CommandAbstract.java - the Command

public abstract class CommandAbstract {
   public abstract void execute();
}
To download source right-click here and "Save As...".


DvdCommandNameStarsOn.java - one of two Concrete Commands

public class DvdCommandNameStarsOn extends CommandAbstract {
   private DvdName dvdName;  
    
   public DvdCommandNameStarsOn(DvdName dvdNameIn) {
       this.dvdName = dvdNameIn;
   }    
   public void execute() {
       this.dvdName.setNameStarsOn();
   } 
}
To download source right-click here and "Save As...".


DvdCommandNameStarsOff.java - two of two Concrete Commands

public class DvdCommandNameStarsOff extends CommandAbstract {  
   private DvdName dvdName;  
    
   public DvdCommandNameStarsOff(DvdName dvdNameIn) {
       this.dvdName = dvdNameIn;
   }    
   public void execute() {
       this.dvdName.setNameStarsOff();
   } 
}
To download source right-click here and "Save As...".


TestCommand.java - testing the Command

class TestCommand {            
   public static void main(String[] args) {
       DvdName jayAndBob = 
         new DvdName("Jay and Silent Bob Strike Back");
       DvdName spongeBob = 
         new DvdName("Sponge Bob Squarepants - " +
                     "Nautical Nonsense and Sponge Buddies");
       System.out.println("as first instantiated");
       System.out.println(jayAndBob.toString());  
       System.out.println(spongeBob.toString()); 
       
       CommandAbstract bobStarsOn = 
         new DvdCommandNameStarsOn(jayAndBob);
       CommandAbstract bobStarsOff = 
         new DvdCommandNameStarsOff(jayAndBob);
       CommandAbstract spongeStarsOn = 
         new DvdCommandNameStarsOn(spongeBob);
       CommandAbstract spongeStarsOff = 
         new DvdCommandNameStarsOff(spongeBob);

bobStarsOn.execute(); spongeStarsOn.execute(); System.out.println(" "); System.out.println("stars on"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString());

spongeStarsOff.execute(); System.out.println(" "); System.out.println("sponge stars off"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); } }
To download source right-click here and "Save As...".


Test Results



as first instantiated DVD: Jay and Silent Bob Strike Back DVD: Sponge Bob Squarepants - Nautical Nonsense and Sponge Buddies stars on DVD: Jay*and*Silent*Bob*Strike*Back DVD: Sponge*Bob*Squarepants*-*Nautical*Nonsense*and*Sponge*Buddies sponge stars off DVD: Jay*and*Silent*Bob*Strike*Back DVD: Sponge Bob Squarepants - Nautical Nonsense and Sponge Buddies

UML

UML for Command

References


online
Portland Pattern Repository
Books
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Java Design Patterns - A Tutorial by James W. Cooper
Comments
Sign in to be the first to comment on Java Design Patterns Command.