观察者模式 java/kotlin/javascript

观察者模式,说白了就是一种消息订阅机制,解决主体对象和观察者之间的耦合。当面对一对多这种情况的时候,一个对象状态改变会让依赖于它的多个对象收到通知进行改变。而我们要做的实际上就是将它们进行绑定然后在对象改变时候调用对象的遍历函数依次便利,从而改变对象状态,达到目的。

我们在借鉴其他博客后给出了java的写法

抽象的观察者接口

Observe.java
public interface Observe {
    void update(String message);
}

抽象的被观察者接口

observerable.java
public interface Observerable {
    void registerObserver(Observe o);
    void removeObserver(Observe o);
    void notifyObserver();
}

被观察者,用来进行观察者的绑定解绑以及消息的更新

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

public class ServerPackage implements Observerable {

    private List list;
    private String message;

    public ServerPackage() {
        list = new ArrayList();
    }

    @Override
    public void registerObserver(Observe o) {
        list.add(o);
    }

    @Override
    public void removeObserver(Observe o) {
        if (!list.isEmpty()) {
            list.remove(o);
        }
    }

    @Override
    public void notifyObserver() {
        for(int i = 0; i < list.size(); i++) {
            Observe observer = list.get(i);
            observer.update(message);
        }
    }

    public void setInformation(String s) {
        this.message = s;
        System.out.println("消息更新: " + s);
        notifyObserver();
    }
}

观察者

User.java
public class User implements Observe{

    private String name;
    private String message;

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

    @Override
    public void update(String message) {
        this.message = message;
        read();
    }

    public void read() {
        System.out.println(name + "收到消息 " + message);
    }
}

测试类

Test.java
public class Test {
    public static void main(String[] args) {
        ServerPackage server = new ServerPackage();

        Observe userZhang = new User("ZhangSan");
        Observe userLi = new User("LiSi");
        Observe userWang = new User("WangWu");

        server.registerObserver(userZhang);
        server.registerObserver(userLi);
        server.registerObserver(userWang);
        server.setInformation("PHP是世界上最好用的语言!");

        System.out.println("----------------------------------------------");
        server.removeObserver(userZhang);
        server.setInformation("JAVA是世界上最好用的语言!");
    }
}

输出结果


输出结果

现在我们用kotlin写一下观察者模式

Observe.kt
interface Observe {
    fun update(message: String)
}
Observerable.kt
interface Observerable {
    fun registerObserver(o: Observe)
    fun removeObserver(o: Observe)
    fun notifyObserver()
}
ObserverPackage.kt
class ObserverPackage: Observerable {
    private var list: MutableList = ArrayList()
    var message: String? = null

    override fun registerObserver(o: Observe) {
        list.add(o)
    }

    override fun removeObserver(o: Observe) {
        if (!list.isEmpty()) {
            list.remove(o)
        }
    }

    override fun notifyObserver() {
        for (i in list.indices) {
            val observer = list[i]
            observer.update(message!!)
        }
    }

    fun setInformation(message: String) {
        this.message = message
        println("消息更新: $message")
        notifyObserver()
    }
}
User.kt
class User(private val name: String): Observe {
    private var message: String? = null
    override fun update(message: String) {
        this.message = message
        read()
    }

    fun read() {
        println("${name}收到消息:$message")
    }
}
Test.kt
fun main() {
    val server = ObserverPackage()
    val userZhang = User("张")
    val userLee = User("李")
    val userWang = User("王")

 server.registerObserver(userZhang)
    server.registerObserver(userLee)
    server.registerObserver(userWang)
        server.setInformation("PHP是世界上最好用的语言!")

    println("----------------------------------------------")
    server.removeObserver(userZhang)
        server.setInformation("JAVA是世界上最好用的语言!")

}

你可能感兴趣的:(观察者模式 java/kotlin/javascript)