[JAVA] PrintObserver "Observer Design Pattern: Description and Implementation Example"

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.

What is Observer Pattern?

Observer = observer

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.

The advantages of using this pattern are:

  1. Describe the coupling between the object and the observer.

  2. Supports broadcast type communication.

how to use:

  1. When it is necessary to reflect changes in the state of an object to another object while keeping the objects tightly coupled.

  2. When it is necessary to add a new observer with minimal changes in the future.

Sample Print Observer

PrintObserverUML.png

role

PrintObserver << Abstract Class >>:

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);
    }
}

Printable << Interface >>:

All classes that implement this interface are observers, waiting for something to print.

Printable.java


public interface Printable {
    void print(String toPrint);
}

PrintTerminal A and Piikun:

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);
    }
    
}

PrintSource << Main >>:

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();
    }
}

Download this project on github:

[Github:] https://github.com/CoralBeef-zz/PrintObserverExample

Recommended Posts

PrintObserver "Observer Design Pattern: Description and Implementation Example"
Application example of design pattern (No. 1)
BloomFilter description and implementation sample (JAVA)
Observer Pattern
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
About synchronized and Reentrant Lock (Java & Kotlin implementation example)