DOM APIs 得失

Dissecting DOM

Bill Venners : What's wrong with DOM?

Elliotte Rusty Harold : There's a phrase, "A camel is a horse designed by committee." That's a slur on a camel. A camel is actually very well adapted to its environment. DOM, on the other hand, is the sort of thing that that phrase was meant to describe.

DOM was designed by committee for many different purposes. It had to work for HTML and XML. It had to work for JavaScript in web browsers and traditional programming languages running on servers. It had to support many different languages: Java, C, C++, JavaScript, AppleScript, Perl, Python, Rexx—and for all I know, INTERCAL. The committee that designed DOM was trying to do all these things. Within the constraints that they were operating under, they failed.

DOM is incredibly complex. It is full of gotchas.

DOM's Gotchas

Bill Venners : What's are some of DOM's gotchas?

Elliotte Rusty Harold : Take namespaces, for example. There are two basic models for handling namespaces in an XML API. In one model, you assign each element and attribute a certain namespace, and you figure out where the namespace declarations need to go when you serialize the document. In the other model, you don't provide any special support for namespaces—you just treat the namespaces as attributes. That also works, although it's harder on the end user. DOM is the only API I know of that does both, simultaneously. DOM requires client programmers to understand and use both models. Otherwise they'll produce namespace-malformed documents, which is truly evil. DOM has all the complexity of both approaches and the simplicity of neither.

There are a lot of other issues with DOM that stem from its cross-language nature. For example, DOM defines exactly one exception, DOMException , which has short type codes to indicate which kind of exception it is. To a Java programmer, this is just plain weird. Java programmers use many different exception classes, and never use short s for anything. When was the last time you used a short in code? Have you ever? I don't think I've ever used short , except when I was trying to demonstrate all the data types. But using a short makes sense from a C or C++ programming perspective, where shorts are more common, and having many exception types is not.

Some of the languages DOM needs to support, especially JavaScript, did not support method overloading at the time DOM was invented. Therefore, DOM could not have two methods such as createElement , one that takes an element name and a namespace, and another that takes only a local name. Instead, DOM has createElement , which takes just the name, and createElementNamespace , which takes both a name and a namespace. There are many non-overloaded methods in the DOM API that, to any Java or C++ programmer, should clearly be overloaded.

There are several other DOM design decisions that confuse people. For example, DOM trees do not allow nodes to be detached from their parent document. Only the document that created the node is allowed to hold the node. Also, DOM's DocType object is read-only. Why? I can't explain these design decisions. I just know that they are painful when you're actually trying to get work done with DOM.

Learning from DOM

Bill Venners : What did you learn from DOM? What things did they do that you thought made sense?

Elliotte Rusty Harold : The single biggest lesson from DOM that comes to mind is that polymorphism is good. It's very useful to have a Node interface or class, which all parts of the XML tree extend in some fashion. Often you just want to walk the tree and work with all the Node s. You don't care whether a Node is an Element , a Comment , a ProcessingInstruction —you don't need that more specific type. DOM works very well in those cases.

Bill Venners : What kind of processing can you do with a Node irrespective of its more specific type?

Elliotte Rusty Harold : You can merge two documents, for example. You want to select this portion of document A, and copy it into this element of document B. You just want to walk down the tree of document A, and copy each node in the tree into document B.

Bill Venners : One thing you said you learned from DOM is that interfaces are a bad idea. Why?

Elliotte Rusty Harold : I learned that partially from DOM. DOM is designed around interfaces, rather than concrete classes, because it is written in IDL and needs to be compiled to many different programming languages. It relies on the abstract factory design pattern to actually form documents and DOM implementations.

A large part of the trouble of getting started with DOM is learning to work with the interfaces, rather than directly with the classes. If you look at the code many XML novices write with DOM, you'll see it is littered with the implementation classes of the specific DOM implementation, such as Xerces or Crimson.

To some extent mentioning implementation classes is unavoidable, because DOM is incomplete. The Document class serves as an abstract factory for creating Element objects, Comment objects, Text objects, and so forth. The DOM implementation class is an abstract factory, which is used to create Document and DocType objects. However, they left out the part of the abstract factory design pattern, where there's a static method that lets you load the factory itself. You can't load the factory in DOM without using implementation-specific classes. Overall, I just saw that the interfaces were making life more difficult than it needed to be for a lot of programmers.

Bill Venners : You also said that you learned from DOM that successful APIs must be simple.

Elliotte Rusty Harold : Yes, although I suppose DOM is the reverse of that. DOM proves that a complex API is not likely to be successful, at least if it's substantially more complex than what it's trying to model.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值