Yui3's AOP (Aspect-Oriented Programming), OOP (Object-Oriented Programming), and yui3aop

Source: Internet
Author: User

Yui3's AOP (Aspect-Oriented Programming), OOP (Object-Oriented Programming), and yui3aop

First, put your hands in front of your chest to become a meditation: I went to life, or I was living myself?

I didn't come up with the answer. Well, I can read the following. From a semantic perspective, different expressions of the same thing can reflect the different subjective perspectives of people. From a philosophical perspective, we will look at different perspectives of world view impact methodology, sometimes we come to totally different conclusions, which will affect our way of doing things and code of conduct. In real life, this is especially true in a variety of programming languages, the programming model is full of simulation of the real world, including process-oriented, object-oriented, and Aspect-oriented. We are probably very familiar with process-oriented and object-oriented. Aspects is used in English (sometimes in terms of translation, I feel that the meaning of Aspects can be better expressed by aspect ).

For links to AOP, see here:
Http://en.wikipedia.org/wiki/Aspect-ori... rogramming

Custom events in YUI3 implement AOP
Http://developer.yahoo.com/yui/3/event

What is a cut surface? Take a simple example. Every day, we go to and from work, take the subway, take the bus, and have a date with our girlfriend, go to the Internet cafe, play games, go to the cinema, watch movies ......, Every day, we have to do a lot of things. Everyone is an Object. Every thing we do is the method of this Object. For example,

A. Go to work ();
B. Take the subway ();
C. Watching movies ();


In fact, from another perspective, the company needs employees to come to work, rail transit requires everyone to take a ride, and the cinema places a movie for everyone. This becomes:

Company. need ()
Metro. carry (B );
Cinema. Give a movie to (c );


In this way, not only is each individual an object, but also a company, subway, or cinema is an object. This abstraction is a traditional aspect-oriented approach. In js programming, the program is generally not large, so it probably does not reach the degree of abstraction that requires the use of the aspect level. However, the event-driven principle is easily reminiscent of AOP. The previous example in js may be:

someone.dosth();//OOPobject.fire('event',someone);//AOP

From the context, the above Code is still far-fetched in semantics. However, many js frameworks encapsulate edge features of aspect programming into methods, which is misleading to humans. For example, event binding. When the execution of function foo ends, execute myfoo and add the listener to foo without modifying foo,

var foo = function(){  //some code here};jQuery.aop.after(foo,function(){  //added code here});

Jquery and prototype both implement this simple function binding. Here is jquery's aop. However, in yui3, AOP is promoted to an internal mechanism of custom events, which can be seen everywhere in the source code. This is helpful in understanding yui3's code reuse mechanism. This abstraction makes the custom events of yui3 extremely powerful and flexible. Compared with OOP, AOP has the advantage of non-intrusive "decoration". However, in most cases, it is not recommended to use AOP to write code first. Let's take a look at this example: Everyone's living habits are similar. Here we use four kinds of behavior as an example: Going to school, going to school, picking up girls, playing games, and having a normal life pattern, the probability of each event is the same. B is a playful child who only plays games and girls in the internet cafe. C is a learning-loving child who never plays or plays games, ding is a super-strong alternative. Every time he does two things at the same time, he picks up girls at school and plays games at school. Here, div is used to represent everyone and onmouseover is used to trigger each event.

The program structure should be as follows using OOP:

Code here: yui_oop.htm

When the odd guy inherits from a normal person, the code is rewritten to achieve the purpose of overloading. This is a clear violation of the non-invasive principle. Let's look at the idea of AOP:

Code here: yui_aop.htm

The event publishing factory is abstracted here to handle event Publishing. The live track object generated by the factory monitors everyone and captures various behaviors of everyone. The factory used the listening binding of the function mentioned above when generating the 'odd person life track'. This binding is non-invasive and can be well decoupled from the factory. Compared with OOP and AOP in this example, the complexity of the two is similar, but the AOP idea is more open and the code is more flexible.

Related Article

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.