Merge pull request #280 from ankurkaushal/master

Reformat Code According to Google Style Guide #224
This commit is contained in:
Ilkka Seppälä 2015-11-01 17:57:14 +02:00
commit 7d3483daad
12 changed files with 165 additions and 169 deletions

View File

@ -6,45 +6,44 @@ import com.iluwatar.observer.generic.GWeather;
/**
*
* The Observer pattern is a software design pattern in which an object, called
* the subject, maintains a list of its dependents, called observers, and notifies
* them automatically of any state changes, usually by calling one of their methods.
* It is mainly used to implement distributed event handling systems. The Observer
* pattern is also a key part in the familiar modelviewcontroller (MVC) architectural
* pattern. The Observer pattern is implemented in numerous programming libraries and
* systems, including almost all GUI toolkits.
* The Observer pattern is a software design pattern in which an object, called the subject,
* maintains a list of its dependents, called observers, and notifies them automatically of any
* state changes, usually by calling one of their methods. It is mainly used to implement
* distributed event handling systems. The Observer pattern is also a key part in the familiar
* modelviewcontroller (MVC) architectural pattern. The Observer pattern is implemented in
* numerous programming libraries and systems, including almost all GUI toolkits.
* <p>
* In this example {@link Weather} has a state that can be observed. The {@link Orcs}
* and {@link Hobbits} register as observers and receive notifications when the
* {@link Weather} changes.
* In this example {@link Weather} has a state that can be observed. The {@link Orcs} and
* {@link Hobbits} register as observers and receive notifications when the {@link Weather} changes.
*
*/
public class App {
/**
* Program entry point
* @param args command line args
*/
public static void main(String[] args) {
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
Weather weather = new Weather();
weather.addObserver(new Orcs());
weather.addObserver(new Hobbits());
Weather weather = new Weather();
weather.addObserver(new Orcs());
weather.addObserver(new Hobbits());
weather.timePasses();
weather.timePasses();
weather.timePasses();
weather.timePasses();
weather.timePasses();
weather.timePasses();
weather.timePasses();
weather.timePasses();
// Generic observer inspired by Java Generics and Collection by Naftalin & Wadler
System.out.println("\n--Running generic version--");
GWeather gWeather = new GWeather();
gWeather.addObserver(new GOrcs());
gWeather.addObserver(new GHobbits());
// Generic observer inspired by Java Generics and Collection by Naftalin & Wadler
System.out.println("\n--Running generic version--");
GWeather gWeather = new GWeather();
gWeather.addObserver(new GOrcs());
gWeather.addObserver(new GHobbits());
gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
}
gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
gWeather.timePasses();
}
}

View File

@ -7,24 +7,23 @@ package com.iluwatar.observer;
*/
public class Hobbits implements WeatherObserver {
@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
System.out.println("The hobbits are shivering in the cold weather.");
break;
case RAINY:
System.out.println("The hobbits look for cover from the rain.");
break;
case SUNNY:
System.out.println("The happy hobbits bade in the warm sun.");
break;
case WINDY:
System.out.println("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;
}
}
@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
System.out.println("The hobbits are shivering in the cold weather.");
break;
case RAINY:
System.out.println("The hobbits look for cover from the rain.");
break;
case SUNNY:
System.out.println("The happy hobbits bade in the warm sun.");
break;
case WINDY:
System.out.println("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;
}
}
}

View File

@ -7,24 +7,23 @@ package com.iluwatar.observer;
*/
public class Orcs implements WeatherObserver {
@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
}
@Override
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
}
}

View File

