Recently, I've been experimenting with working with different terminal types for my project's JavaFX application.
Whenever the application is trying to print something, terminals from both the system terminal, the application terminal view, and other future views should be able to see the String.
We have now determined that the observer pattern is the best way to solve this problem.
This pattern is a pattern in which the observer object receives a notification of the state change from the object to be observed and performs processing that embodies it.
Describe the coupling between the object and the observer.
Supports broadcast type communication.
When it is necessary to reflect changes in the state of an object to another object while keeping the objects tightly coupled.
When it is necessary to add a new observer with minimal changes in the future.
All child nodes of this class can add, remove, and "(classes that implement Printable)" valid terminal types.
PrintObserver.java
import java.util.ArrayList;
public abstract class PrintObserver {
private final ArrayList<Printable> printerList = new ArrayList<>();
public void notifyAllPrinters(String toPrint) {
if(!toPrint.equals("")) {
for(Printable printer : printerList) {
System.out.println("\n");
printer.print(toPrint);
}
} else {
System.out.println("Quitting! Thank you!");
}
}
public void addPrinter(Printable printer) {
this.printerList.add(printer);
}
public void removePrinter(Printable printer) {
this.printerList.remove(printer);
}
}
All classes that implement this interface are observers, waiting for something to print.
Printable.java
public interface Printable {
void print(String toPrint);
}
It implements Printable, so if a child node of PrintObserver adds this Printable and calls a function called notifyAllObservers, it will be printed.
** Each Printable contains its own print implementation that is independent of the other Printables. ** **
PrintTerminalA.java
public class PrintTerminalA implements Printable {
@Override
public void print(String toPrint) {
System.out.println("This is terminal A");
System.out.println("It seems that someone has printed this message: "+toPrint);
}
}
Piikun.java
public class Piikun implements Printable
{
@Override
public void print(String toPrint) {
System.out.println("Hello! This is Pee!");
System.out.println("Thank you for sending me what to print. The message is here:");
System.out.println(toPrint);
}
}
Contains the main method. Add a sample Printable to the list and send user input to all Printable classes.
PrintSource.java
import java.util.Scanner;
public class PrintSource extends PrintObserver {
public PrintSource() {
addPrinter(new PrintTerminalA());
addPrinter(new Piikun());
String toPrint = "~";
while(!toPrint.equals("")) {
System.out.print("\n\nEnter String to print: ");
Scanner sc = new Scanner(System.in);
toPrint = sc.nextLine();
notifyAllPrinters(toPrint);
}
}
public static void main(String[] args) {
PrintSource printSource = new PrintSource();
}
}
[Github:] https://github.com/CoralBeef-zz/PrintObserverExample
Recommended Posts