diff --git a/src/headfirst/designpatterns/adapter/ducks/DuckTestDrive.java b/src/headfirst/designpatterns/adapter/ducks/DuckTestDrive.java index 6881496e..4e0d3b14 100755 --- a/src/headfirst/designpatterns/adapter/ducks/DuckTestDrive.java +++ b/src/headfirst/designpatterns/adapter/ducks/DuckTestDrive.java @@ -1,5 +1,9 @@ package headfirst.designpatterns.adapter.ducks; +import headfirst.designpatterns.adapter.ducks.challenge.Drone; +import headfirst.designpatterns.adapter.ducks.challenge.DroneAdapter; +import headfirst.designpatterns.adapter.ducks.challenge.SuperDrone; + public class DuckTestDrive { public static void main(String[] args) { MallardDuck duck = new MallardDuck(); @@ -16,6 +20,11 @@ public static void main(String[] args) { System.out.println("\nThe TurkeyAdapter says..."); testDuck(turkeyAdapter); + + // Challenge + Drone drone = new SuperDrone(); + Duck droneAdapter = new DroneAdapter(drone); + testDuck(droneAdapter); } static void testDuck(Duck duck) { diff --git a/src/headfirst/designpatterns/command/remoteWL/Command.java b/src/headfirst/designpatterns/command/remoteWL/Command.java index d2bad5cf..264affe4 100755 --- a/src/headfirst/designpatterns/command/remoteWL/Command.java +++ b/src/headfirst/designpatterns/command/remoteWL/Command.java @@ -1,5 +1,6 @@ package headfirst.designpatterns.command.remoteWL; +@FunctionalInterface public interface Command { public void execute(); } diff --git a/src/headfirst/designpatterns/command/simpleremoteWL/Command.java b/src/headfirst/designpatterns/command/simpleremoteWL/Command.java index 61f3c19d..fd87776b 100755 --- a/src/headfirst/designpatterns/command/simpleremoteWL/Command.java +++ b/src/headfirst/designpatterns/command/simpleremoteWL/Command.java @@ -1,5 +1,6 @@ package headfirst.designpatterns.command.simpleremoteWL; +@FunctionalInterface public interface Command { public void execute(); } diff --git a/src/headfirst/designpatterns/composite/menuiterator/MenuTestDrive.java b/src/headfirst/designpatterns/composite/menuiterator/MenuTestDrive.java index ecda7ca9..9016f08b 100755 --- a/src/headfirst/designpatterns/composite/menuiterator/MenuTestDrive.java +++ b/src/headfirst/designpatterns/composite/menuiterator/MenuTestDrive.java @@ -2,7 +2,7 @@ public class MenuTestDrive { public static void main(String args[]) { - + MenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast"); MenuComponent dinerMenu = @@ -108,6 +108,7 @@ public static void main(String args[]) { Waitress waitress = new Waitress(allMenus); waitress.printVegetarianMenu(); + //waitress.printMenu(); } } diff --git a/src/headfirst/designpatterns/decorator/io/InputTest.java b/src/headfirst/designpatterns/decorator/io/InputTest.java index ffaae51e..b4a852e4 100755 --- a/src/headfirst/designpatterns/decorator/io/InputTest.java +++ b/src/headfirst/designpatterns/decorator/io/InputTest.java @@ -5,9 +5,9 @@ public class InputTest { public static void main(String[] args) throws IOException { int c; - + InputStream in = null; try { - InputStream in = + in = new LowerCaseInputStream( new BufferedInputStream( new FileInputStream("test.txt"))); @@ -15,8 +15,20 @@ public static void main(String[] args) throws IOException { while((c = in.read()) >= 0) { System.out.print((char)c); } - - in.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (in != null) { in.close(); } + } + System.out.println(); + try (InputStream in2 = + new LowerCaseInputStream( + new BufferedInputStream( + new FileInputStream("test.txt")))) + { + while((c = in2.read()) >= 0) { + System.out.print((char)c); + } } catch (IOException e) { e.printStackTrace(); } diff --git a/src/headfirst/designpatterns/decorator/starbuzz/CondimentDecorator.java b/src/headfirst/designpatterns/decorator/starbuzz/CondimentDecorator.java index 4601893e..30d1025d 100755 --- a/src/headfirst/designpatterns/decorator/starbuzz/CondimentDecorator.java +++ b/src/headfirst/designpatterns/decorator/starbuzz/CondimentDecorator.java @@ -1,5 +1,6 @@ package headfirst.designpatterns.decorator.starbuzz; public abstract class CondimentDecorator extends Beverage { + Beverage beverage; public abstract String getDescription(); } diff --git a/src/headfirst/designpatterns/decorator/starbuzz/Milk.java b/src/headfirst/designpatterns/decorator/starbuzz/Milk.java index bcd9c010..17e1befe 100755 --- a/src/headfirst/designpatterns/decorator/starbuzz/Milk.java +++ b/src/headfirst/designpatterns/decorator/starbuzz/Milk.java @@ -1,8 +1,6 @@ package headfirst.designpatterns.decorator.starbuzz; public class Milk extends CondimentDecorator { - Beverage beverage; - public Milk(Beverage beverage) { this.beverage = beverage; } diff --git a/src/headfirst/designpatterns/decorator/starbuzz/Mocha.java b/src/headfirst/designpatterns/decorator/starbuzz/Mocha.java index 73469c39..1df34ae6 100755 --- a/src/headfirst/designpatterns/decorator/starbuzz/Mocha.java +++ b/src/headfirst/designpatterns/decorator/starbuzz/Mocha.java @@ -1,8 +1,6 @@ package headfirst.designpatterns.decorator.starbuzz; public class Mocha extends CondimentDecorator { - Beverage beverage; - public Mocha(Beverage beverage) { this.beverage = beverage; } diff --git a/src/headfirst/designpatterns/decorator/starbuzz/Soy.java b/src/headfirst/designpatterns/decorator/starbuzz/Soy.java index 1381c81e..ea6a92cb 100755 --- a/src/headfirst/designpatterns/decorator/starbuzz/Soy.java +++ b/src/headfirst/designpatterns/decorator/starbuzz/Soy.java @@ -1,8 +1,6 @@ package headfirst.designpatterns.decorator.starbuzz; public class Soy extends CondimentDecorator { - Beverage beverage; - public Soy(Beverage beverage) { this.beverage = beverage; } diff --git a/src/headfirst/designpatterns/decorator/starbuzz/Whip.java b/src/headfirst/designpatterns/decorator/starbuzz/Whip.java index 04932be3..318c1872 100755 --- a/src/headfirst/designpatterns/decorator/starbuzz/Whip.java +++ b/src/headfirst/designpatterns/decorator/starbuzz/Whip.java @@ -1,8 +1,6 @@ package headfirst.designpatterns.decorator.starbuzz; public class Whip extends CondimentDecorator { - Beverage beverage; - public Whip(Beverage beverage) { this.beverage = beverage; } diff --git a/src/headfirst/designpatterns/iterator/dinermergeri/MenuTestDrive.java b/src/headfirst/designpatterns/iterator/dinermergeri/MenuTestDrive.java index 5914afb5..8dbe7bbf 100755 --- a/src/headfirst/designpatterns/iterator/dinermergeri/MenuTestDrive.java +++ b/src/headfirst/designpatterns/iterator/dinermergeri/MenuTestDrive.java @@ -5,8 +5,11 @@ public static void main(String args[]) { PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); DinerMenu dinerMenu = new DinerMenu(); Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu); - waitress.printMenu(); - waitress.printVegetarianMenu(); + //waitress.printMenu(); + // -- added 12/30/2016 + waitress.printMenu(1); + // --- + //waitress.printVegetarianMenu(); System.out.println("\nCustomer asks, is the Hotdog vegetarian?"); System.out.print("Waitress says: "); diff --git a/src/headfirst/designpatterns/iterator/dinermergeri/Waitress.java b/src/headfirst/designpatterns/iterator/dinermergeri/Waitress.java index 84351f5f..a645239f 100755 --- a/src/headfirst/designpatterns/iterator/dinermergeri/Waitress.java +++ b/src/headfirst/designpatterns/iterator/dinermergeri/Waitress.java @@ -1,6 +1,7 @@ package headfirst.designpatterns.iterator.dinermergeri; import java.util.Iterator; +import java.util.ArrayList; public class Waitress { Menu pancakeHouseMenu; @@ -10,6 +11,27 @@ public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) { this.pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; } + + // --- added 12/30/2016 - not in original code + public void printMenu(int withNewConstructs) { + ArrayList breakfastItems = ((PancakeHouseMenu) pancakeHouseMenu).getMenuItems(); + //pMenu.forEach(m -> printMenuItem(m)); + for (MenuItem m : breakfastItems) { + printMenuItem(m); + } + + MenuItem[] lunchItems = ((DinerMenu) dinerMenu).getMenuItems(); + for (MenuItem m : lunchItems) { + printMenuItem(m); + } + } + + public void printMenuItem(MenuItem menuItem) { + System.out.print(menuItem.getName() + ", "); + System.out.print(menuItem.getPrice() + " -- "); + System.out.println(menuItem.getDescription()); + } + // --- public void printMenu() { Iterator pancakeIterator = pancakeHouseMenu.createIterator(); diff --git a/src/headfirst/designpatterns/observer/weatherobservable/WeatherStation.java b/src/headfirst/designpatterns/observer/weatherobservable/WeatherStation.java index 107039f4..ad824289 100755 --- a/src/headfirst/designpatterns/observer/weatherobservable/WeatherStation.java +++ b/src/headfirst/designpatterns/observer/weatherobservable/WeatherStation.java @@ -1,5 +1,7 @@ package headfirst.designpatterns.observer.weatherobservable; +import java.util.Observable; + public class WeatherStation { public static void main(String[] args) { diff --git a/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxy.java b/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxy.java index 1a884c5f..dc9b5d99 100755 --- a/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxy.java +++ b/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxy.java @@ -39,7 +39,7 @@ public void paintIcon(final Component c, Graphics g, int x, int y) { g.drawString("Loading CD cover, please wait...", x+300, y+190); if (!retrieving) { retrieving = true; - +/* retrievalThread = new Thread(new Runnable() { public void run() { try { @@ -50,6 +50,15 @@ public void run() { } } }); + */ + retrievalThread = new Thread(() -> { + try { + setImageIcon(new ImageIcon(imageURL, "CD Cover")); + c.repaint(); + } catch (Exception e) { + e.printStackTrace(); + } + }); retrievalThread.start(); } } diff --git a/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxyTestDrive.java b/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxyTestDrive.java index 37a5aa9b..16f69367 100755 --- a/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxyTestDrive.java +++ b/src/headfirst/designpatterns/proxy/virtualproxy/ImageProxyTestDrive.java @@ -15,7 +15,7 @@ public static void main (String[] args) throws Exception { ImageProxyTestDrive testDrive = new ImageProxyTestDrive(); } - public ImageProxyTestDrive() throws Exception{ + public ImageProxyTestDrive() throws Exception { cds.put("Buddha Bar","http://images.amazon.com/images/P/B00009XBYK.01.LZZZZZZZ.jpg"); cds.put("Ima","http://images.amazon.com/images/P/B000005IRM.01.LZZZZZZZ.jpg"); cds.put("Karma","http://images.amazon.com/images/P/B000005DCB.01.LZZZZZZZ.gif"); diff --git a/src/headfirst/designpatterns/strategy/MiniDuckSimulator.java b/src/headfirst/designpatterns/strategy/MiniDuckSimulator.java index 40168b31..f13e8cc6 100755 --- a/src/headfirst/designpatterns/strategy/MiniDuckSimulator.java +++ b/src/headfirst/designpatterns/strategy/MiniDuckSimulator.java @@ -5,7 +5,9 @@ public class MiniDuckSimulator { public static void main(String[] args) { MallardDuck mallard = new MallardDuck(); - RubberDuck rubberDuckie = new RubberDuck(); + FlyBehavior cantFly = () -> System.out.println("I can't fly"); + QuackBehavior squeak = () -> System.out.println("Squeak"); + RubberDuck rubberDuckie = new RubberDuck(cantFly, squeak); DecoyDuck decoy = new DecoyDuck(); Duck model = new ModelDuck(); diff --git a/src/headfirst/designpatterns/strategy/RubberDuck.java b/src/headfirst/designpatterns/strategy/RubberDuck.java index a2761e1b..4c4df51b 100755 --- a/src/headfirst/designpatterns/strategy/RubberDuck.java +++ b/src/headfirst/designpatterns/strategy/RubberDuck.java @@ -4,7 +4,13 @@ public class RubberDuck extends Duck { public RubberDuck() { flyBehavior = new FlyNoWay(); - quackBehavior = new Squeak(); + //quackBehavior = new Squeak(); + quackBehavior = () -> System.out.println("Squeak"); + } + + public RubberDuck(FlyBehavior flyBehavior, QuackBehavior quackBehavior) { + this.flyBehavior = flyBehavior; + this.quackBehavior = quackBehavior; } public void display() {