- 浏览: 11965 次
最新评论
A Way to implement Abstract Class In Flex It's a fact that, until now(3.0) the ActionScript doesn't implement the abstract class, it has Interface, but the abstract class is very useful when you have some logic that is fixed, and these class extends the class have some same methods(logic), and some other methods should be implemented in the subclasses, we should prevent the customer(of the super class) instantiating the super class. Compare to Interface, the abstract emphasize the "extending", which means code reusing. But the Interface is more flexiable. Okey, I found an article which introduce a way to implement the abstract class, but the way is not that perfect, it can implement the "Abstract" in runtime, it can't check the error in complie time if the customer(of the super class) try to new the instance of the super class. The following are copied from http://joshblog.net/2007/08/19/enforcing-abstract- classes-at-runtime-in-actionscript-3/, by JOSH TYNJALA. As you probably know, abstract classes are not a language feature of ActionScript 3. If you are unfamiliar with the concept of an abstract class, let me explain. Think of an abstract class as a super-powered interface. It defines a series of functions that must be implemented, and it cannot be instantiated directly. It goes a step beyond an interface because it also defines some functions that are already fully implemented. To use an analogy, all electronic devices generally have the same connector to plug into the wall. However, not all electronics have the same purpose. A coffee maker and a DVD player do very different things. We could make an interface IElectronicDevice to make sure they all have a plugIntoWall() function, but then every device will need to re-implement the common wall plug functionality that doesn't differ very often. By making a class AbstractElectronicDevice, we can implement the wall plug functionality once, and all subclasses of AbstractElectronicDevice will be able to use it without re-implementing the same code. The problem, of course, is that the coffee maker and DVD player have different controls for turning power on and off. The coffee maker might have a switch, while the DVD player has a button. We can't implement the togglePower() function in AbstractElectronicDevice because many electronics will have different controls, so we need some way to force all subclasses of AbstractElectronicDevice to implement this function themselves. Using the methods developed to enforce Singletons in ActionScript 3 as a guide, I've found a way to enforce the abstractness of a class at runtime upon instantiation. There are two main parts to enforcing an abstract class. First, we must stop a developer from instantiating the abstract class directly. To do this, the constructor needs a little special sauce. Since anyone can create an instance of a public class by simply using the new keyword, we need the constructor of our abstract class to require a parameter that only subclasses will be able to pass. Second, we must ensure that a developer implements functions that the abstract class has defined, but not implemented. Stopping Direct Instantiation of an Abstract Class The magic bean to stop a developer from instantiating a class directly is one keyword: this. You don't have access to an instance of a class until after you call a constructor, so only an instance of a subclass will be able to pass a reference to itself to the super class. Let's look at some code to help make things clearer. The instance of MyAbstractType expects to receive a reference to itself as the first parameter in the constructor. If it does not, an error will be thrown. package com.joshtynjala.abstract { import flash.errors.IllegalOperationError; public class MyAbstractType { public function MyAbstractType(self:MyAbstractType) { if(self != this) { //only a subclass can pass a valid reference to self throw new IllegalOperationError("Abstract class did not receive reference to self. MyAbstractType cannot be instantiated directly."); } } } } Only MyConcreteType and other subclasses of MyAbstractType will be able to pass a reference to the instance to their super() constructors. Notice that users of MyConcreteType don't need to know that it extends an abstract class. The signature of the MyConcreteType's constructor can be completely different than MyAbstractType. Next, like with an interface, we need to force subclasses of our abstract class to implement specific functions. Ideally, we want this enforcement to happen immediately when the object is created (to make bugs visible as early as possible). After we check that the user isn't trying to instantiate the abstract class directly, we should check to be sure the unimplemented methods are overridden. We can do by checking the results from theflash.utils.describeType() function against a list of unimplemented methods in the abstract class. Again, a little code should give us a clearer picture. In MyAbstractType, after we check for a reference to the self parameter, we build a list of unimplemented functions in an Array. Next, we use describeType()to get a list of the methods declared in the instance. If a subclass of MyAbstractType overrides a method, the declaredBy attribute in the method XML will specify the name of the subclass rather than MyAbstractType. package com.joshtynjala.abstract { import flash.errors.IllegalOperationError; import flash.utils.describeType; import flash.utils.getQualifiedClassName; public class MyAbstractType { public function MyAbstractType(self:MyAbstractType) { if(self != this) { //only a subclass can pass a valid reference to self throw new IllegalOperationError("Abstract class did not receive reference to self. MyAbstractType cannot be instantiated directly."); } //these functions MUST be implemented in subclasses var unimplemented:Array = [mustBeOverridden]; //get the fully-qualified name the abstract class var abstractTypeName:String = getQualifiedClassName(MyAbstractType); //get a list of all the methods declared by the abstract class //if a subclass overrides a function, declaredBy will contain the subclass name var selfDescription:XML = describeType(this); var methods:XMLList = selfDescription.method.(@declaredBy == abstractTypeName && unimplemented.indexOf(this[@name]) >= 0); if(methods.length() > 0) { //we'll only get here if the function is still unimplemented var concreteTypeName:String = getQualifiedClassName(this); throw new IllegalOperationError("Function " + methods[0].@name + " from abstract class " + abstractTypeName + " has not been implemented by subclass " + concreteTypeName); } } //implemented public function alreadyImplemented():void { trace("Don't forget to list me in the Array of valid functions."); } //unimplemented public function mustBeOverridden(param:String):void {}; } } Now, in MyConcreteType, we can implement the mustBeOverridden() function. If we do not, an error will be thrown. To be certain, try commenting out the implementation of mustBeOverridden() in MyConcreteClass. If you instantiate it, you will receive a runtime error. As you probably noticed, the first part of implementing an abstract class is very simple. Making the abstract class receive a reference to itself enforces subclassing, and it can be done in only a few lines of code. The second part, making an abstract class work like an interface, requires a lot more code, and it may be more prone to errors. You, as a developer, must remember to add all the unimplemented methods to the Array in the constructor. Additionally, if the class gets large, I can imagine that calling describeType() often enough could lead to performance problems. In most cases, as long as you keep your classes clean, it should work well. I highly recommend the subclass enforcement, but I wouldn't be heartbroken if you feel that checking for unimplemented methods is overkill. Yes, it is really a good idea to implement the "Abstract Class", but the checking is postponed to runtime, then some defects appear: May be it costs too much, you have to call describeType, this is really costing, especially when you have to create a lot of instances of subclasses.
发表评论
-
字符串内建函数
2012-07-06 09:52 604>>> quest='what ... -
js+正文规则 高亮搜索关键字(二)
2012-07-06 09:46 71711 我顶 用户通过搜索引擎搜索关键字进入你的网站或者 ... -
TWaver Flex与.net最新版,三大视图组件齐备
2012-07-03 13:44 824TWaver Flex与.NET最新版,三大视图组件齐 ... -
flash特效原理:标签云
2012-07-02 13:13 662其实标签云是一个比较常见的特效类,在wondefl里 ... -
Flex: flash聊天框 表情+文字 图文混排 (思路)
2012-07-02 13:13 851flash聊天框 表情+文字 图文混排 (思路) s ... -
Flex 4(Hero)对应Flex 3 容器方法
2012-07-02 13:13 575Spark容器 Halo容器 说明 numElem ... -
项目启动异常:Exception: java.lang.RuntimeException: XPathFactory#newInstance()
2012-07-02 13:12 1657项目启动时,报如下异常: **** MessageBrok ... -
Flex中datagrid动态添加列
2012-07-02 13:12 685addChild(dataGrid ... -
flex4:中DataGroup组件获取数据源(ArrayCollection方式)实例
2012-07-01 10:44 645flex/spark" xmlns:mx=&q ... -
flex--LineChart
2012-07-01 10:44 589最近工作上用到了linechart,在网上查的资料还是非常 ... -
Flex 动态创建 多个曲线图/柱形图 ColumnSeries
2012-07-01 10:44 700获取数据后的回调函数 private functio ... -
flex cairngorm项目实战
2012-07-01 10:44 506项目功能与界面模仿阿布网站 系统功能: 新闻管 ... -
Flex 拖拽范例
2012-06-30 17:08 502Flex 拖拽范例 2011年03月09日 Flex由于 ... -
Flex和Java 整合
2012-06-30 17:08 553Flex和Java 整合 2010年07月21日 Fle ... -
Flex FTP文件上传
2012-06-30 17:08 764Flex FTP文件上传 2010年10月10日 Fle ... -
Flex中的set啊Flex中的set
2012-06-30 17:07 475Flex中的set啊Flex中的set 2010年10月17 ...
相关推荐
Implement with Class and Collection a List Collection with add and remove data elements.
A tool to generate class files to implement stored procedures创建类文件执行存储过程的工具
信息安全_数据安全_How to Implement a World Class I 云数据库 区块链 解决方案 安全攻击 网络安全
a Freeware MFC class to implement Map pins(284KB)
Planing to Implement Service Management
IMPLEMENT EVENTS IN TABLE MAINTENANCE
Describes a method to implement resizable child windows.(108KB)
How to Implement ObjectOriented Programming Principles in MATLAB
This application demonstrates how to implement a splitter bar in a Visual Basic Project.
A C language repo to implement CSAPP
An MFC class to implement single instance apps(17KB)
Teradata Cookbook Over 85 recipes to implement efficient data warehousing solutions 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
How-to Guide%3a Implement Value Helps in GenIL Components
A C++ wrapper for TWAIN. Allows you to implement a scanning interface.(67KB)
1)Write code for ShoeInfoVisitor to implement all the methods in this class to get descriptions of all the kind of shoes Also in the client class SportsShoesGUI, add necessary methods to get the ...
Part 3 explains how Flex could be leveraged to implement Web 2.0 ideas in practice. Chapter 10: Flex Mashups Mashups are the new-generation style of dynamically creating composite applications. ...
Machine Learning: Step-by-Step Guide To Implement Machine Learning Algorithms with Python By 作者: Rudolph Russell ISBN-10 书号: 1719528403 ISBN-13 书号: 9781719528405 出版日期: 2018-05-22 pages ...
在你的程序中实现打印支持Learn how to implement print support in your applications.
rsa using java to implement and we can encrypt and decrypt files.