找到你要的答案

Q:Java alternative Observer Pattern

Q:java设计模式的选择

In my Object Oriented Lecture at the University I encountered a strange Observer-Pattern which differs from the ones which I see online.

Instead of seperating the Observable and Observer in two different Objects there is one big Object which combines the two. Additionally, the Superobject got a method, which returns the observer (as a handle).

My lecturer told me that this way of implementing the pattern would solve an architectural issue which would cause the update-method to explode if we extend it. I think I get most of his intention.

My issue right now is, that I got no other source to learn about this 'alternative' Observer-Pattern, or at least I'm not able to find an article about the pattern. Though I think I get the point of my lecturer, I don't think I fully understand this patttern, i.e. the benefits of compress all classes together.

Can anybody elaborate, why this architecture would benefit my code? Or why can't I simply extract the logic of the update-method out into an utility class?

Below is the (java) class which we built. I deleted some of the unnecessary logic.

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.Semaphore;

public class Server implements InterfaceServer {
    public Client client;
    private final ObjServer oServer;

    public Server() {
        oServer = new ObjServer();
        new Thread(server).start();
    }

    public Client connect() throws InterruptedException {
        Client handle = new Client();
        oServer.addObserver(handle);
        return handle;
    }

    public class ObjServer extends Observable implements Runnable {

        @Override
        public void run() {
            while (true) {
                // pull data

                setChanged();
                notifyObservers(m);
            }
        }
    }

    public static class Client implements Observer {

        private Object obj;
        private Semaphore semaphore = new Semaphore(0, true);

        public Object getObject() throws InterruptedException {
            semaphore.acquire();
            System.out.println(obj);

            return obj;
        }

        @Override
        public void update(Observable o, Object arg) {
            obj = arg;
            semaphore.release();
        }

    }

    public static void main(String... args) throws Exception {
        Server server = new Server();

        Client client = new Client();

        client = server.connect();

        while (true) {
            client.getObject();
        }
    }
}

在我在大学的面向对象的演讲中,我遇到了一个奇怪的观察者模式,这与我在网上看到的不同。

而不是分离观察和观察者在两个不同的对象有一个大的对象相结合的两。此外,SuperObject有一个方法,它返回的观察者(如手柄)。

我的讲师告诉我,这种模式的实施将解决一个架构问题,这将导致更新方法爆炸,如果我们延长它。我想我得到了他的大部分意图。

我现在的问题是,我没有其他来源可以了解这种“替代”观察者模式,或者至少我不能找到一篇关于模式的文章。虽然我认为我得到我的老师的一点,我不认为我完全理解这一模式,即压缩所有一起上课的好处。

谁能解释一下,为什么这个架构会对我的代码有利?或者为什么我不能简单的将更新方法的逻辑提取到一个实用程序类中呢?

下面是(java)类,我们建立了。我删除了一些不必要的逻辑。

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.Semaphore;

public class Server implements InterfaceServer {
    public Client client;
    private final ObjServer oServer;

    public Server() {
        oServer = new ObjServer();
        new Thread(server).start();
    }

    public Client connect() throws InterruptedException {
        Client handle = new Client();
        oServer.addObserver(handle);
        return handle;
    }

    public class ObjServer extends Observable implements Runnable {

        @Override
        public void run() {
            while (true) {
                // pull data

                setChanged();
                notifyObservers(m);
            }
        }
    }

    public static class Client implements Observer {

        private Object obj;
        private Semaphore semaphore = new Semaphore(0, true);

        public Object getObject() throws InterruptedException {
            semaphore.acquire();
            System.out.println(obj);

            return obj;
        }

        @Override
        public void update(Observable o, Object arg) {
            obj = arg;
            semaphore.release();
        }

    }

    public static void main(String... args) throws Exception {
        Server server = new Server();

        Client client = new Client();

        client = server.connect();

        while (true) {
            client.getObject();
        }
    }
}
answer1: 回答1:

I don't see how this differs from the traditional observer pattern. You still have one observable object: ObjServer, and an observer object: Client. And the observable has a method to add observers, and it calls back the added observers.

How these objects are created and who creates them is irrelevant to the observer pattern.

Note that these lines:

Client client = new Client();
client = server.connect();

make little sense. Why do you create a new Client, and immediately throw it away and reassign the client returned by server.connect() to the variable? You could simply do

Client client = server.connect();

我不明白这和传统的观察者模式有什么不同。你还有一个观察对象:objserver,和观察者对象:客户。观察者有一个方法来添加观察者,它调用返回的观察者。

这些对象是如何创建的,谁创建它们与观察者模式无关。

注意这些行:

Client client = new Client();
client = server.connect();

没什么意义。你为什么要创建一个新的客户,并立即把它扔掉,重新分配客户端返回服务器。connect()的变量?你可以简单地做

Client client = server.connect();
java  observer-pattern