Reread the JavaScript design pattern-Interface

Source: Internet
Author: User

Interface

And Class , JavaScript does not have native support for interfaces.

Here are three ways to implement the interface in the book:

Tips

In the real world, we're going to make a choice between the books.

  1. comments

    Annotation uses documents to constrain code, not enforce constraints, but only by the developer's conscious maintenance. The advantage of

    Is that it does not support additional code, does not increase the file size, and does not affect execution speed.

    The disadvantage is that you cannot provide a check, error message.

     

    # interface composite# Add (Child) # Remove (Child) # Getchild (index) # # INT Erface formitem# Save () Compositeform = (ID, method, action), # implements Composit, FormItem <link xmlns= "h Ttp://docbook.org/ns/docbook "xlink:href="%e5%af%8c%e6%9c%89%e8%a1%a8%e7%8e%b0%e5%8a%9b%e7%9a%84javascript.xml " xmlns:xlink= "Http://www.w3.org/1999/xlink"/> Compositeform.portotype = Add:-------... getchild:--- 

     

  2. Property Check

    This method can check if an object is annotated to implement some interfaces, but does not check whether the method that implements the interface definition is implemented.

    # interface composite#   Add (Child) #   Remove (Child) #   Getchild (Index) # # interface formitem#   Save () Compositeform = (ID, method, action),    @implementsInterfaces = [' Composite ', ' FormItem ']    ... addform = ( forminstance),    if (!implements (forminstance, ' Composite ', ' FormItem '))        throw new Error (' Object does not Implements a required interface. ')    ... # Add to Global/windowglabol.implements = (object), for    i in [1...arguments.length] # interfaces if not        Arguments[i] in object.implementsinterfaces            return false    True

  3. Duck Type Distinguishing form

    This method, on the previous basis, examines whether all the methods that implement the interface are implemented. And a new class is added to assist in the design of the Interface interface.

    # interfacescomposite = new Interface ' Composite ', [' Add ', ' Remove ', ' getchild ']formitem = new Interface ' FormItem ', [' Save ']# classcompositeform = (id,method,action) ... # implements# Business Codeaddform = (forminstance)-Interf Ace.ensureimplements forminstance, Composite, formitem# Interface classinterface = (name, method), throw new Error ( "Interface constructor called with #{arguments.length} arguments, but expected exactly 2.") If arguments.length!=2 @name = name @methods = [] For method in methods throw new Error (' Interface con Structor expects method names to being passed in as a string. ') If typeof Method! = ' string ' @methods. Push method # static Class Methodinterface.ensureimplements = (object) throw new Error ("expected at least 2 arguments") if arguments.length<2 for interface in ARGUMENTS[1...A Rguments.length] throw new Error ("Function interface.ensureimplements expert argumnets" and above to IS inStances of Interface. ") If interface.constructor! = Interface for method in Interface.methods if!object[method] | | typeof Object[method]! = ' function ' throw new Error ' object does not implemnet the #{interface.name} interf Ace. Method #{method} is not found. "

    Tips

    If the above interface implementation is used, then the type checking (instanceof) in the code should be removed because we have directly checked the implementation of the method.

Reread the JavaScript design pattern-Interface

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.