siteName > > observer
Threads
java ( 1609537 ) - com.iluwatar.ob ( 1611108 ) stack: com.iluwatar.observer.App.main(App.java:51) org.codehaus.mojo.exec.ExecJavaMojo.doMain(ExecJavaMojo.java:385) org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:374) org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:296) java.base/java.lang.Thread.run(Thread.java:840)
/*
 * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
 *
 * The MIT License
 * Copyright © 2014-2022 Ilkka Seppälä
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.iluwatar.observer;

import com.iluwatar.observer.generic.GenHobbits;
import com.iluwatar.observer.generic.GenOrcs;
import com.iluwatar.observer.generic.GenWeather;
import lombok.extern.slf4j.Slf4j;

/**
 * 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
 * model–view–controller (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.
 */
@Slf4j
public class App {

  /**
   * Program entry point.
   *
   * @param args command line args
   */
  public static void main(String[] args) {

    var weather = new Weather();
    weather.addObserver(new Orcs());
    weather.addObserver(new Hobbits());

    weather.timePasses();
    weather.timePasses();
    weather.timePasses();
    weather.timePasses();

    // Generic observer inspired by Java Generics and Collections by Naftalin & Wadler
    LOGGER.info("--Running generic version--");
    var genericWeather = new GenWeather();
    genericWeather.addObserver(new GenOrcs());
    genericWeather.addObserver(new GenHobbits());

    genericWeather.timePasses();
    genericWeather.timePasses();
    genericWeather.timePasses();
    genericWeather.timePasses();
  }
}
Variables All
No.FromNameValue
1class@51LOGGERLogger[com.iluwatar.observer.App]
251args[Ljava.lang.String;@242af014
END 0 00
Output All Filter Merge
Process FilterThread Filter
1609537 java 1611108 com.iluwatar.ob
No.PNPIDTIDTNTLMessage
1java16095371611108com.iluwatar.obcom.iluwatar.observer.WeatherIThe weather changed to rainy.
2java16095371611108com.iluwatar.obcom.iluwatar.observer.OrcsIThe orcs are facing Rainy weather now
3java16095371611108com.iluwatar.obcom.iluwatar.observer.HobbitsIThe hobbits are facing Rainy weather now
4java16095371611108com.iluwatar.obcom.iluwatar.observer.WeatherIThe weather changed to windy.
5java16095371611108com.iluwatar.obcom.iluwatar.observer.OrcsIThe orcs are facing Windy weather now
6java16095371611108com.iluwatar.obcom.iluwatar.observer.HobbitsIThe hobbits are facing Windy weather now
7java16095371611108com.iluwatar.obcom.iluwatar.observer.WeatherIThe weather changed to cold.
8java16095371611108com.iluwatar.obcom.iluwatar.observer.OrcsIThe orcs are facing Cold weather now
9java16095371611108com.iluwatar.obcom.iluwatar.observer.HobbitsIThe hobbits are facing Cold weather now
10java16095371611108com.iluwatar.obcom.iluwatar.observer.WeatherIThe weather changed to sunny.
11java16095371611108com.iluwatar.obcom.iluwatar.observer.OrcsIThe orcs are facing Sunny weather now
12java16095371611108com.iluwatar.obcom.iluwatar.observer.HobbitsIThe hobbits are facing Sunny weather now
13java16095371611108com.iluwatar.obcom.iluwatar.observer.AppI--Running generic version--
14java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenWeatherIThe weather changed to rainy.
15java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenOrcsIThe orcs are facing Rainy weather now
16java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenHobbitsIThe hobbits are facing Rainy weather now
17java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenWeatherIThe weather changed to windy.
18java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenOrcsIThe orcs are facing Windy weather now
19java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenHobbitsIThe hobbits are facing Windy weather now
20java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenWeatherIThe weather changed to cold.
21java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenOrcsIThe orcs are facing Cold weather now
22java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenHobbitsIThe hobbits are facing Cold weather now
23java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenWeatherIThe weather changed to sunny.
24java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenOrcsIThe orcs are facing Sunny weather now
25java16095371611108com.iluwatar.obcom.iluwatar.observer.generic.GenHobbitsIThe hobbits are facing Sunny weather now
END 0 0 0 0 0 00
Project:JavaDesignPatterns
Update:20240509
Commit:bf6456ba6
Source Code:observer
BuildTool:Java17
Compiler:Java17
Runtime:Openjdk17
System:MySystemD
Kernel:Linux5.10.211
Cpu:Intel:Corei7-7700K
Machine:AwesomeMachine