JavaScript refactoring (7): reuse old code

Source: Internet
Author: User

In Java, there is such an old code:
Java code
Class Round {
Public void drawRound (); // draw a circle
}
 
Now the new Code wants to coexist with it and use a Person object to control it. However, it may be drawRound or drawRect:
Java code
Class Rect {
Public void drawRect (); // painter
}
 
Well, I thought about the Adapter mode first:
Java code
Interface Drawable {
Public void draw ();
}
 
Public class RoundAdapter implements Drawable {
Private Round round;
Public void draw (){
Round. drawRound ();
}
}
 
Public class RectAdapter implements Drawable {
Private Rect rect;
Public void draw (){
Rect. drawRect ();
}
}
 
Then, I will introduce another Person object to get it done:
Java code
Class Person {
Private Drawable adapter;
Public Person (Drawable adapter ){
This. adapter = adapter;
}
Public void draw (){
This. adapter. draw ();
}
}
 
Drawable rou = new RoundAdapter ();
Drawable rec = new RectAdapter ();
New Person (rou). draw (); // circle
New Person (rec). draw (); // painter
 
It may be annoying to see the simplest example of the Adapter mode. Let's take another look. What is the core of this model? Interface! Yes, it is the Drawable interface in the example. It is under the Protocol and leadership of the interface that we can make the circle and the painter so obedient.
 
In JavaScript, there is also such an old code:
Java code
Function Round (){
This. drawRound = function (){
Alert ("round ");
}
}
 
I also want to draw images based on Huludao, but JavaScript has no interface. What should I do?
......
What is the role of an interface? It is the statute of class behavior, but JavaScript behavior is dynamic and cannot be implemented or restricted by simple and pure interfaces, even if such an interface is simulated (see JavaScript Design Pattern), is it necessary to use it here? Does it violate the original intention of JavaScript to forcibly create an interface?
Back to this question, I originally hoped that the Person object could call a unified draw method, but passed in a Drawable object of different implementations when constructing the Person object, implemented under different constraints.
In JavaScript, not only can the caller of a method be passed in as a parameter, but also the method itself can be passed in as a parameter (the so-called method closure). In this way, all change points are controlled in this parameter, does it also achieve the effect of the Interface Protocol I want:
Java code
Function Rect (){
This. drawRect = function (){
Alert ("rect ");
}
}
 
Function Person (obj ){
// Obj parameter format: {doWhat, who}
For (var I in obj ){
This. doWhat = I;
This. who = obj [I];
Break;
}
This. draw = function (){
This. who [this. doWhat]. call (this. who );
};
}
 
Var rou = {drawRound: new Round ()};
Var rec = {drawRect: new Rect ()};
(New Person (rou). draw ();
(New Person (rec). draw ();
 
 
I am very happy to write it here:
In Java, through the Protocol of the interface and the help of the adapter, I encapsulate the change points in the parameters of the Person constructor;
In JavaScript, without the help of interfaces and adapters, I can still encapsulate the change points in the Person constructor parameters.

Author's "four-fire BLOG"
 

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.