Sie sind auf Seite 1von 6

1.

ITERATOR

This one is easy. Methods that return an Enumeration demonstrate the Iterator pattern. In Mall.java, ShoppingCart.java,Store.java etc, we have some methods that return an Enumeration, such as the Enumeration of items in the ShoppingCart or the Enumeration of customers in the Mall etc, this demonstrate the Iterator Pattern. This pattern is easy. Sample code:
//Iterator pattern //Enumeration of all stores in the Mall public Enumeration stores(){ Enumeration ev = stores.elements(); return ev; }

2.

COMPOSITE

One way to utilize the Composite pattern is to allow stores to contain specialty stores within them. For example, the Book Store could have a stationary. This pattern is utilized by allowing stores to contain substores.For example,in BookStore, we have Stationary as its substores.To implement this, at first, in 'Store.java', we have defined following methods:
public interface Store { /*Used for 'Composite' pattern */ public Enumeration subStores(); //Enumeration of all substores //Add a substore to this store public boolean add(SubStore ss) throws NoSuchElementException; //Remove a substore from this store public void remove(SubStore ss) throws NoSuchElementException; //Return a specific substore public SubStore getChild(int ssidx) throws NoSuchElementException; //Get the parent store public SubStore getParent(); //Set the parent store public void setParent(SubStore parent); //If the current store is a leaf (no child) public boolean isLeaf(); }

Then, in 'SubStore.java',we implement these methods:


public class SubStore extends Observable implements Store { public boolean isLeaf(){ return leaf; }

public Enumeration subStores(){ Vector v = new Vector(); return v.elements (); } public boolean add(SubStore ss) throws NoSuchElementException { throw new NoSuchElementException("No SubStores"); } public void remove(SubStore ss) throws NoSuchElementException { throw new NoSuchElementException("No SubStores"); } public SubStore getChild(int ssidx) throws NoSuchElementException { throw new NoSuchElementException("No children"); } public SubStore getParent(){ return parent; } public void setParent(SubStore parent){ this.parent = parent; } }

Since 'BookStore' has substores, in 'BookStore.java', we overrides these methodsas follows:


public class BookStore extends SubStore { Vector substores; //Get substores' enumeration //'Iterator' pattern public Enumeration subStores(){ return substores.elements(); } // Add a substore public boolean add(SubStore ss) throws NoSuchElementException { substores.addElement(ss); return true; } //Remove a substore public void remove(SubStore ss) throws NoSuchElementException { substores.removeElement(ss); } //Get a substore public SubStore getChild(int ssidx) throws NoSuchElementException { return (SubStore)substores.elementAt(ssidx); } }

Thus, when we create a BookStore and Stationary, we can just use Composite pattern to add Stationary as children of BookStore. 3. COMMAND

In the customer pop-up window, we have 5 buttons,I use the 'Command Pattern to encapsulate the actions specified, such as Add Item To Cart,Remove Item From Cart, Undo Previous Action, Checking Out & Exit, Notification Register. To implement this pattern, we have an interface 'Command.java':
public interface Command { public void Execute(); public void unDo(); }

and all java commands: AddCommand.java ExitCommand.java RegisterCommand.java UndoCommand.java RemoveCommand.java

To implement this interface, Also we need another interface 'CommandHolder.java' to making use of UI elements:
public interface CommandHolder { public void setCommand(Command comd); public Command getCommand(); }

the 'CmdButton.java' implement this interface as follows:


public class CmdButton extends JButton implements CommandHolder { protected Command buttonCommand; // Constructor public CmdButton(String name){ super(name); } public void setCommand(Command comd){ buttonCommand = comd; //save the command } public Command getCommand(){ return buttonCommand; //return the command } }

Thus, in my 'DemoCustomer.java', I can use above classes as follows:


private CmdButton jButton1; private CmdButton jButton2;

private CmdButton jButton3; private CmdButton jButton4; private CmdButton jButton5; //'Command' pattern jButton1 = new CmdButton ("Add Item To Cart"); jButton1.setPreferredSize (new java.awt.Dimension(160, 18)); jButton1.setBorder (new javax.swing.border.SoftBevelBorder(0)); jButton1.setFont (new java.awt.Font ("Dialog", 1, 11)); jButton1.setBackground (java.awt.Color.lightGray); jButton1.setCommand(new AddCommand(this,jList2,jList3,customer)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); //'Command' pattern //Add an item to shopping cart private void jButton1_actionPerformed(ActionEvent e) { CommandHolder obj = (CommandHolder)e.getSource(); Command cmd = obj.getCommand(); ucmd.add(cmd); cmd.Execute(); }

4. ABSTRACT FACTORY WITH FACTORY METHODS Create all stores using factories. In 'StoreFactory.java', We define an abstract 'StoreFactory' superclass, which has an abstract method 'makeStore()', then I have BookStoreFactory, StationaryFactory, GameStoreFactory and ShoeStoreFactory. All of them extends the superclass of the StoreFactory, with the different implementation of the makeStore() method, and thus make different kinds of stores. Sample code:
public class BookStoreFactory extends StoreFactory { .... public Store makeStore(){ return new BookStore("BookStore", 1); } }

Then We have a StoreCreate class which creates a store using factory methods:
public class StoreCreate { public Store createStore(StoreFactory factory){ Store store = factory.makeStore(); return store;

} }

5.

SINGLETON

This pattern is demonstrated by allow only one instance of any factory type, We only allow one instance of each Store(BookStoreFactory, StationaryFactory, GameStoreFactory,etc) . Sample Code:
/*Class BookStoreFactory only allows one instantiation */ /*Singleton Pattern*/ public class BookStoreFactory extends StoreFactory { private static BookStoreFactory uniqueInstance = null; public static BookStoreFactory instance(){ if (uniqueInstance==null) uniqueInstance = new BookStoreFactory(); return uniqueInstance; } /*The Constructor*/ private BookStoreFactory(){}; public Store makeStore(){ return new BookStore("BookStore", 1); } }

6. OBSERVER All stores should be observable objects. In particular, a customer should be able to be notified whenever a new item has been added for sale at a store. Demonstrate this pattern by having one or more customers register for such notifications with one or more stores. All stores are observable objects, a customer can be able to be notified whenever a new item has been added for sale at a store if he/she registers for such service. Also, to the mall's administrator, the mall itself and all the stores in the mall should be observable objects, the administrator can monitor the most up-to-date status of the mall. And then DemoControl.java and DemoCustomer.java, we have corresponding codes about the use of addObserver() methods, and the corresponding Observer inner class with update(Observable src, Object obj)' method. Sample code: In 'Mall.java':
public class Mall extends Observable { //Customer enter the Mall public void enter(Customer c){ customers.addElement(c); setChanged(); notifyObservers(c);

} //Customer exit the Mall public void exit(Customer c){ int idx = customers.indexOf(c); customers.removeElementAt(idx); setChanged(); notifyObservers(c); } ... } In 'SubStore.java': ... public class SubStore extends Observable implements Store { ... //'Observer' pattern //Add a new item for sale public void addItem(Item item){ items.addElement(item); setChanged(); notifyObservers(item); } ... //'Observer' pattern //Customer enter the store, update corresponding GUI public void enter(Customer c){ customers.addElement(c); setChanged(); notifyObservers(c); } //Customer exit the store, update corresponding GUI public void exit(Customer c){ int idx = customers.indexOf(c); customers.removeElementAt(idx); setChanged(); notifyObservers(c); } ... }