Skip to content

Commit

Permalink
Updates to code files
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Apr 28, 2019
1 parent 3ae3397 commit 9d04e30
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/headfirst/designpatterns/adapter/ducks/DuckTestDrive.java
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/headfirst/designpatterns/command/remoteWL/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package headfirst.designpatterns.command.remoteWL;

@FunctionalInterface
public interface Command {
public void execute();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package headfirst.designpatterns.command.simpleremoteWL;

@FunctionalInterface
public interface Command {
public void execute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class MenuTestDrive {
public static void main(String args[]) {

MenuComponent pancakeHouseMenu =
new Menu("PANCAKE HOUSE MENU", "Breakfast");
MenuComponent dinerMenu =
Expand Down Expand Up @@ -108,6 +108,7 @@ public static void main(String args[]) {
Waitress waitress = new Waitress(allMenus);

waitress.printVegetarianMenu();
//waitress.printMenu();

}
}
20 changes: 16 additions & 4 deletions src/headfirst/designpatterns/decorator/io/InputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@
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")));

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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package headfirst.designpatterns.decorator.starbuzz;

public abstract class CondimentDecorator extends Beverage {
Beverage beverage;
public abstract String getDescription();
}
2 changes: 0 additions & 2 deletions src/headfirst/designpatterns/decorator/starbuzz/Milk.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package headfirst.designpatterns.decorator.starbuzz;

public class Milk extends CondimentDecorator {
Beverage beverage;

public Milk(Beverage beverage) {
this.beverage = beverage;
}
Expand Down
2 changes: 0 additions & 2 deletions src/headfirst/designpatterns/decorator/starbuzz/Mocha.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package headfirst.designpatterns.decorator.starbuzz;

public class Mocha extends CondimentDecorator {
Beverage beverage;

public Mocha(Beverage beverage) {
this.beverage = beverage;
}
Expand Down
2 changes: 0 additions & 2 deletions src/headfirst/designpatterns/decorator/starbuzz/Soy.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package headfirst.designpatterns.decorator.starbuzz;

public class Soy extends CondimentDecorator {
Beverage beverage;

public Soy(Beverage beverage) {
this.beverage = beverage;
}
Expand Down
2 changes: 0 additions & 2 deletions src/headfirst/designpatterns/decorator/starbuzz/Whip.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package headfirst.designpatterns.decorator.starbuzz;

public class Whip extends CondimentDecorator {
Beverage beverage;

public Whip(Beverage beverage) {
this.beverage = beverage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
22 changes: 22 additions & 0 deletions src/headfirst/designpatterns/iterator/dinermergeri/Waitress.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package headfirst.designpatterns.iterator.dinermergeri;

import java.util.Iterator;
import java.util.ArrayList;

public class Waitress {
Menu pancakeHouseMenu;
Expand All @@ -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<MenuItem> 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<MenuItem> pancakeIterator = pancakeHouseMenu.createIterator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package headfirst.designpatterns.observer.weatherobservable;

import java.util.Observable;

public class WeatherStation {

public static void main(String[] args) {
Expand Down
11 changes: 10 additions & 1 deletion src/headfirst/designpatterns/proxy/virtualproxy/ImageProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion src/headfirst/designpatterns/strategy/MiniDuckSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion src/headfirst/designpatterns/strategy/RubberDuck.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 9d04e30

Please sign in to comment.