Last active
December 6, 2017 15:20
-
-
Save Vitalik549/dca035c0a527385dd1ab79fb3c40be01 to your computer and use it in GitHub Desktop.
java observer pattern (listener)
This file contains hidden or 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
| Observer pattern is used when there is one-to-many relationship between objects | |
| such as if one object is modified, its depenedent objects are to be notified automatically. | |
| Observer pattern falls under behavioral pattern category. | |
This file contains hidden or 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
| public class Application implements ComputerListener { | |
| private static int counter = 0; | |
| private int id; | |
| Application(){ id = ++counter; } | |
| @Override | |
| public void onStart() { | |
| System.out.println("app " + id + " launched..."); | |
| } | |
| @Override | |
| public void onStop() { | |
| System.out.println("app " + id + " saving before comp down..."); | |
| } | |
| } |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class Computer { | |
| List<ComputerListener> listeners = new ArrayList<>(); | |
| void start(){ | |
| System.out.println("started..."); | |
| listeners.forEach(ComputerListener::onStart); | |
| } | |
| void stop(){ | |
| System.out.println("comp is going to stop, notifying everyone!"); | |
| listeners.forEach(ComputerListener::onStop); | |
| System.out.println("computer stopped!"); | |
| } | |
| } |
This file contains hidden or 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
| public interface ComputerListener { | |
| void onStart(); | |
| void onStop(); | |
| } |
This file contains hidden or 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
| public class Main { | |
| public static void main(String[] args) { | |
| Computer computer = new Computer(); | |
| computer.listeners.add(new Application()); | |
| computer.listeners.add(new Application()); | |
| computer.start(); | |
| computer.stop(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment