Advertisement
ivorakitin

PublisherSubscriber

Feb 28th, 2025 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. /*
  2.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3.  */
  4. //https://medium.com/@smagid_allThings/uml-class-diagrams-tutorial-step-by-step-520fd83b300b
  5. package com.mycompany.publishsubscribe;
  6.  
  7. import java.util.*;
  8.  
  9. /**
  10.  *
  11.  * @author IvoRakitin
  12.  */
  13. public class PublishSubscribe {
  14.  
  15.     public static void main(String[] args) {
  16.        
  17.         Publisher publisher = new Publisher("p1");
  18.         Subscriber s1 = new Subscriber("s1");
  19.         s1.subscribe(publisher);
  20.         Subscriber s2 = new Subscriber("s2");
  21.         s2.subscribe(publisher);
  22.         Subscriber s3 = new Subscriber("s3");
  23.         s3.subscribe(publisher);
  24.         publisher.doWork();
  25.         s2.unsubscribe(publisher);
  26.         System.out.println();
  27.         publisher.doWork();
  28.         /*
  29.         int numSubs = 10;
  30.         Publisher p1 = new Publisher("p1");
  31.         Publisher p2 = new Publisher("p2");
  32.         Subscriber[] s = new Subscriber[numSubs];
  33.         for (int i = 0; i < s.length; i++) {
  34.             s[i] = new Subscriber("s" + i);
  35.             s[i].subscribe(p1);
  36.             if (i % 2 == 0)
  37.                 s[i].subscribe(p2);
  38.         }
  39.         p1.doWork();
  40.         p2.doWork();
  41.         for (int i = 0; i < s.length; i++) {
  42.             if (i % 2 != 0)
  43.                 s[i].unsubscribe(p1);
  44.         }
  45.         System.out.println("");
  46.         p1.doWork();
  47.         p2.doWork();   */  
  48.     }
  49. }
  50.  
  51. class Publisher {
  52.  
  53.     private List<Calling> subscribers;
  54.     private String name;
  55.  
  56.     public Publisher(String name) {
  57.         subscribers = new ArrayList<>();
  58.         this.name = name;
  59.     }
  60.  
  61.     public interface Calling {
  62.         public void call(Object state, String n);
  63.     }
  64.  
  65.     public void subscribe(Calling sub) {
  66.         subscribers.add(sub);
  67.     }
  68.  
  69.     public void unsubscribe(Calling subscriber) {
  70.         subscribers.remove(subscriber);
  71.     }
  72.  
  73.     public void doWork() {
  74.         for (int i = 0; i < 100; i++) {
  75.             if (i == 50) {
  76.                 for (Calling subscriber : subscribers) {
  77.                     subscriber.call(i, name);
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. class Subscriber {
  85.  
  86.     private String name;
  87.     private Publisher.Calling calling;
  88.  
  89.     public Subscriber(String name) {
  90.         this.name = name;
  91.     }
  92.  
  93.     public void subscribe(Publisher publisher) {
  94.         if (calling == null) {
  95.  
  96.             /*
  97.             calling = (Object state) -> {
  98.                 stateChanged(state);
  99.             };*/
  100.             calling = new Publisher.Calling() {
  101.                 @Override
  102.                 public void call(Object state, String n) {
  103.                     stateChanged(state, n);
  104.                 }
  105.             };
  106.         }
  107.         publisher.subscribe(calling);
  108.     }
  109.     public void unsubscribe(Publisher publisher) {
  110.         if (calling != null) {
  111.             publisher.unsubscribe(calling);
  112.         }
  113.     }
  114.     private void stateChanged(Object state, String n) {
  115.         System.out.println("Subscriber " + name + " called with state " + state + " from Publisher " + n);
  116.     }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement