Skip to content

Commit

Permalink
Adding decorator pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
awssimplified committed Oct 31, 2020
1 parent d5cfa7e commit 4445c2e
Show file tree
Hide file tree
Showing 18 changed files with 300 additions and 194 deletions.
8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions patterns/decorator/Coffee.java
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();
}
5 changes: 5 additions & 0 deletions patterns/decorator/CoffeeDecorator.java
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();
}
14 changes: 14 additions & 0 deletions patterns/decorator/Espresso.java
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;
}

}
20 changes: 20 additions & 0 deletions patterns/decorator/WithMilk.java
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;
}
}
20 changes: 20 additions & 0 deletions patterns/decorator/WithSugar.java
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;
}
}
26 changes: 26 additions & 0 deletions patterns/decorator/base/Main.java
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());
}
}
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);
}
}
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()));

}
}
66 changes: 33 additions & 33 deletions observer/Main.java → patterns/observer/Main.java
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;
}
}
10 changes: 5 additions & 5 deletions observer/Observer.java → patterns/observer/Observer.java
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);
}
14 changes: 7 additions & 7 deletions observer/Subject.java → patterns/observer/Subject.java
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();
}
Loading

0 comments on commit 4445c2e

Please sign in to comment.