@ -5,38 +5,38 @@ import java.util.List;
/**
*
* Weather can be observed by implementing {@link WeatherObserver} interface and
* registering as listener.
* Weather can be observed by implementing {@link WeatherObserver} interface and registering as
* listener.
*
*/
public class Weather {
private WeatherType currentWeather;
private List<WeatherObserver> observers;
private WeatherType currentWeather;
private List<WeatherObserver> observers;
public Weather() {
observers = new ArrayList<>();
currentWeather = WeatherType.SUNNY;
}
public Weather() {
observers = new ArrayList<>();
currentWeather = WeatherType.SUNNY;
}
public void addObserver(WeatherObserver obs) {
observers.add(obs);
}
public void addObserver(WeatherObserver obs) {
observers.add(obs);
}
public void removeObserver(WeatherObserver obs) {
observers.remove(obs);
}
public void removeObserver(WeatherObserver obs) {
observers.remove(obs);
}
public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers();
}
public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers();
}
private void notifyObservers() {
for (WeatherObserver obs : observers) {
obs.update(currentWeather);
}
}
private void notifyObservers() {
for (WeatherObserver obs : observers) {
obs.update(currentWeather);
}
}
}

View File

@ -7,6 +7,6 @@ package com.iluwatar.observer;
*/
public interface WeatherObserver {
void update(WeatherType currentWeather);
void update(WeatherType currentWeather);
}

View File

@ -7,11 +7,10 @@ package com.iluwatar.observer;
*/
public enum WeatherType {
SUNNY, RAINY, WINDY, COLD;
@Override
public String toString() {
return this.name().toLowerCase();
}
SUNNY, RAINY, WINDY, COLD;
@Override
public String toString() {
return this.name().toLowerCase();
}
}

View File

@ -8,23 +8,23 @@ import com.iluwatar.observer.WeatherType;
*
*/
public class GHobbits implements Race {
@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
System.out.println("The hobbits are shivering in the cold weather.");
break;
case RAINY:
System.out.println("The hobbits look for cover from the rain.");
break;
case SUNNY:
System.out.println("The happy hobbits bade in the warm sun.");
break;
case WINDY:
System.out.println("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;
}
@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
System.out.println("The hobbits are shivering in the cold weather.");
break;
case RAINY:
System.out.println("The hobbits look for cover from the rain.");
break;
case SUNNY:
System.out.println("The happy hobbits bade in the warm sun.");
break;
case WINDY:
System.out.println("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;
}
}
}

View File

@ -8,24 +8,24 @@ import com.iluwatar.observer.WeatherType;
*
*/
public class GOrcs implements Race {
@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
@Override
public void update(GWeather weather, WeatherType weatherType) {
switch (weatherType) {
case COLD:
System.out.println("The orcs are freezing cold.");
break;
case RAINY:
System.out.println("The orcs are dripping wet.");
break;
case SUNNY:
System.out.println("The sun hurts the orcs' eyes.");
break;
case WINDY:
System.out.println("The orc smell almost vanishes in the wind.");
break;
default:
break;
}
}
}

View File

@ -9,16 +9,16 @@ import com.iluwatar.observer.WeatherType;
*/
public class GWeather extends Observable<GWeather, Race, WeatherType> {
private WeatherType currentWeather;
private WeatherType currentWeather;
public GWeather() {
currentWeather = WeatherType.SUNNY;
}
public GWeather() {
currentWeather = WeatherType.SUNNY;
}
public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers(currentWeather);
}
public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers(currentWeather);
}
}

View File

@ -12,20 +12,20 @@ import java.util.concurrent.CopyOnWriteArrayList;
*/
public abstract class Observable<S extends Observable<S, O, A>, O extends Observer<S, O, A>, A> {
protected List<O> observers;
protected List<O> observers;
public Observable() {
this.observers = new CopyOnWriteArrayList<>();
}
public Observable() {
this.observers = new CopyOnWriteArrayList<>();
}
public void addObserver(O observer) {
this.observers.add(observer);
}
public void addObserver(O observer) {
this.observers.add(observer);
}
@SuppressWarnings("unchecked")
public void notifyObservers(A argument) {
for (O observer : observers) {
observer.update((S) this, argument);
}
@SuppressWarnings("unchecked")
public void notifyObservers(A argument) {
for (O observer : observers) {
observer.update((S) this, argument);
}
}
}

View File

@ -10,5 +10,5 @@ package com.iluwatar.observer.generic;
*/
public interface Observer<S extends Observable<S, O, A>, O extends Observer<S, O, A>, A> {
void update(S subject, A argument);
void update(S subject, A argument);
}

View File

@ -11,9 +11,9 @@ import com.iluwatar.observer.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}