Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The ultimate goal is to have an interface which each component agrees upon.</p> <p>So, if, for example, I was building a JavaScript site that was done -entirely- in an old-school MVC (non-Rails/PHP) implementation, and completely in AJAX, I would make sure that each of the components implemented the same interface for observing.</p> <p>In each model/view/controller, I could name my "subscribe" method something completely different. Or, I could implement a standard interface for each.</p> <p>So I can have a public ".Register(event_type, subscribing_class)" method implemented in EVERY single component which could be expected to be called.</p> <p>Likewise, I can have a public ".Update(event_type, data)" method implemented in EVERY single component which could be expected to be called.</p> <p>The .Register and .Update are the interface for my Observer communication. Inside of my classes, each might have a ".Subscribe(publisher, event_type, self_reference)" method. That method might just be:</p> <pre><code>Class.Subscribe = function (publisher, event_type) { var self_reference = this; publisher.Register(event_type, self_reference); }; </code></pre> <p>Each might have an internal .Notify method:</p> <pre><code>Class.Notify = function (type, data) { var subscribers = this.subscribers[type], i = 0, l = subscribers.length; for (; i &lt; l; i++) { subscribers[i].Update(type, data); } }; </code></pre> <p>Because <em>I've agreed that ALL of my communication interface is going to behave this way</em>, it doesn't matter what my internals look like.</p> <p>My Model can continue to be oblivious to my View, and my View can continue to be oblivious to my Controller.</p> <p>The .Notify and .Subscribe don't need to be implemented that way - they aren't a part of the publicly-accessible interface. They could be anything I want.</p> <p>.Subscribe could take an ARRAY of publishers and push it through a for-loop, to subscribe to multiple points of data. Or take an array of { "pub" : x, "type" : y } object literals, and call the .Register method of each one, so that you can get ALL bootstrapping of that Class out of the way with one function call.</p> <p>Same thing goes with making an audio-player app. I don't care what public properties MusicPlayer shares. I know that it uses .Play(), .Pause(), .Stop(), .Load(track).</p> <p>If I make sure that I ONLY use the agreed-upon, public interface methods, the program is going to work. Why?</p> <p>Because the guy who works on MusicPlayer might change the internals of MusicPlayer. He might completely rewrite them. Maybe there's a ._<em>precacheSong(track) method. But what if it gets replaced with .</em>_cueTrack(track) down the road?</p> <p>You're just using some dude's widget, and one day your widget crashes, because you were extending it or implementing it based on non-interface methods or non-interface data, which happened to change as of v1.2.1</p> <p>So even in Loose Languages, the Interface is important. They give you a map of how you can expect to call ANY component that is expected to have that functionality - and regardless of how the internals of that component work, the inputs and outputs will be the exact same (though more type/error-checking is needed on inputs).</p> <p>They allow you to "Duck-Type" really easily (take a list of different Class instances - fire off the same function call on each one, expecting each has the same method and takes the same data-format).</p> <p>Even better with JavaScript:</p> <p>My .Subscribe code might even be written only once, and then bound to anything that I want to "inherit" it.</p> <pre><code>Interface.Subscribe = function (publisher, evt_type) { var self_ref = this; publisher.Register(evt_type, self_ref); }; Class_1.Subscribe = Interface.Subscribe.bind(Class_1); Class_2.Subscribe = Interface.Subscribe.bind(Class_2); Class_3.Subscribe = Some_Other_Interface.Subscribe.bind(Class_3); </code></pre> <p>And I can do that freely, because I know that everything I want to subscribe to is going to have the same public-interface.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload