The viewer pattern in Ruby, JavaScript, PHP implements code _ruby topics

Source: Internet
Author: User

Interview was asked to JS in the Observer mode, this thing has long been known, can be said to be a dynamic language is very useful in a design pattern, very ashamed of no systematic research. But it does not matter, where does not make up where, immediately start, with the language will be implemented again.

Observer mode

As for the observer model, the Sumbon world of procedure speaks very clearly: The Observer pattern is that when an object's state changes, all objects dependent on that state are automatically notified, and the observer and the Observer are a one-to-many relationship. The book also has a very understandable example: get the system's current time and output to the command line. If you want to hard code is also very simple, define a method, first get the time, and then deal with the display. So the function of acquiring time is basically not reused, because of the drag with a handle display.

This can be done in the Observer mode: Define two classes, one for timing, for the current time, and another for the display of time. The class of show time is the observer, and the benefit of doing so is to reduce mutual dependence. The first class does not need to deal with anything after it is changed, just give notice to its observers that the specific things are handled by them. For example, one day we want to show time in different forms, or show time in many ways, without having to involve the first class at all.

PS: "Sumbon's Program World" is a good book. At first I wanted to read the book as a ruby tutorial, and later found out that it was not at all, and that the book contained a lot of esoteric philosophical ideas in a very understandable language. Although this book has little practical value for my entry-level rookie, it's a very good reading experience after reading it.

Ruby implementation

Implementing the Observer pattern in Ruby is simply and clearly outrageous, on the code:

Copy Code code as follows:

Require "observer"
#被观察者
Class Subject
Include observable
def notify
#do STH
Changed #更新标志为真
Notify_observers (*args) #如果更新标志为真, call the Observer with the parameter args method
End
End
#观察者
Class Obs
#回调函数, that's what it's called.
def update
#do STH
End
End
#实例
Sub = Subject.new
Sub.add_observer (obs.new)
Sub.notify

Yes, it's that simple ...
Mainly depends on the Observer module, in addition to the above, this module also has the following interface:

1.delete_observer (obj) deletes a specific observer
2.delete_observers Delete all observers
3.count_observers the number of people who receive observers
4.changed? Check for update flags

PHP implementation

PHP is not a hassle to implement:

Copy Code code as follows:

The person being observed
Class Subject
{
Private $_observers;
Public Function __construct () {
$this->_observers = Array ();
}
Public Function Add_observer ($obs) {
$this->_observers[] = $obs;
}
Public funtion Delete_observer ($bos) {
$index = Array_search ($bos, $this->_observers);
unset ($this->_observers[$index]);
}
Public Function notify_observers () {
foreach ($this->_observers as $v) {
$v->update ();
}
}
}
Observer
Class Observer
{
Public Function __construct () {
do sth;
}
Public Function Update () {
do sth;
}
}
Instance
$sub = new Subject ();
$obs = new Observer ();
$sub->add_observer ($obs);
$sub->notify_observers ();

JS implementation

JS implementation is not trouble, is to write some tools to facilitate the use of, such as the deletion of the array specified elements. The following is the simplest implementation method.

Copy Code code as follows:

The person being observed
function Subject () {
var _this = this;
This.observers = [];
This.addobserver = function (obj) {
_this.observers.push (obj);
}
This.deleteobserver = function (obj) {
var length = _this.observers.length;
for (var i = 0; i < length; i++) {
if (_this.observers[i] = = obj) {
_this.observers.splice (i, 1);
}
}
}
This.notifyobservers = function () {
var length = _this.observers.length;
Console.log (length)
for (var i = 0; i < length; i++) {
_this.observers[i].update ();
}
}
}
Observer
function Observer () {
This.update = function () {
Alert (1)
}
}
var sub = new Subject ();
var obs = new Observer ();
Sub.addobserver (OBS);
Sub.notifyobservers ();
var sub = new Subject ();

Conclusion: Ruby allows us to focus on the program design itself, not the syntax and characteristics of the program. JS Flexible, is a complete programming language in addition to the birth of a child can be achieved, is to play up a little kung fu. PHP, the good, not much to say.

These three procedures are running up to understand this design pattern thoroughly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.