[GO] [Gang of Four] Design pattern learning --Observer

Observer-Observer

** Table of Contents ** This pattern is to let other objects monitoring it do something at the same time when the properties of one object are updated.

It's an image that notifies the monitored object that the updated object has been updated. This is the Live Data of Android Architecture Components.

Purpose

Define a one-to-many dependency between objects so that when an object changes state, all objects that depend on it are automatically notified and updated.

Component

ยท Subject Observed abstract class -Observer Abstract class you want to monitor -ConcreteSubject A monitored concrete class -ConcreteObserver The concrete class you want to monitor

Implementation

We will implement a sample program that is automatically updated when the application installed on the smartphone is updated. The play store is the subject and the smartphone is the observer. I don't implement an abstract class because it's annoying.

ConcreteSubject The concrete class to be monitored

It is a play store.

PlayStore.kt


package observer

class PlayStore {
    interface UpdateListener {
        fun update(latestVersion: Int)
    }

    private val listeners = ArrayList<UpdateListener>()

    fun update(latestVersion: Int) {
        println("The sample app${latestVersion}It has been updated to.")
        listeners.forEach {
            it.update(latestVersion)
        }
    }

    fun addListener(listener: UpdateListener) {
        listeners.add(listener)
    }
}

ConcreteObserver The concrete class you want to monitor

Implement two classes to observe so that you can easily feel the effect of this pattern.

First user who has the sample app installed

SnakeSmartPhone.kt


package observer

class SnakeSmartPhone(playStore: PlayStore) {
    init {
        playStore.addListener(object: PlayStore.UpdateListener {
            override fun update(latestVersion: Int) {
                println("Snake's smartphone app${latestVersion}The sample was updated to.")
            }
        })
    }
}

Second user who has the sample app installed

Raiden.kt


package observer

class RaidenSmartPhone(playStore: PlayStore) {
    init {
        playStore.addListener(object: PlayStore.UpdateListener {
            override fun update(latestVersion: Int) {
                println("Raiden's smartphone sample app${latestVersion}It has been updated to.")
            }
        })
    }
}

People who use

Client.kt


package observer

class Client {
    init {
        val playStore = PlayStore()
        SnakeSmartPhone(playStore)
        RaidenSmartPhone(playStore)

        playStore.update(1)

        playStore.update(2)
    }
}

Output result

[out-put]
The sample app has been updated to 1.
Snake's smartphone sample app has been updated to 1.
Raiden's smartphone sample app has been updated to 1.
The sample app has been updated to 2.
Snake's smartphone sample app has been updated to 2.
Raiden's smartphone sample app has been updated to 2.

Every time you update the play store app, the smartphone on which it is installed is automatically updated.

that's all

Recommended Posts

[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning
[Gang of Four] Design pattern learning --Singleton
[Gang of Four] Design Pattern Learning --Decorator
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Iterator
[Gang of Four] Design pattern learning --Facade
[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --Memento
[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Command
[Gang of Four] Design pattern learning --Abstract Factory
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Design Pattern #Observer
Learn the design pattern "Observer" in Python
Design Pattern #Builder
Design Pattern #Adapter
Design Pattern #Decorator
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Singleton
Design Pattern #Proxy
Learn the design pattern "Chain of Responsibility" in Python
Observer pattern in Java
Design Pattern #Factory Method
Deep learning 1 Practice of deep learning
Design Pattern #Template Method
Sample source of Observer pattern realized by Java, PHP, Python
GoF design pattern is just an "introduction of abstraction layer"