Home  >  Article  >  Web Front-end  >  AOP (aspect-oriented programming) and OOP (object-oriented programming) of yui3_YUI.Ext related

AOP (aspect-oriented programming) and OOP (object-oriented programming) of yui3_YUI.Ext related

WBOY
WBOYOriginal
2016-05-16 16:01:311167browse

First of all, please put your hands on your chest and meditate: Am I falling in love with life, or am I being caught up in life?

I haven’t figured out the answer yet. Well, you can read the following. From a semantic point of view, different expressions of the same thing can reflect different people's subjective perspectives. From a philosophical point of view, worldview affects methodology. We look at things from different angles and sometimes draw completely contradictory conclusions, which affects how we do things. Methods and codes of conduct are like this in real life, and even more so in colorful programming languages. Programming models are full of various simulations of the real world, including process-oriented, object-oriented, and aspect-oriented. We are probably already very familiar with process-oriented and object-oriented. The English word for aspects is aspects (sometimes in terms of translation, I feel that using aspects can more appropriately express the connotation of aspects).

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 are slices? To give a simple example, every day we commute to and from work, take the subway, take the bus, date with our girlfriends, play games at the Internet cafe, go to the cinema to watch movies..., we have to do many things in a day, everyone is an Object, everything we do is The method of this Object, for example,

甲.上班();<br>乙.坐地铁();<br>丙.看电影();


In fact, we can look at it from another angle. Companies need employees to come to work, rail transit needs everyone to take it, and cinemas show movies to everyone. This becomes:

公司.need(甲)<br>地铁.carry(乙);<br>电影院.放电影给(丙);


From this point of view, not only individuals A, B, and C are objects, but companies, subways, and movie theaters are also objects. This abstraction is the traditional aspect-oriented aspect. In js programming, the program is generally not large, so it may not reach the point where aspect-level abstraction is necessary. But its event-driven principle is easily reminiscent of AOP. The previous example in js may be:

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

If viewed out of context, the above code is still semantically far-fetched. It's just that many js frameworks encapsulate the edge features of aspect programming into methods, which causes a lot of misleading to people. Such as event binding. When the execution of function foo ends, execute myfoo and add monitoring for foo without modifying foo.

var foo = 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 an internal mechanism of custom events, which can be seen everywhere in the source code. This is helpful in understanding the code reuse mechanism of yui3. It is precisely thanks to this abstraction that yui3's custom events are extremely powerful and flexible. Compared with OOP, the advantage of AOP is non-intrusive "decoration", but in most cases, it is not recommended to use AOP to write code first. Let’s look at this example: Everyone’s living habits are very similar. Here are four behaviors as examples: going to school, going to school, picking up girls, and playing games. A’s life pattern is very normal. The probability of each event happening is the same. B is a greedy person. The child who plays only goes to Internet cafes to play games and pick up girls. C is a child who loves to study and never picks up girls or plays games. Ding is an alternative with super strong experience. He always does two things at the same time. When he goes to school, Picking up girls and playing games after school. Here, a div is used to represent each person, and onmouseover is used to trigger each event.

Using OOP method, the program structure should be like this:

The code is here: yui_oop.htm

When the "weirdos" here inherit from the "normal people", they achieve the purpose of overloading through code rewriting, which obviously violates the principle of non-intrusiveness. Let’s look at the idea of ​​​​AOP:

The code is here: yui_aop.htm

The event release factory is abstracted here, which is used to specifically handle the release of events. The life track objects generated by it monitor everyone and capture everyone's various behaviors. The factory uses the listener binding of the function mentioned above when generating the "weird person's life track". This binding is non-invasive and can be well decoupled from the factory. Comparing OOP and AOP from this example, the complexity of the two is similar, but AOP has a broader idea and the code is more flexible.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn