Skip to content

Commit

Permalink
adding files for Bootcamp II
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Jan 7, 2020
1 parent dec2636 commit e83cdfc
Show file tree
Hide file tree
Showing 37 changed files with 860 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package headfirst.designpatterns.bridge.remote;

public class Client {
public static void main(String[] args) {
TVFactory tvFactory = new TVFactory();
SpecialRemote remoteSony = new SpecialRemote(tvFactory);
System.out.println("Connect your remote to the TV");
remoteSony.setTV("Sony");
remoteSony.on();
remoteSony.up();
remoteSony.down();
remoteSony.off();

GenericRemote remoteLG = new GenericRemote(tvFactory);
System.out.println("Connect your remote to the TV");
remoteLG.setTV("LG");
remoteLG.on();
remoteLG.nextChannel();
remoteLG.prevChannel();
remoteLG.off();
}
}
15 changes: 15 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/GenericRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package headfirst.designpatterns.bridge.remote;

public class GenericRemote extends RemoteControl {
public GenericRemote(TVFactory tvFactory) {
super(tvFactory);
}
public void nextChannel() {
int channel = this.getChannel();
this.setChannel(channel+1);
}
public void prevChannel() {
int channel = this.getChannel();
this.setChannel(channel-1);
}
}
18 changes: 18 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/LG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package headfirst.designpatterns.bridge.remote;

public class LG extends TV {
int channel = 1;
public void on() {
System.out.println("Turning on the LG TV");
}
public void off() {
System.out.println("Turning off the LG TV");
}
public void tuneChannel(int channel) {
this.channel = channel;
System.out.println("Set the LG TV Channel to " + this.channel);
}
public int getChannel() {
return channel;
}
}
28 changes: 28 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/RemoteControl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package headfirst.designpatterns.bridge.remote;

public abstract class RemoteControl {
TV tv;
TVFactory tvFactory;
public RemoteControl(TVFactory tvFactory) {
this.tvFactory = tvFactory;
}
public void on() {
this.tv.on();
}
public void off() {
this.tv.off();
}
public void setChannel(int channel) {
tv.tuneChannel(channel);
}
public int getChannel() {
return tv.getChannel();
}
public void setTV(String type) {
try {
tv = tvFactory.getTV(type);
} catch (Exception e) {
System.out.println(e);
}
}
}
18 changes: 18 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/Sony.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package headfirst.designpatterns.bridge.remote;

public class Sony extends TV {
int station = 0;
public void on() {
System.out.println("Turning on the Sony TV");
}
public void off() {
System.out.println("Turning off the Sony TV");
}
public void tuneChannel(int channel) {
this.station = channel;
System.out.println("Set the Sony TV station to " + this.station);
}
public int getChannel() {
return station;
}
}
15 changes: 15 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/SpecialRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package headfirst.designpatterns.bridge.remote;

public class SpecialRemote extends RemoteControl {
public SpecialRemote(TVFactory tvFactory) {
super(tvFactory);
}
public void up() {
int channel = this.getChannel();
this.setChannel(channel+1);
}
public void down() {
int channel = this.getChannel();
this.setChannel(channel-1);
}
}
8 changes: 8 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/TV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package headfirst.designpatterns.bridge.remote;

public abstract class TV {
public abstract void on();
public abstract void off();
public abstract void tuneChannel(int channel);
public abstract int getChannel();
}
13 changes: 13 additions & 0 deletions src/headfirst/designpatterns/bridge/remote/TVFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package headfirst.designpatterns.bridge.remote;

