0

I have a question regard design of my little IRC Framework, just for learning JAVA event and observer..

First question: http://en.wikipedia.org/wiki/Observer_pattern#Critics

The Observer pattern is criticized[6] for being too verbose, introducing too many bugs and violating software engineering principles, such as not promoting side-effects, encapsulation, composability, separation of concepts, scalability, uniformity, abstraction, resource management, semantic distance. The recommended approach is to gradually deprecate observers in favor of reactive programming abstractions.

I should use observer pattern in a production program, if is criticized and maybe deprected in future? java.util.observer is always a good choice?

Second question: I have two objects. Server and Channel

Server.java

public Server {
    public Server () {
        // Some stuff
        channelList = new ArrayList<Channels>();
    }

    public Channel searchChannel(String channel) {
        // Implements searching channel
    }

    public void parseMessage() {
        if (someCondition1)
           onEvent1();
        elseif (someCondition2)
           onEvent2();
    }

    public void onEvent1(String channel, String param) {
        channel = searchChannel(channel);
        channel.onEvent1(param);
    }

    public void onEvent2(String channel, String param) {
        channel = searchChannel(channel);
        channel.onEvent2(param);
    }
}

** Channel.java**

public Channel {
    public Channel(Server server) {
         // Some stuff
         this.server = server;
    }

    public void onEvent1(String channel, String param) {
        // Stuff for Event1 fired from Server
        server.responseAtEvent("blablabla");
    }

    public void onEvent2(String channel, String param) {
        // Do stuff for Event2 from Server
        server.responseAtEvent("blablabla");
    }

In this case, in channel I have all method public and those are called from Server class... There is a better way to manage event handling? I thinking at ObserverPattern, but I guess it's more used in GUI stuff thus business logic class..

Cheers


I want to ask another thing regard Observer... Why a lot of people dislike the JDK Observer pattern and suggest to implement your?
Why re-invent the wheel?

The re-implemented observer that I see is same observer of the JDK...

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Kaiser69
  • 430
  • 8
  • 19

2 Answers2

0

You don't have to use the observer interface from Java. It is not even using generics. But you can implement your own observer DP.

Regarding your server / channel The problem I see in it, is that extending it would be a problem. What happens if you will have a new event: event3 in the future?

I suggest that you can have a list of listeners in the server. Once an event comes, all are activated. Each listener can be implemented by a concrete class. Each concrete class checks the condition, and if satisfies, the it performs.

Eyal Golan
  • 794
  • 1
  • 6
  • 17
  • My first question is regard having all method public..I thinks isn't a good way for encapsulating...So I must write a method in Server class like 'public void spamMessage(message)' to each channel associated to that server... – Kaiser69 Feb 18 '12 at 13:15
  • Of course that public method breaks encapsulation. but wouldn't you prefer to pass that work to the Channel? That way the server just passes the event to the listeners, and each one of them does what it needs to do. – Eyal Golan Feb 18 '12 at 18:56
  • Yes, in python I wrote a little framework that work in this manner..But is a bit easy (no static typo, no public/private). - Can you suggest some tips to watch out and implement listeners/eventslisteners? Thanks – Kaiser69 Feb 19 '12 at 13:20
0

Above the paragraph you quoted, Wikipedia displays the following note:

This article's Criticism or Controversy section may compromise the article's neutral point of view of the subject.

So this is to be considered an opinion rather than established fact.

The only reference for that claim is a paper that apparently has not been published in any peer-reviewed journal, nor has the technique described in that paper seen any widespread application. As such, it is just an idea. A promising idea, perhaps, but still just an idea.

The Observer pattern, on the other hand, has been applied countless times. Its defects and limitations have been throughly explored, and none of the those defects is sufficient grounds for deprecating the pattern for all possible applications.

It would appear the people at Wikipedia have come to the same conclusion.

So, if you want a known quantity, and don't want to learn a new programming language and a new paradigm to possibly improve on the observer pattern, I'd leave the scientific exploration to the scientists, and stick to the classic observer pattern for now.

meriton
  • 68,356
  • 14
  • 108
  • 175