I have been trying to make a decision which approach to take to build UIs: programmatic or declarative. I am using JavaScript and web technologies. In some projects like ExtJS and YUI they have taken a programmatic approach:
var container = new Ext.Container({
style: 'padding: 4px;',
items: [
new Button({text: 'hello'})
]
});
container.render(document.body);
Programmatic approach may be a necessity when you need to create view components dynamically(?). It can also be easier to program, at least it feels that for me since I'm used to APIs and code assistants.
However, some technologies like XUL (used for Firefox and Thunderbird UI) are based on declarative approach:
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="vbox example" title="Example 3...."
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<vbox>
<button id="yes" label="Yes"/>
<button id="no" label="No"/>
<button id="maybe" label="Maybe"/>
</vbox>
</window>
There is also a JavaScript GUI framework called AmpleSDK that takes the declarative approach. So, I am left with two choices to choose from, for my JavaScript based application.
Flex also supports declarative UIs, as well as programmatic ones. WFP and Silverlight seem to be focusing on XAML. Also, Open Laszlo seems to use the declarative approach.
Then I see jQuery UI being programmatic, and Dojo Toolkit being something in between the two approaches.
I have a few questions in my mind:
1) Can dynamic (components added in run-time) UIs be implemented solely in declarative manner?
2) Would it make sense 开发者_如何学Goto support both approaches, so that one could use declarative style for most of the part and if needed, use programmatic style?
3) Which one you prefer and why?
1) Yes, you can always do something like: elem.innerHTML = "<content>"; Most languages support something similar.
2) From what I've seen most languages support both. My guess is in the end, one variety is just transformed into the other. By not supporting one or the other, the language would limit itself. (I don't want to use xyz, I can't write UI declaratively/programmatically).
3) I personally like the declarative, it tends to get things done in fewer lines.
It depends on what kind of application you want to make:
→ Static
Declarative syntax is much better, easier to manage
→ Dynamic
A mix of both declarative and programmatic will be best
精彩评论