public class TVFactory {
public TV getTV(String type) throws Exception {
if (type.equals("LG")) {
return new LG();
} else if (type.equals("Sony")) {
return new Sony();
} else {
throw new Exception("Invalid TV Type");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package headfirst.designpatterns.builder.pizza;

public class MeatLoversPizzaBuilder extends PizzaBuilder {
public MeatLoversPizzaBuilder() {
this.name = "Meat Lovers Pizza";
}
public PizzaBuilder addCheese() {
// meat lovers like moz
this.toppings.add("mozzerella");
return this;
}
public PizzaBuilder addSauce() {
this.toppings.add("NY style sauce");
return this;
}
public PizzaBuilder addTomatoes() {
this.toppings.add("sliced tomatoes");
return this;
}
public PizzaBuilder addGarlic() {
this.toppings.add("garlic");
return this;
}
public PizzaBuilder addOlives() {
// never add olives to meat lovers pizza
return this;
}
public PizzaBuilder addSpinach() {
// never add spinach to meat lovers pizza
return this;
}
public PizzaBuilder addPepperoni() {
this.toppings.add("pepperoni");
return this;
}
public PizzaBuilder addSausage() {
this.toppings.add("sausage");
return this;
}
}
50 changes: 50 additions & 0 deletions src/headfirst/designpatterns/builder/pizza/Pizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package headfirst.designpatterns.builder.pizza;

import java.util.*;

public class Pizza {
String name;
List<String> toppings;

void addToppings(List<String> toppings) {
this.toppings = toppings;
}

void prepare() {
System.out.println("Prepare " + name);
System.out.println("Tossing dough...");
System.out.println("Adding sauce...");
System.out.println("Adding toppings: ");
for (String topping : toppings) {
System.out.println(" " + topping);
}
}

void bake() {
System.out.println("Bake for 25 minutes at 350");
}

void cut() {
System.out.println("Cut the pizza into diagonal slices");
}

void box() {
System.out.println("Place pizza in official PizzaStore box");
}

public void setName(String name) {
this.name = name;
}

public String toString() {
StringBuffer display = new StringBuffer();
display.append("---- " + this.name + " ----\n");
for (String topping : toppings) {
display.append(topping + "\n");
}
return display.toString();
}
}



24 changes: 24 additions & 0 deletions src/headfirst/designpatterns/builder/pizza/PizzaBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package headfirst.designpatterns.builder.pizza;

import java.util.ArrayList;
import java.util.List;

public abstract class PizzaBuilder {
String name;
List<String> toppings = new ArrayList<String>();

public abstract PizzaBuilder addCheese();
public abstract PizzaBuilder addSauce();
public abstract PizzaBuilder addTomatoes();
public abstract PizzaBuilder addGarlic();
public abstract PizzaBuilder addOlives();
public abstract PizzaBuilder addSpinach();
public abstract PizzaBuilder addPepperoni();
public abstract PizzaBuilder addSausage();
public Pizza build() {
Pizza pizza = new Pizza();
pizza.setName(this.name);
pizza.addToppings(toppings);
return pizza;
}
}
56 changes: 56 additions & 0 deletions src/headfirst/designpatterns/builder/pizza/PizzaDirector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package headfirst.designpatterns.builder.pizza;

public class PizzaDirector {

// Fluent Interface pattern (return the builder each time so we can string the calls together)
// Builder pattern (we have two different builders so the "same construction process can create
// different representations").
// Builder is a solution to the telescoping constructor anti-pattern, where we have multiple,
// complex constructor methods with various args for all various combinations of options
// in construction options.

// in this example main() is the construct() method
public static void main(String[] args) {
// Could hand builders to PizzaStore which would take the customer's order,
// and call appropriate methods for each topping, then call the
// pizza methods to prep and return to the customer.
PizzaBuilder veggieBuilder = new VeggieLoversPizzaBuilder();
// The PizzaDirector calls the methods in the correct order to
// build a veggiePizza.
Pizza veggie = veggieBuilder.addSauce().addCheese().addOlives().addTomatoes().addSausage().build();
veggie.prepare();
veggie.bake();
veggie.cut();
veggie.box();
System.out.println(veggie);

PizzaBuilder meatBuilder = new MeatLoversPizzaBuilder();
// The PizzaDirector calls the methods in the correct order to build
// a meat lovers Pizza
Pizza meat = meatBuilder.addSauce().addTomatoes().addCheese().addSausage().addPepperoni().build();
meat.prepare();
meat.bake();
meat.cut();
meat.box();
System.out.println(meat);

// There is some difference of opinion about StringBuilder and whether it's using
// the Builder pattern or not. Some say yes, some say no.
// StringBuilder does not provide an abstract API with multiple subclasses for
// creating different representations (variations). So, strictly, no, it doesn't
// use the Builder Pattern, but rather the Fluent Interface Pattern.
StringBuilder sb = new StringBuilder();
sb.append("\nTesting String Builder\n").append(veggie).insert(0, "====");
System.out.println("Length of the String Builder: " + sb.length());
System.out.println("Result of the String Builder: " + sb.toString());

String sb2 = new StringBuilder().append("\nTesting String Builder\n").append(meat).insert(0, "====").toString();
System.out.println(sb2);

// Builder has similarities to Abstract Factory.
// But difference is that Builder provides a step by step API for building a product;
// the client is responsible for calling the steps, and those can vary in order, etc.
// With Builder, the client must have more knowledge of the details of the product being built.
// Product implementations can be swapped for others; clients don't change because the use the abstract API.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package headfirst.designpatterns.builder.pizza;

public class VeggieLoversPizzaBuilder extends PizzaBuilder {
public VeggieLoversPizzaBuilder() {
this.name = "Veggie Lovers Pizza";
}
public PizzaBuilder addCheese() {
// veggie lovers like parm
this.toppings.add("parmesan");
return this;
}
public PizzaBuilder addSauce() {
this.toppings.add("sauce");
return this;
}
public PizzaBuilder addTomatoes() {
this.toppings.add("chopped tomatoes");
return this;
}
public PizzaBuilder addGarlic() {
this.toppings.add("garlic");
return this;
}
public PizzaBuilder addOlives() {
this.toppings.add("green olives");
return this;
}
public PizzaBuilder addSpinach() {
this.toppings.add("spinach");
return this;
}
public PizzaBuilder addPepperoni() {
// never EVER add Pepperoni to veggie lovers pizza
return this;
}
public PizzaBuilder addSausage() {
// never EVER add Sausage to veggie lovers pizza
return this;
}

}
Loading

0 comments on commit e83cdfc

Please sign in to comment.