Can you tell the difference between JS dp : Observer vs Mediator

can you tell the difference between JS dp : Observer vs Mediator,…below is MEDIATOR Pattern… is it part A below same in both?

// PART A
var mediator = (function(){
   
    // Storage for our topics/events
    var channels = {};
   
    // Subscribe to an event, supply a callback to be executed
    // when that event is broadcast
    var subscribe = function(channel, fn){
        if (!channels[channel]) channels[channel] = [];
        channels[channel].push({ context: this, callback: fn });
        return this;
    };
    // Publish/broadcast an event to the rest of the application
    var publish = function(channel){
        if (!channels[channel]) return false;
        var args = Array.prototype.slice.call(arguments, 1);
        for (var i = 0, l = channels[channel].length; i < l; i++) {
            var subscription = channels[channel][i];
            subscription.callback.apply(subscription.context, args);
        }
        return this;
    };
    return {
        publish: publish,
        subscribe: subscribe,
        installTo: function(obj){
            obj.subscribe = subscribe;
            obj.publish = publish;
        }
    };
}());

/*
Here is an example that uses the implementation from above. It's effectively centralized
Publish/Subscribe where a mediated implementation of the Observer pattern is used:
*/
// PART B
(function( m ){
      // Set a default value for 'person'
      var person = "Luke";
      // Subscribe to a topic/event called 'nameChange' with
      // a callback function which will log the original
      // person's name and (if everything works) the incoming
      // name
      m.subscribe('nameChange', function( arg ){
          console.log( person ); // Luke
          person = arg;
          console.log( person ); // David
     });
   
      // Publish the 'nameChange' topic/event with the new data
      m.publish( 'nameChange', 'David' );
    
})( mediator );

Hi,

I’m a little confused about what you question is. Are you asking about the difference between the mediator and observer patterns?

yes exactly this…

tell me code has one that has not the other…?

In a basic implementation of the observer pattern, the ‘listener’ object subscribes directly to the object which will generate the events that you want to listen for. The problem with this is that it couples the objects together. The listener (or subscriber) depends on the publisher.

This is where the mediator pattern and your example code comes in. You have an object which acts as an event ‘channel’ that other objects can subscribe or publish to. This is especially useful in applications where you are loading different modules on demand… a subscriber can be set to listen for events from a module that’s not yet loaded. As soon as the module loads and starts publishing events, the listeners will receive them without any additional work.

You can find a much more detailed explanation of these patterns (along with examples) in this online book: Essential JavaScript Design Patterns.