Skip to content

Commit

Permalink
updates for 2e
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Jul 13, 2020
1 parent 1eaf6bb commit 0162a60
Show file tree
Hide file tree
Showing 25 changed files with 286 additions and 114 deletions.
4 changes: 2 additions & 2 deletions src/headfirst/designpatterns/combined/djview/BeatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

public class BeatModel implements BeatModelInterface, MetaEventListener {
Sequencer sequencer;
ArrayList<BeatObserver> beatObservers = new ArrayList<BeatObserver>();
ArrayList<BPMObserver> bpmObservers = new ArrayList<BPMObserver>();
List<BeatObserver> beatObservers = new ArrayList<BeatObserver>();
List<BPMObserver> bpmObservers = new ArrayList<BPMObserver>();
int bpm = 90;
Sequence sequence;
Track track;
Expand Down
20 changes: 19 additions & 1 deletion src/headfirst/designpatterns/combined/djview/DJView.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,36 @@ public void createControls() {
menu = new JMenu("DJ Control");
startMenuItem = new JMenuItem("Start");
menu.add(startMenuItem);
startMenuItem.addActionListener((event) -> controller.start());
// was....
/*
startMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
controller.start();
}
});
*/
stopMenuItem = new JMenuItem("Stop");
menu.add(stopMenuItem);
stopMenuItem.addActionListener((event) -> controller.stop());
// was...
/*
stopMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
controller.stop();
}
});
*/
JMenuItem exit = new JMenuItem("Quit");
exit.addActionListener((event) -> System.exit(0));
// was...
/*
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
*/

menu.add(exit);
menuBar.add(menu);
Expand Down Expand Up @@ -137,7 +149,13 @@ public void disableStartMenuItem() {

public void actionPerformed(ActionEvent event) {
if (event.getSource() == setBPMButton) {
int bpm = Integer.parseInt(bpmTextField.getText());
int bpm = 90;
String bpmText = bpmTextField.getText();
if (bpmText == null || bpmText.contentEquals("")) {
bpm = 90;
} else {
bpm = Integer.parseInt(bpmTextField.getText());
}
controller.setBPM(bpm);
} else if (event.getSource() == increaseBPMButton) {
controller.increaseBPM();
Expand Down
101 changes: 101 additions & 0 deletions src/headfirst/designpatterns/combined/djview/DJViewHttpHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package headfirst.designpatterns.combined.djview;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.sun.net.httpserver.*;

// Controller for the DJ Web View
// Takes actions from the view and translates them to actions on the model
// View has no interaction to the model
// Using the exact same model, BeatModel, as we used with the DJView and BeatContoller
public class DJViewHttpHandler implements HttpHandler {
BeatModel beatModel;
Map<String, String> queryPairs;

public DJViewHttpHandler(BeatModel beatModel) {
this.beatModel = beatModel;
}
public void handle(HttpExchange httpExchange) throws IOException {
String uri = httpExchange.getRequestURI().toString();
System.out.println("URI: " + uri);
String[] params = uri.split("\\?");
String queryStrings = "";
if (params.length > 1) {
queryStrings = params[1];
}

boolean noAction = true;
try {
queryPairs = this.splitQuery(queryStrings);
noAction = false;
} catch (Exception e) {
System.out.println("Error splitting query: " + e.getMessage());
}
System.out.println("Query pairs: " + queryPairs);

if (!noAction) {
String bpm = this.getParameter("bpm");
if (bpm == null) {
bpm = beatModel.getBPM() + "";
}
String set = this.getParameter("set");
if (set != null) {
int bpmNumber = 90;
bpmNumber = Integer.parseInt(bpm);
beatModel.setBPM(bpmNumber);
}
String decrease = this.getParameter("decrease");
if (decrease != null) {
beatModel.setBPM(beatModel.getBPM() - 1);
}
String increase = this.getParameter("increase");
if (increase != null) {
beatModel.setBPM(beatModel.getBPM() + 1);
}
String on = this.getParameter("on");
if (on != null) {
beatModel.on();
}
String off = this.getParameter("off");
if (off != null) {
beatModel.off();
}
}

// allow access from web server other than 8080
Headers headers = httpExchange.getResponseHeaders();
headers.add("Access-Control-Allow-Origin","*");

OutputStream outputStream = httpExchange.getResponseBody();
StringBuilder responseStringBuilder = new StringBuilder();
responseStringBuilder.append(beatModel.getBPM());
System.out.println("BPM: " + beatModel.getBPM());

String responseString = responseStringBuilder.toString();
httpExchange.sendResponseHeaders(200, responseString.length());
outputStream.write(responseString.getBytes());
outputStream.flush();
outputStream.close();
}

Map<String, String> splitQuery(String query) throws UnsupportedEncodingException {
Map<String, String> query_pairs = new HashMap<String, String>();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
}
return query_pairs;
}

// bpm, set, decrease, increase, on, off
String getParameter(String param) {
return queryPairs.get(param);
}
}
26 changes: 26 additions & 0 deletions src/headfirst/designpatterns/combined/djview/DJViewHttpServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package headfirst.designpatterns.combined.djview;

import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;

public class DJViewHttpServer {

public static void main(String[] args) {

try {
System.out.println("DJView Http Server Running");

BeatModel beatModel = new BeatModel();
beatModel.initialize();
// create a server on port 8080, with a backlog queue allowed of size 0 (ie, no queue)
// most people will already have a web server running on 80, so we're using 8080
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 8080), 0);
server.createContext("/djview", new DJViewHttpHandler(beatModel));
server.start();
System.out.println("DJView Server is running at http://localhost:8080/djview");

} catch (Exception e) {
e.printStackTrace();
}
}
}
10 changes: 10 additions & 0 deletions src/headfirst/designpatterns/combined/djview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ folder:
13. Navigate to localhost:8080/djview/djview.jsp


NEW
Don't need tomcat.
Run DJViewHttpServer, this will run the server on port 8080.
CORS disabled so requests can come from different servers.
Client:
run HFDP/dj/dj.html from local web server (port 80 usually).





4 changes: 2 additions & 2 deletions src/headfirst/designpatterns/combining/observer/Flock.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package headfirst.designpatterns.combining.observer;

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

public class Flock implements Quackable {
ArrayList<Quackable> ducks = new ArrayList<Quackable>();
List<Quackable> ducks = new ArrayList<Quackable>();

public void add(Quackable duck) {
ducks.add(duck);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package headfirst.designpatterns.combining.observer;

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

public class Observable implements QuackObservable {
ArrayList<Observer> observers = new ArrayList<Observer>();
List<Observer> observers = new ArrayList<Observer>();
QuackObservable duck;

public Observable(QuackObservable duck) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package headfirst.designpatterns.command.diner;

public class BurgerAndFriesOrder implements Order {
Cook cook;
public BurgerAndFriesOrder(Cook cook) {
this.cook = cook;
}
public void orderUp() {
cook.makeBurger();
cook.makeFries();
}
}
16 changes: 5 additions & 11 deletions src/headfirst/designpatterns/facade/hometheater/Amplifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
public class Amplifier {
String description;
Tuner tuner;
DvdPlayer dvd;
CdPlayer cd;
StreamingPlayer player;

public Amplifier(String description) {
this.description = description;
Expand All @@ -31,18 +30,13 @@ public void setVolume(int level) {
}

public void setTuner(Tuner tuner) {
System.out.println(description + " setting tuner to " + dvd);
System.out.println(description + " setting tuner to " + player);
this.tuner = tuner;
}

public void setDvd(DvdPlayer dvd) {
System.out.println(description + " setting DVD player to " + dvd);
this.dvd = dvd;
}

public void setCd(CdPlayer cd) {
System.out.println(description + " setting CD player to " + cd);
this.cd = cd;
public void setStreamingPlayer(StreamingPlayer player) {
System.out.println(description + " setting Streaming player to " + player);
this.player = player;
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class HomeTheaterFacade {
Amplifier amp;
Tuner tuner;
DvdPlayer dvd;
StreamingPlayer player;
CdPlayer cd;
Projector projector;
TheaterLights lights;
Expand All @@ -12,17 +12,15 @@ public class HomeTheaterFacade {

public HomeTheaterFacade(Amplifier amp,
Tuner tuner,
DvdPlayer dvd,
CdPlayer cd,
StreamingPlayer player,
Projector projector,
Screen screen,
TheaterLights lights,
PopcornPopper popper) {

this.amp = amp;
this.tuner = tuner;
this.dvd = dvd;
this.cd = cd;
this.player = player;
this.projector = projector;
this.screen = screen;
this.lights = lights;
Expand All @@ -38,11 +36,11 @@ public void watchMovie(String movie) {
projector.on();
projector.wideScreenMode();
amp.on();
amp.setDvd(dvd);
amp.setStreamingPlayer(player);
amp.setSurroundSound();
amp.setVolume(5);
dvd.on();
dvd.play(movie);
player.on();
player.play(movie);
}


Expand All @@ -53,28 +51,8 @@ public void endMovie() {
screen.up();
projector.off();
amp.off();
dvd.stop();
dvd.eject();
dvd.off();
}

public void listenToCd(String cdTitle) {
System.out.println("Get ready for an audiopile experence...");
lights.on();
amp.on();
amp.setVolume(5);
amp.setCd(cd);
amp.setStereoSound();
cd.on();
cd.play(cdTitle);
}

public void endCd() {
System.out.println("Shutting down CD...");
amp.off();
amp.setCd(cd);
cd.eject();
cd.off();
player.stop();
player.off();
}

public void listenToRadio(double frequency) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ public class HomeTheaterTestDrive {
public static void main(String[] args) {
Amplifier amp = new Amplifier("Top-O-Line Amplifier");
Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp);
DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp);
StreamingPlayer player = new StreamingPlayer("Top-O-Line Streaming Player", amp);
CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp);
Projector projector = new Projector("Top-O-Line Projector", dvd);
Projector projector = new Projector("Top-O-Line Projector", player);
TheaterLights lights = new TheaterLights("Theater Ceiling Lights");
Screen screen = new Screen("Theater Screen");
PopcornPopper popper = new PopcornPopper("Popcorn Popper");

HomeTheaterFacade homeTheater =
new HomeTheaterFacade(amp, tuner, dvd, cd,
new HomeTheaterFacade(amp, tuner, player,
projector, screen, lights, popper);

homeTheater.watchMovie("Raiders of the Lost Ark");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public class Projector {
String description;
DvdPlayer dvdPlayer;
StreamingPlayer player;

public Projector(String description, DvdPlayer dvdPlayer) {
public Projector(String description, StreamingPlayer player) {
this.description = description;
this.dvdPlayer = dvdPlayer;
this.player = player;
}

public void on() {
Expand Down
Loading

0 comments on commit 0162a60

Please sign in to comment.