Skip to content

Commit

Permalink
Updated Diner example for Command Pattern to show non-Lambda version,…
Browse files Browse the repository at this point in the history
… and add separate Lambda version
  • Loading branch information
bethrobson committed Jun 8, 2020
1 parent 5da3229 commit 1eaf6bb
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/headfirst/designpatterns/command/diner/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 2 additions & 3 deletions src/headfirst/designpatterns/command/diner/Diner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
14 changes: 14 additions & 0 deletions src/headfirst/designpatterns/command/dinerLambda/Cook.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
17 changes: 17 additions & 0 deletions src/headfirst/designpatterns/command/dinerLambda/Customer.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 11 additions & 0 deletions src/headfirst/designpatterns/command/dinerLambda/Diner.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
6 changes: 6 additions & 0 deletions src/headfirst/designpatterns/command/dinerLambda/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package headfirst.designpatterns.command.dinerLambda;

@FunctionalInterface
public interface Order {
public void orderUp();
}
10 changes: 10 additions & 0 deletions src/headfirst/designpatterns/command/dinerLambda/Waitress.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 1eaf6bb

Please sign in to comment.