In-depth understanding of the Javascript series (21): Five Principles of s.o.l. I. D: interface isolation principle ISP

Source: Internet
Author: User
Preface

In this chapter, we will explain 4th of the five principles s.o.l. I. d implemented by the Javascript language, and the interface isolation principle ISP (the interface segregation principle ).

 
Http://freshbrewedcode.com/derekgreer/2012/01/08/solid-javascript-the-interface-segregation-principle/.
Note: This articleArticleThe author wrote a detour, So uncle was also quite depressed. Let's take a look. Don't get stuck.

The interface isolation principle is described as follows:

 
Clients shocould not be forced to depend on methods they do not use.
Customers should not be forced to rely on methods they do not need.

If the interface method that the user depends on is used only by other users and does not need to use it, it must implement these interfaces. In other words, A user depends on interfaces that are not used but used by other users. When other users modify this interface, all users dependent on this interface will be affected. This clearly violates the open and closed principle and is not what we expect.

The principle of interface isolation is similar to that of a single role. Both are used to gather responsibilities. In fact, an ISP can be understood to have a single role.ProgramConvert to an object with a public interface.

Javascript Interface

In JavaScript, how should we observe this principle? After all, JavaScript has no interface features. If the interface is what we want to establish contract and decoupling through the abstract types provided by a language, it can be said that it is okay, however, JavaScript has another form of interface. In the book design patterns-elements of reusable object-oriented software, we found the interface definition:

 
Any operation declared by an object contains an operation name, parameter object, and Operation Return Value. We call it the signature of the operator ).
All the operations declared in an object are called interfaces of this object ). An object interface depicts all request information that occurs on this object.

No matter whether a language provides a separate structure to represent interfaces, all objects have an implicit interface consisting of all attributes and methods of the object. Refer to the followingCode:

 VaR Examplebinder = {};
Examplebinder. modelobserver = ( Function (){
/* Private variable */
Return {
Observe: Function (Model ){
/* Code */
Return Newmodel;
},
Onchange: Function (Callback ){
/* Code */
}
}
})();

Examplebinder. viewadaptor = ( Function (){
/* Private variable */
Return {
BIND: Function (Model ){
/* Code */
}
}
})();

Examplebinder. Bind = Function (Model ){
/* Private variable */
Examplebinder. modelobserver. onchange ( /* Callback callback */ );
VaR Om = examplebinder. modelobserver. Observe (model );
Examplebinder. viewadaptor. BIND (OM );
Return Om;
};

The preceding examplebinder class library implements bidirectional binding. The public interface exposed by this class library is the bind method. The change notification and view interaction functions used in the BIND are implemented by the independent object modelobserver and viewadaptor respectively, in a sense, these objects are the concrete implementation of the common interface bind method.

Although JavaScript does not provide interface types to support contract of objects, the implicit interface of this object can still be provided to program users as a contract.

ISP and JavaScript

The following sections discuss the impact of violations of interface isolation principles in JavaScript. As we can see above, it is a pity to implement the interface isolation principle in javascript programs, but it is not as powerful as the static type language. The language features of JavaScript sometimes make the so-called interfaces a little non-stick.

Degraded implementation

In a static language, one reason for violating the ISP principle is the degraded implementation. The methods defined in all interfaces in Java and C # Must be implemented. If you only need several methods, other methods must also be implemented (empty implementation or exception throwing can be used ). In JavaScript, if you only need some interfaces in an object, he cannot solve the problem of downgrading the implementation, although he does not need to implement the above interfaces forcibly. However, this implementation still violates the Lee's replacement principle.

 VaR Rectangle = {
Area: Function (){
/* Code */
},
Draw:Function (){
/* Code */
}
};

VaR Geometryapplication = {
Getlargestrectangle: Function (Rectangles ){
/* Code */
}
};

VaR Drawingapplication = {
Drawrectangles: Function (Rectangles ){
/* Code */
}
};

When a rectangle alternative is used to meet the getlargestrectangle of the new geometryapplication object, it only needs the area () method of rectangle, but it violates the LSP (because it does not use the draw method that is used by the drawrectangles method ).

Static Coupling

Another reason for violation of ISP in static language is static coupling. In static language, interfaces play a major role in a loosely coupled design program. Whether in a dynamic or static language, sometimes an object may need to communicate with users on multiple clients (such as the shared state, the best solution is to use role interfaces, which allows users to interact with the object (and the object may need to be in multiple roles) as its implementation, it decouples user and irrelevant behaviors. This problem does not occur in Javascript, because objects are decoupled by the unique advantages of dynamic languages.

Semantic Coupling

One common cause of ISP violation is that both dynamic and static languages have semantic coupling, and semantic coupling is mutual dependency, that is, the behavior of an object depends on another object, which means that if a user changes one of the objects, it is likely to affect another user. This also violates the single responsibility principle. This problem can be solved through inheritance and object substitution.

Scalability

Another cause is the scalability. Many people will give callback examples to demonstrate scalability (for example, callback settings after Ajax is successful ). ISP will become very important if you want to implement such an interface and there are many familiar or methods in the implementation object, that is to say, when an interface becomes a requirement to implement many methods, its implementation will become abnormal and complex, and may lead to these interfaces to assume a non-sticky responsibility, this is the fat interface we often mention.

Summary

The Dynamic Language Features in Javascript make our implementation of non-viscous interfaces less influential than static languages, but the interface isolation principle still plays a role in JavaScript programming patterns.

Transferred from: Uncle Tom

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.