✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page

Was this helpful?

  1. Software Design
  2. Design Patterns
  3. Behavioral Design Pattern

Observer DP

  • Just like subscriber subscribed to a channel on YouTube

  • Channel → Subject || Subscriber → Observer

  • Observer observes a Subject

Subscriber

package org.example;

public class Subscriber {

    private String name;
    private Channel channel = new Channel();

    public Subscriber(String name) {
        this.name = name;
    }

    public void update() {
        System.out.println("video uploaded");
    }

    public void subscriberChannel(Channel ch) {
        this.channel = ch;
    }

}

Subject

package org.example;

import java.util.ArrayList;
import java.util.List;

public class Channel {

    List<Subscriber> subsLst = new ArrayList<>();
    private String title;

    // add
    public void subscibe(Subscriber sub) {
        subsLst.add(sub);
    }

    public void unsubscriber(Subscriber sub) {
        subsLst.remove(sub);
    }

    public void notifySubscribers() {
        for (Subscriber sub : subsLst) {
            sub.update();
        }
    }

    public void upload(String title) {
        this.title = title;
        notifySubscribers();
    }

}

Main Code :

Channelch1 = new Channel();

Subscribers1 = new Subscriber("sub 1");
Subscribers2 = new Subscriber("sub 2");
Subscribers3 = new Subscriber("sub 3");

ch1.subscibe(s1);
ch1.subscibe(s2);
ch1.subscibe(s3);

ch1.notifySubscribers();
PreviousBehavioral Design PatternNextStructural Design Pattern

Last updated 2 years ago

Was this helpful?