JS界面框架的思考——控件的继承、组合和可见性

deltamaster posted @ Sep 01, 2011 02:52:11 PM in JavaScript with tags javascript 框架 继承 组合 可见性 界面 消息传递 , 3679 阅读

  JS框架的另一个重要任务,就是控件的访问控制,即通过怎样的方式访问一个控件,如何保证不丢失引用,以及如何确保控件可见性的合理和安全。

  控件的继承是一个额外的问题,因为控件公共的属性和方法太多是需要使用继承特性的一个重要原因,而另一个重要原因则是为了使用多态性带来的便利。JavaScript的语法决定了,在JavaScript中实现多重继承是可行的,多重继承写法可以有意识地写成类似接口继承的形式,避免混淆的对象引用。

  例如,一个Form控件,要如何控制允许出现在其中的控件类型呢?比较合理的做法就是,将文本框、单选框、提交按钮等抽象FormItem的基类即可。某种控件里,应该限定允许存在的控件类型。

  一般情况下理想情况是,组合关系不可以形成环,但是有时形成环是无法避免的,这时候框架必须小心避免实际控件的循环包含关系,否则将导致无穷递归包含的现象发生。因此,要由框架来限制环的形成(毕竟无穷递归的广义表只是理论上研究的数据结构)。

  站在安全可控的角度,我们不应该让使用者用new关键字来建立一个控件,使用new建立的控件意味着框架无法得知控件的引用情况,这对于面向对象的界面控制是一种灾难,因为既无法保证安全的可见性,也无法保证不发生引用丢失。不安全的可见性就意味着,框架将无法感知控件的更改,而丢失引用意味着一些DOM没有逻辑控件对象可以操控。

  因此,我们往往通过父对象的方法来创建一个子对象,这个子对象的可见性自然处于父对象之中,由框架感知、掌控其所有操作和引用计数。这时候程序员的设计方法就变成,先创建父对象,然后向其中添加子对象,与使用new关键字方式创建控件的常见实现正好相反。

  不过这自然会导致代码复用性的问题,因为基于这种方式就意味着无法直接使用已经创建好的控件。对于这个问题的解决方案是,重用用于创建这个控件的选项参数,而不是重用这个控件本身。

  站在消息传递的角度,控件产生的消息只能由其创建者捕获,父控件捕获消息后,自行处理或将其转发到合适的子控件的处理队列中。这种消息传递机制非常严格,类似于树形网络的消息传递,是非常严谨可靠的,但是不可避免对于开发效率有一定的影响。这种写法是有长远的好处的,因为如果程序员无法明确控件的父子关系,那么很有可能无法预料异常结果的发生。

* 本文在CC BY-SA(署名-相同方式共享)协议下发布。
babysitting services 说:
Oct 05, 2019 05:40:43 PM

Everyone take self confidence in exposing ourselves among the leading maid services for Dubai. We have been adept for accomplishing the needs of this customers. Even the expense of a multi-storeyed family home or a high-rise apartment, our smooth home service personnel are always geared up at any doorstep by having a pleasing response and high quality etiquettes. At Vitality Clean Maid Service everyone follow a state-of-the-art guidance strategy of our amateurish Home Service personnel. And that’s how come our maids are often the best. Basically no stone is normally unturned a fabulous Gem located at Power Sparkling maid offerings.

part time maids in d 说:
Jan 26, 2021 04:55:09 PM

An important maid might improve family members life. Absolutely yes, a maid with the rescue will be sure the family unit spends some more time together in its place being normally busy in the house doing house hold chores. This separated up time will be spent socialising to provide a family and even bonding. House stuff will refuse you the possibility to bond to provide a family and in actual fact often establish more disagreements inside your home.

villa painting dubai 说:
Jun 23, 2021 07:23:22 PM

The 1st year life is extremely good. Every job looks like it's a safe bet. You've got one or two buddies working for you. They have zero complaints, doing his or her job along with you're making some dough. With the many cash being released you obtain yourself a new sweet brand-new 4WD pickup. Time passes by. Word is otherwise engaged that one does good operate. What that you do not hear is that men and women say you happen to be cheap. You leave a bunch of money shared. Soon you've got more operate than you'll be able to handle. You may need more tools, the guys will often be bent actually working whilst you drive around as part of your fancy brand-new truck.

monthly maid service 说:
Sep 24, 2021 07:58:38 PM

Know we typically are not trying to make cleaning services a terrible rap, it's just you will want to err quietly of caution just as with any several other service you should have or implement. The services you'll tend to obtain that shouldn't have that very good of history or is simply not used normally or exceptionally well known, many times it can be caused by the sizeable overturn some people see on employees. Most folks who make application for a job along with a cleaning product, find out the fact that the job just isn't what some people thought and often decide the fact that the job just isn't for them all. This can result in a maintaining service to battle to find help to be longstanding for your kids.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter