Merge pull request #280 from ankurkaushal/master
Reformat Code According to Google Style Guide #224
This commit is contained in:
commit
7d3483daad
@ -6,45 +6,44 @@ import com.iluwatar.observer.generic.GWeather;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The Observer pattern is a software design pattern in which an object, called
|
* The Observer pattern is a software design pattern in which an object, called the subject,
|
||||||
* the subject, maintains a list of its dependents, called observers, and notifies
|
* maintains a list of its dependents, called observers, and notifies them automatically of any
|
||||||
* them automatically of any state changes, usually by calling one of their methods.
|
* state changes, usually by calling one of their methods. It is mainly used to implement
|
||||||
* It is mainly used to implement distributed event handling systems. The Observer
|
* distributed event handling systems. The Observer pattern is also a key part in the familiar
|
||||||
* pattern is also a key part in the familiar model–view–controller (MVC) architectural
|
* model–view–controller (MVC) architectural pattern. The Observer pattern is implemented in
|
||||||
* pattern. The Observer pattern is implemented in numerous programming libraries and
|
* numerous programming libraries and systems, including almost all GUI toolkits.
|
||||||
* systems, including almost all GUI toolkits.
|
|
||||||
* <p>
|
* <p>
|
||||||
* In this example {@link Weather} has a state that can be observed. The {@link Orcs}
|
* In this example {@link Weather} has a state that can be observed. The {@link Orcs} and
|
||||||
* and {@link Hobbits} register as observers and receive notifications when the
|
* {@link Hobbits} register as observers and receive notifications when the {@link Weather} changes.
|
||||||
* {@link Weather} changes.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
* @param args command line args
|
*
|
||||||
*/
|
* @param args command line args
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Weather weather = new Weather();
|
Weather weather = new Weather();
|
||||||
weather.addObserver(new Orcs());
|
weather.addObserver(new Orcs());
|
||||||
weather.addObserver(new Hobbits());
|
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
|
// Generic observer inspired by Java Generics and Collection by Naftalin & Wadler
|
||||||
System.out.println("\n--Running generic version--");
|
System.out.println("\n--Running generic version--");
|
||||||
GWeather gWeather = new GWeather();
|
GWeather gWeather = new GWeather();
|
||||||
gWeather.addObserver(new GOrcs());
|
gWeather.addObserver(new GOrcs());
|
||||||
gWeather.addObserver(new GHobbits());
|
gWeather.addObserver(new GHobbits());
|
||||||
|
|
||||||
gWeather.timePasses();
|
gWeather.timePasses();
|
||||||
gWeather.timePasses();
|
gWeather.timePasses();
|
||||||
gWeather.timePasses();
|
gWeather.timePasses();
|
||||||
gWeather.timePasses();
|
gWeather.timePasses();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,24 +7,23 @@ package com.iluwatar.observer;
|
|||||||
*/
|
*/
|
||||||
public class Hobbits implements WeatherObserver {
|
public class Hobbits implements WeatherObserver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(WeatherType currentWeather) {
|
public void update(WeatherType currentWeather) {
|
||||||
switch (currentWeather) {
|
switch (currentWeather) {
|
||||||
case COLD:
|
case COLD:
|
||||||
System.out.println("The hobbits are shivering in the cold weather.");
|
System.out.println("The hobbits are shivering in the cold weather.");
|
||||||
break;
|
break;
|
||||||
case RAINY:
|
case RAINY:
|
||||||
System.out.println("The hobbits look for cover from the rain.");
|
System.out.println("The hobbits look for cover from the rain.");
|
||||||
break;
|
break;
|
||||||
case SUNNY:
|
case SUNNY:
|
||||||
System.out.println("The happy hobbits bade in the warm sun.");
|
System.out.println("The happy hobbits bade in the warm sun.");
|
||||||
break;
|
break;
|
||||||
case WINDY:
|
case WINDY:
|
||||||
System.out.println("The hobbits hold their hats tightly in the windy weather.");
|
System.out.println("The hobbits hold their hats tightly in the windy weather.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,24 +7,23 @@ package com.iluwatar.observer;
|
|||||||
*/
|
*/
|
||||||
public class Orcs implements WeatherObserver {
|
public class Orcs implements WeatherObserver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(WeatherType currentWeather) {
|
public void update(WeatherType currentWeather) {
|
||||||
switch (currentWeather) {
|
switch (currentWeather) {
|
||||||
case COLD:
|
case COLD:
|
||||||
System.out.println("The orcs are freezing cold.");
|
System.out.println("The orcs are freezing cold.");
|
||||||
break;
|
break;
|
||||||
case RAINY:
|
case RAINY:
|
||||||
System.out.println("The orcs are dripping wet.");
|
System.out.println("The orcs are dripping wet.");
|
||||||
break;
|
break;
|
||||||
case SUNNY:
|
case SUNNY:
|
||||||
System.out.println("The sun hurts the orcs' eyes.");
|
System.out.println("The sun hurts the orcs' eyes.");
|
||||||
break;
|
break;
|
||||||
case WINDY:
|
case WINDY:
|
||||||
System.out.println("The orc smell almost vanishes in the wind.");
|
System.out.println("The orc smell almost vanishes in the wind.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,38 +5,38 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Weather can be observed by implementing {@link WeatherObserver} interface and
|
* Weather can be observed by implementing {@link WeatherObserver} interface and registering as
|
||||||
* registering as listener.
|
* listener.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Weather {
|
public class Weather {
|
||||||
|
|
||||||
private WeatherType currentWeather;
|
private WeatherType currentWeather;
|
||||||
private List<WeatherObserver> observers;
|
private List<WeatherObserver> observers;
|
||||||
|
|
||||||
public Weather() {
|
public Weather() {
|
||||||
observers = new ArrayList<>();
|
observers = new ArrayList<>();
|
||||||
currentWeather = WeatherType.SUNNY;
|
currentWeather = WeatherType.SUNNY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addObserver(WeatherObserver obs) {
|
public void addObserver(WeatherObserver obs) {
|
||||||
observers.add(obs);
|
observers.add(obs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeObserver(WeatherObserver obs) {
|
public void removeObserver(WeatherObserver obs) {
|
||||||
observers.remove(obs);
|
observers.remove(obs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void timePasses() {
|
public void timePasses() {
|
||||||
WeatherType[] enumValues = WeatherType.values();
|
WeatherType[] enumValues = WeatherType.values();
|
||||||
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
|
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
|
||||||
System.out.println("The weather changed to " + currentWeather + ".");
|
System.out.println("The weather changed to " + currentWeather + ".");
|
||||||
notifyObservers();
|
notifyObservers();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notifyObservers() {
|
private void notifyObservers() {
|
||||||
for (WeatherObserver obs : observers) {
|
for (WeatherObserver obs : observers) {
|
||||||
obs.update(currentWeather);
|
obs.update(currentWeather);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,6 @@ package com.iluwatar.observer;
|
|||||||
*/
|
*/
|
||||||
public interface WeatherObserver {
|
public interface WeatherObserver {
|
||||||
|
|
||||||
void update(WeatherType currentWeather);
|
void update(WeatherType currentWeather);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,10 @@ package com.iluwatar.observer;
|
|||||||
*/
|
*/
|
||||||
public enum WeatherType {
|
public enum WeatherType {
|
||||||
|
|
||||||
SUNNY, RAINY, WINDY, COLD;
|
SUNNY, RAINY, WINDY, COLD;
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.name().toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.name().toLowerCase();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,23 +8,23 @@ import com.iluwatar.observer.WeatherType;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GHobbits implements Race {
|
public class GHobbits implements Race {
|
||||||
@Override
|
@Override
|
||||||
public void update(GWeather weather, WeatherType weatherType) {
|
public void update(GWeather weather, WeatherType weatherType) {
|
||||||
switch (weatherType) {
|
switch (weatherType) {
|
||||||
case COLD:
|
case COLD:
|
||||||
System.out.println("The hobbits are shivering in the cold weather.");
|
System.out.println("The hobbits are shivering in the cold weather.");
|
||||||
break;
|
break;
|
||||||
case RAINY:
|
case RAINY:
|
||||||
System.out.println("The hobbits look for cover from the rain.");
|
System.out.println("The hobbits look for cover from the rain.");
|
||||||
break;
|
break;
|
||||||
case SUNNY:
|
case SUNNY:
|
||||||
System.out.println("The happy hobbits bade in the warm sun.");
|
System.out.println("The happy hobbits bade in the warm sun.");
|
||||||
break;
|
break;
|
||||||
case WINDY:
|
case WINDY:
|
||||||
System.out.println("The hobbits hold their hats tightly in the windy weather.");
|
System.out.println("The hobbits hold their hats tightly in the windy weather.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,24 +8,24 @@ import com.iluwatar.observer.WeatherType;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GOrcs implements Race {
|
public class GOrcs implements Race {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(GWeather weather, WeatherType weatherType) {
|
public void update(GWeather weather, WeatherType weatherType) {
|
||||||
switch (weatherType) {
|
switch (weatherType) {
|
||||||
case COLD:
|
case COLD:
|
||||||
System.out.println("The orcs are freezing cold.");
|
System.out.println("The orcs are freezing cold.");
|
||||||
break;
|
break;
|
||||||
case RAINY:
|
case RAINY:
|
||||||
System.out.println("The orcs are dripping wet.");
|
System.out.println("The orcs are dripping wet.");
|
||||||
break;
|
break;
|
||||||
case SUNNY:
|
case SUNNY:
|
||||||
System.out.println("The sun hurts the orcs' eyes.");
|
System.out.println("The sun hurts the orcs' eyes.");
|
||||||
break;
|
break;
|
||||||
case WINDY:
|
case WINDY:
|
||||||
System.out.println("The orc smell almost vanishes in the wind.");
|
System.out.println("The orc smell almost vanishes in the wind.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,16 +9,16 @@ import com.iluwatar.observer.WeatherType;
|
|||||||
*/
|
*/
|
||||||
public class GWeather extends Observable<GWeather, Race, WeatherType> {
|
public class GWeather extends Observable<GWeather, Race, WeatherType> {
|
||||||
|
|
||||||
private WeatherType currentWeather;
|
private WeatherType currentWeather;
|
||||||
|
|
||||||
public GWeather() {
|
public GWeather() {
|
||||||
currentWeather = WeatherType.SUNNY;
|
currentWeather = WeatherType.SUNNY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void timePasses() {
|
public void timePasses() {
|
||||||
WeatherType[] enumValues = WeatherType.values();
|
WeatherType[] enumValues = WeatherType.values();
|
||||||
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
|
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
|
||||||
System.out.println("The weather changed to " + currentWeather + ".");
|
System.out.println("The weather changed to " + currentWeather + ".");
|
||||||
notifyObservers(currentWeather);
|
notifyObservers(currentWeather);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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> {
|
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() {
|
public Observable() {
|
||||||
this.observers = new CopyOnWriteArrayList<>();
|
this.observers = new CopyOnWriteArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addObserver(O observer) {
|
public void addObserver(O observer) {
|
||||||
this.observers.add(observer);
|
this.observers.add(observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void notifyObservers(A argument) {
|
public void notifyObservers(A argument) {
|
||||||
for (O observer : observers) {
|
for (O observer : observers) {
|
||||||
observer.update((S) this, argument);
|
observer.update((S) this, argument);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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> {
|
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);
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import com.iluwatar.observer.App;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user