From 1eaf6bb341d1f9e9fd47112534378e371cf760e2 Mon Sep 17 00:00:00 2001 From: bethrobson Date: Mon, 8 Jun 2020 12:42:41 -0700 Subject: [PATCH] Updated Diner example for Command Pattern to show non-Lambda version, and add separate Lambda version --- .../designpatterns/command/diner/Customer.java | 8 ++++++-- .../designpatterns/command/diner/Diner.java | 5 ++--- .../command/dinerLambda/Cook.java | 14 ++++++++++++++ .../command/dinerLambda/Customer.java | 17 +++++++++++++++++ .../command/dinerLambda/Diner.java | 11 +++++++++++ .../command/dinerLambda/Order.java | 6 ++++++ .../command/dinerLambda/Waitress.java | 10 ++++++++++ 7 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 src/headfirst/designpatterns/command/dinerLambda/Cook.java create mode 100644 src/headfirst/designpatterns/command/dinerLambda/Customer.java create mode 100644 src/headfirst/designpatterns/command/dinerLambda/Diner.java create mode 100755 src/headfirst/designpatterns/command/dinerLambda/Order.java create mode 100644 src/headfirst/designpatterns/command/dinerLambda/Waitress.java diff --git a/src/headfirst/designpatterns/command/diner/Customer.java b/src/headfirst/designpatterns/command/diner/Customer.java index 10c3995c..b764a400 100644 --- a/src/headfirst/designpatterns/command/diner/Customer.java +++ b/src/headfirst/designpatterns/command/diner/Customer.java @@ -2,10 +2,14 @@ public class Customer { Waitress waitress; + Order order; public Customer(Waitress waitress) { this.waitress = waitress; } - public void hungry(Order o) { - waitress.takeOrder(o); + public void createOrder(Order order) { + this.order = order; + } + public void hungry() { + waitress.takeOrder(order); } } \ No newline at end of file diff --git a/src/headfirst/designpatterns/command/diner/Diner.java b/src/headfirst/designpatterns/command/diner/Diner.java index e917ea47..4a695ea7 100644 --- a/src/headfirst/designpatterns/command/diner/Diner.java +++ b/src/headfirst/designpatterns/command/diner/Diner.java @@ -5,8 +5,7 @@ public static void main(String[] args) { Cook cook = new Cook(); Waitress waitress = new Waitress(); Customer customer = new Customer(waitress); - - Order o = () -> { cook.makeBurger(); cook.makeFries(); }; - customer.hungry(o); + customer.createOrder(new BurgerAndFriesOrder(cook)); + customer.hungry(); } } \ No newline at end of file diff --git a/src/headfirst/designpatterns/command/dinerLambda/Cook.java b/src/headfirst/designpatterns/command/dinerLambda/Cook.java new file mode 100644 index 00000000..f16cd68f --- /dev/null +++ b/src/headfirst/designpatterns/command/dinerLambda/Cook.java @@ -0,0 +1,14 @@ +package headfirst.designpatterns.command.dinerLambda; + +public class Cook { + + public Cook() {} + + public void makeBurger() { + System.out.println("Making a burger"); + } + + public void makeFries() { + System.out.println("Making fries"); + } +} \ No newline at end of file diff --git a/src/headfirst/designpatterns/command/dinerLambda/Customer.java b/src/headfirst/designpatterns/command/dinerLambda/Customer.java new file mode 100644 index 00000000..cbfeb77d --- /dev/null +++ b/src/headfirst/designpatterns/command/dinerLambda/Customer.java @@ -0,0 +1,17 @@ +package headfirst.designpatterns.command.dinerLambda; + +public class Customer { + Waitress waitress; + Cook cook; + Order o; + public Customer(Waitress waitress, Cook cook) { + this.waitress = waitress; + this.cook = cook; + } + public void createOrder() { + Order o = () -> { cook.makeBurger(); cook.makeFries(); }; + } + public void hungry() { + waitress.takeOrder(o); + } +} \ No newline at end of file diff --git a/src/headfirst/designpatterns/command/dinerLambda/Diner.java b/src/headfirst/designpatterns/command/dinerLambda/Diner.java new file mode 100644 index 00000000..1df53790 --- /dev/null +++ b/src/headfirst/designpatterns/command/dinerLambda/Diner.java @@ -0,0 +1,11 @@ +package headfirst.designpatterns.command.dinerLambda; + +public class Diner { + public static void main(String[] args) { + Cook cook = new Cook(); + Waitress waitress = new Waitress(); + Customer customer = new Customer(waitress, cook); + customer.createOrder(); + customer.hungry(); + } +} \ No newline at end of file diff --git a/src/headfirst/designpatterns/command/dinerLambda/Order.java b/src/headfirst/designpatterns/command/dinerLambda/Order.java new file mode 100755 index 00000000..457dce09 --- /dev/null +++ b/src/headfirst/designpatterns/command/dinerLambda/Order.java @@ -0,0 +1,6 @@ +package headfirst.designpatterns.command.dinerLambda; + +@FunctionalInterface +public interface Order { + public void orderUp(); +} diff --git a/src/headfirst/designpatterns/command/dinerLambda/Waitress.java b/src/headfirst/designpatterns/command/dinerLambda/Waitress.java new file mode 100644 index 00000000..5ae13ec8 --- /dev/null +++ b/src/headfirst/designpatterns/command/dinerLambda/Waitress.java @@ -0,0 +1,10 @@ +package headfirst.designpatterns.command.dinerLambda; + +public class Waitress { + Order order; + public Waitress() {} + public void takeOrder(Order order) { + this.order = order; + order.orderUp(); + } +} \ No newline at end of file