Yui3 AOP (facet-oriented programming) and OOP (object-oriented programming)

Source: Internet
Author: User
Tags aop comments

This article mainly introduces Yui3 AOP (aspect-oriented programming) and OOP (object-oriented programming), the need for friends can refer to the

First, put your hand on your chest and meditate: Am I in life or am I being myself?

I didn't come up with the answer, well, I can read the following. From the semantic point of view, the different expressions of the same thing can reflect the difference of human's subjective angle of view, from a philosophical point of view, the impact of the world view of the methodology, we look at things from different angles, sometimes come to a diametrically opposed conclusions, which will affect our way of doing things and code of conduct, real life so, in the rich and colorful programming language is so , programming patterns are full of simulations of the real world, including process-oriented, object-oriented, and facet-oriented. We are probably already very familiar with process oriented and object-oriented, the English of cutting plane is aspects (sometimes in translation aspect, I feel more apt to express aspects meaning with slice).

For links to AOP look here:

Http://en.wikipedia.org/wiki/Aspect-ori ... rogramming

The custom event in YUI3 implements the AOP

Http://developer.yahoo.com/yui/3/event

What is slice? To give a simple example, every day we commute to the subway to go to the bus and the girlfriend to date the Internet cafes to play games to the cinema ..., a day to do a lot of things, everyone is an object, we do everything is this object method, for example,

A. Work () B. Take the subway (); watch the movie ();

In fact, you can change the angle, the company needs employees to go to work, rail transport needs everyone to ride, the movie theater for everyone to play. This becomes:

Company need (a) subway. Carry (b); movie theater. Give a film to (c);

In this way, not only each individual is an object, companies, subways, cinemas are also objects, this abstraction is the traditional aspect-oriented. In the JS programming, the program is generally not large, so probably will not reach the level of abstraction to be used. But its event-based principle is easy to associate with AOP, the last example in JS may be:

?

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

If viewed from the context, the above code is still semantically far-fetched. Just a lot of JS framework to the edge of the programming edges of the packaging method, the people caused a lot of misleading. such as the binding of events. When the function Foo executes at the end of the execution Myfoo, add the listening to Foo on the basis of not modifying Foo.

?

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

Both jquery and prototype implement this simple function binding. jquery's AOP is here. But in Yui3, AOP is promoted to a built-in mechanism of custom events that can be found everywhere in the source code. This is helpful in understanding the YUI3 code reuse mechanism. It is also thanks to this abstraction that makes Yui3 's custom events extremely powerful and flexible. AOP has the advantage of non-intrusive "decoration" compared to OOP, but in most cases it is not recommended to use AOP first to write code. Look at this example: everyone's habits are very similar, here are four kinds of behavior for example, school, school, pick up, play games, a life law is very normal, the probability of each event is the same, B is a playful child, will only go to the Internet café playing games and girls, C is a love of learning children, never pick up and play games, Ding is an experienced super exuberant alternative, each time is doing two things at the same time, go to school when the girls, school playing games. This uses div to represent everyone, with onmouseover to trigger each event.

In OOP, the program structure should be this way:

Code here: yui_oop.htm

The "eccentric man" here inherits from "normal people" by rewriting the code to achieve the purpose of overloading, which is a clear violation of the non-intrusive principle. And then look at the AOP idea:

Code here: yui_aop.htm

Here the event publishing factory is abstracted, which is used to deal with the release of the event, to monitor each person by its generated life trajectory object, and to capture each person's behavior. The factory used the listening bindings of the functions mentioned above in generating the ' quirky life trajectory ', a binding that is non-intrusive and can be decoupled from the factory. Comparing OOP and AOP from this example, the two are more complex, but the AOP approach is broader and the code is more flexible.

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.