-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5cfa7e
commit 4445c2e
Showing
18 changed files
with
300 additions
and
194 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package patterns.decorator; | ||
|
||
|
||
public abstract class Coffee { | ||
|
||
String description = "Unknown Coffee"; | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public abstract double cost(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package patterns.decorator; | ||
|
||
public abstract class CoffeeDecorator extends Coffee { | ||
public abstract String getDescription(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package patterns.decorator; | ||
|
||
public class Espresso extends Coffee { | ||
|
||
public Espresso() { | ||
description = "Espresso"; | ||
} | ||
|
||
@Override | ||
public double cost() { | ||
return 1.99; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package patterns.decorator; | ||
|
||
public class WithMilk extends CoffeeDecorator { | ||
|
||
Coffee coffee; | ||
|
||
public WithMilk(Coffee coffee) { | ||
this.coffee = coffee; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return coffee.getDescription() + ", Milk"; | ||
} | ||
|
||
@Override | ||
public double cost() { | ||
return coffee.cost() + .50; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package patterns.decorator; | ||
|
||
public class WithSugar extends CoffeeDecorator { | ||
|
||
Coffee coffee; | ||
|
||
public WithSugar(Coffee coffee) { | ||
this.coffee = coffee; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return coffee.getDescription() + ", Sugar"; | ||
} | ||
|
||
@Override | ||
public double cost() { | ||
return coffee.cost() + .25; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package patterns.decorator.base; | ||
|
||
import patterns.decorator.Coffee; | ||
import patterns.decorator.Espresso; | ||
import patterns.decorator.WithMilk; | ||
import patterns.decorator.WithSugar; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
Coffee espresso = new Espresso(); | ||
// printCoffee(espresso); | ||
|
||
espresso = new WithMilk(espresso); | ||
// printCoffee(espresso); | ||
|
||
espresso = new WithSugar(espresso); | ||
espresso = new WithSugar(espresso); | ||
printCoffee(espresso); | ||
|
||
} | ||
|
||
private static void printCoffee(Coffee c) { | ||
System.out.println("Cost: " + c.cost() + ", Description: " + c.getDescription()); | ||
} | ||
} |
52 changes: 26 additions & 26 deletions
52
observer/CurrentConditionsDisplay.java → ...ns/observer/CurrentConditionsDisplay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
package patterns.observer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CurrentConditionsDisplay implements Observer { | ||
|
||
private int temp; | ||
private int humidity; | ||
|
||
public CurrentConditionsDisplay(Subject weatherStation) { | ||
weatherStation.registerObserver(this); | ||
} | ||
|
||
@Override | ||
public void update(int temp, int humidity) { | ||
this.temp = temp; | ||
this.humidity = humidity; | ||
displayCurrent(); | ||
} | ||
|
||
public void displayCurrent() { | ||
System.out.println("Current Temperature: " + temp); | ||
System.out.println("Current Humidity: " + humidity); | ||
} | ||
} | ||
package patterns.observer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CurrentConditionsDisplay implements Observer { | ||
|
||
private int temp; | ||
private int humidity; | ||
|
||
public CurrentConditionsDisplay(Subject weatherStation) { | ||
weatherStation.registerObserver(this); | ||
} | ||
|
||
@Override | ||
public void update(int temp, int humidity) { | ||
this.temp = temp; | ||
this.humidity = humidity; | ||
displayCurrent(); | ||
} | ||
|
||
public void displayCurrent() { | ||
System.out.println("Current Temperature: " + temp); | ||
System.out.println("Current Humidity: " + humidity); | ||
} | ||
} |
64 changes: 32 additions & 32 deletions
64
observer/ForecastDisplay.java → patterns/observer/ForecastDisplay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
package patterns.observer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ForecastDisplay implements Observer { | ||
|
||
private List<Integer> tempHistory; | ||
private List<Integer> humidityHistory; | ||
|
||
public ForecastDisplay(Subject weatherStation) { | ||
tempHistory = new ArrayList<>(); | ||
humidityHistory = new ArrayList<>(); | ||
weatherStation.registerObserver(this); | ||
} | ||
|
||
@Override | ||
public void update(int temp, int humidity) { | ||
this.tempHistory.add(temp); | ||
this.humidityHistory.add(temp); | ||
display7DayHistory(); | ||
} | ||
|
||
public void display7DayHistory() { | ||
//Print Last 7 days History of Temperature and Humidity | ||
System.out.println("Temperature History: " + | ||
tempHistory.subList(Math.max(tempHistory.size() - 7, 0), tempHistory.size())); | ||
System.out.println("Humidity History: " + | ||
humidityHistory.subList(Math.max(humidityHistory.size() - 7, 0), humidityHistory.size())); | ||
|
||
} | ||
} | ||
package patterns.observer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ForecastDisplay implements Observer { | ||
|
||
private List<Integer> tempHistory; | ||
private List<Integer> humidityHistory; | ||
|
||
public ForecastDisplay(Subject weatherStation) { | ||
tempHistory = new ArrayList<>(); | ||
humidityHistory = new ArrayList<>(); | ||
weatherStation.registerObserver(this); | ||
} | ||
|
||
@Override | ||
public void update(int temp, int humidity) { | ||
this.tempHistory.add(temp); | ||
this.humidityHistory.add(temp); | ||
display7DayHistory(); | ||
} | ||
|
||
public void display7DayHistory() { | ||
//Print Last 7 days History of Temperature and Humidity | ||
System.out.println("Temperature History: " + | ||
tempHistory.subList(Math.max(tempHistory.size() - 7, 0), tempHistory.size())); | ||
System.out.println("Humidity History: " + | ||
humidityHistory.subList(Math.max(humidityHistory.size() - 7, 0), humidityHistory.size())); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
package patterns.observer; | ||
|
||
import java.util.Random; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
//Create the data object (publisher / topic) | ||
WeatherStation weatherStation = new WeatherStation(); | ||
|
||
//Create and register our displays (observers / subscribers) | ||
CurrentConditionsDisplay currentConditionsDisplay = | ||
new CurrentConditionsDisplay(weatherStation); | ||
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherStation); | ||
|
||
//Simulate updates | ||
for (int i = 0; i < 5; i++) { | ||
System.out.println("\n--- Update " + i + " ---"); | ||
|
||
int randomTemp = getRandomint(-50, 40); | ||
int randomHumidity = getRandomint(0, 100); | ||
|
||
weatherStation.measurementsChanged(randomTemp, randomHumidity); | ||
|
||
Thread.sleep(1000); | ||
} | ||
} | ||
|
||
private static int getRandomint(int min, int max) { | ||
Random rand = new Random(); | ||
return rand.nextInt(max + 1 - min) + min; | ||
} | ||
} | ||
package patterns.observer; | ||
|
||
import java.util.Random; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
//Create the data object (publisher / topic) | ||
WeatherStation weatherStation = new WeatherStation(); | ||
|
||
//Create and register our displays (observers / subscribers) | ||
CurrentConditionsDisplay currentConditionsDisplay = | ||
new CurrentConditionsDisplay(weatherStation); | ||
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherStation); | ||
|
||
//Simulate updates | ||
for (int i = 0; i < 5; i++) { | ||
System.out.println("\n--- Update " + i + " ---"); | ||
|
||
int randomTemp = getRandomint(-50, 40); | ||
int randomHumidity = getRandomint(0, 100); | ||
|
||
weatherStation.measurementsChanged(randomTemp, randomHumidity); | ||
|
||
Thread.sleep(1000); | ||
} | ||
} | ||
|
||
private static int getRandomint(int min, int max) { | ||
Random rand = new Random(); | ||
return rand.nextInt(max + 1 - min) + min; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package patterns.observer; | ||
|
||
public interface Observer { | ||
public void update(int temp, int humidity); | ||
} | ||
package patterns.observer; | ||
|
||
public interface Observer { | ||
public void update(int temp, int humidity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package patterns.observer; | ||
|
||
public interface Subject { | ||
public void registerObserver(Observer o); | ||
public void removeObserver(Observer o); | ||
public void notifyObservers(); | ||
} | ||
package patterns.observer; | ||
|
||
public interface Subject { | ||
public void registerObserver(Observer o); | ||
public void removeObserver(Observer o); | ||
public void notifyObservers(); | ||
} |
Oops, something went wrong.