Command pattern: Shopping : Command Pattern « Design Pattern « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Command Pattern 




Command pattern: Shopping

/*

Software Architecture Design Patterns in Java
by Partha Kuchana 

Auerbach Publications

*/



import java.util.HashMap;

public class CommandTest {

  public static void main(String[] args) {

    //Add an item to the CD category
    //create Receiver objects
    Item CD = new Item("A Beautiful Mind");
    Category catCD = new Category("CD");
    //create the command object
    CommandInterface command = new AddCommand(CD, catCD);
    //create the invoker
    ItemManager manager = new ItemManager();
    //configure the invoker
    //with the command object
    manager.setCommand(command);
    manager.process();

    //Add an item to the CD category
    CD = new Item("Duet");
    catCD = new Category("CD");
    command = new AddCommand(CD, catCD);
    manager.setCommand(command);
    manager.process();

    //Add an item to the New Releases category
    CD = new Item("Duet");
    catCD = new Category("New Releases");
    command = new AddCommand(CD, catCD);
    manager.setCommand(command);
    manager.process();

    //Delete an item from the New Releases category
    command = new DeleteCommand(CD, catCD);
    manager.setCommand(command);
    manager.process();

  }
}

class ItemManager {
  CommandInterface command;

  public void setCommand(CommandInterface c) {
    command = c;
  }

  public void process() {
    command.execute();
  }
}

class Item {

  private HashMap categories;

  private String desc;

  public Item(String s) {
    desc = s;
    categories = new HashMap();
  }

  public String getDesc() {
    return desc;
  }

  public void add(Category cat) {
    categories.put(cat.getDesc(), cat);
  }

  public void delete(Category cat) {
    categories.remove(cat.getDesc());
  }

}

class DeleteCommand implements CommandInterface {
  Item item;

  Category cat;

  public DeleteCommand(Item i, Category c) {
    item = i;
    cat = c;
  }

  public void execute() {
    item.delete(cat);
    cat.delete(item);
  }
}

interface CommandInterface {
  public void execute();
}

class Category {

  private HashMap items;

  private String desc;

  public Category(String s) {
    desc = s;
    items = new HashMap();
  }

  public String getDesc() {
    return desc;
  }

  public void add(Item i) {
    items.put(i.getDesc(), i);
    System.out.println("Item '" + i.getDesc() "' has been added to the '"
        + getDesc() "' Category ");
  }

  public void delete(Item i) {
    items.remove(i.getDesc());
    System.out.println("Item '" + i.getDesc()
        "' has been deleted from the '" + getDesc() "' Category ");
  }
}

class AddCommand implements CommandInterface {
  Item item;

  Category cat;

  public AddCommand(Item i, Category c) {
    item = i;
    cat = c;
  }

  public void execute() {
    item.add(cat);
    cat.add(item);
  }
}
           
       














Related examples in the same category
1.Command pattern in Java 1Command pattern in Java 1
2.Command pattern in Java 2Command pattern in Java 2
3.Command pattern in Java 3Command pattern in Java 3
4.Command pattern in Java 4Command pattern in Java 4
5.Command Pattern 2 in Java
6.Command Pattern - Example: FTP GUICommand Pattern - Example: FTP GUI
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.