Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:20
Show Gist options
  • Save up1/63015b94991a28cff124 to your computer and use it in GitHub Desktop.
Save up1/63015b94991a28cff124 to your computer and use it in GitHub Desktop.
Shopping Cart
while (!cart.isEmpty()) {
shipping.ship(cart.takeNext(), ???);
}
while (!cart.isEmpty()) {
Item currentItem = cart.takeNext();
if(current.isDownloadable) {
shipping.ship(currentItem, shipToEmailAddress);
} else {
shipping.ship(currentItem, shipToSurfaceAddress);
}
}
public interface Item {
public boolean ship(Shipping shipper);
}
public class DownloadableItem implements Item {
public boolean ship(Shipping shipper) {
shipper.ship(this, customer.getEmailAddress());
}
}
public class SurfaceItem implements Item {
public boolean ship(Shipping shipper) {
shipper.ship(this, customer.getSurfaceAddress());
}
}
public class Shipping {
public boolean ship(Item item, SurfaceAddress address) { ... }
public boolean ship(Item item, EMailAddress address { ... }
}
public class ShoppingCart {
private List<Item> cart = new ArrayList<Item>();
public void add(Item item) { cart.add(item); }
public Item takeNext() { return cart.remove(0); }
public boolean isEmpty() { return cart.isEmpty(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment