`
hacker47
  • 浏览: 336586 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

FLEX提高篇------------FLEX依赖注入

阅读更多

原文来自www.adobe.com, 翻译得比较烂,欢迎指正.谢谢.

In previous articles I have written on Flex Development, I have stated that one of the good things about developing applications in Flex is how close to Java development it feels. Not to say that Java is the only language in which to develop, but if you work in a language/platform day in and day out to pay the bills, working with something new that is similar to a language/platform that you already know is a plus in my book. Specifically you will learn:

(我在前面的文章中写过关于FLEX的开发.我说过用FLEX来开发应用给人的感觉非常接近于用JAVA在开发,这是一件美妙的事情.不要说JAVA只是一种开发语言,如果当你日复一日地用一种语言或者在一个平台上工作而又不是免费时, 在工作中玩儿些新鲜的东西就像一种语言或者平台.从我的书中你已经知道这是一种乐趣,特别指出,你将学到:)

·                             The Interface construct in ActionScript 3.0 and how it is functionally equivalent to Java interfaces.

·                             How to use Interfaces in ActionScript 3.0 to achieve dependency injection.

·                             How to use a simple form of dependency injection within Flex.

·                             AS3中的构造器接口和如何在功能上做到跟JAVA接口一致

·                             如何在AS3.0中使用接口实现依赖注入

·                             如何在FLEX中使用简单的依赖注入

REQUIREMENTS(必要条件)

In order to make the most of this article, you need the following software:

(为了更好地实现文中的功能,你需要安装下列软件)

Flex 3 SDK

·                                 Download

Flex Builder 3

Eclipse 3.3

Prerequisite knowledge(准备知识)

For this article, familiarity with Flex Builder, although not required, would be helpful. Familiarity with object-oriented techniques is assumed as well as strong familiarity with ActionScript 3.0.

(阅读本文,你需要熟悉FlexBuilder,虽然不是必需的,但是很有帮助.熟悉AS3.0的面向对象技术,假设已经非常熟悉AS3)

UNDERSTANDING DEPENDENCY INJECTION(理解依赖注入)

It is generally considered good object-oriented design to limit the responsibilities of a class to one thing (there may be times when, as a developer, you may need to be flexible with this rule, but I am talking in general terms here). To abide by this rule, most objects work with collaborating objects to fulfill their responsibilities. But where do these collaborating objects come from? Typically, without dependency injection, you would instantiate collaborating objects in your components. This creates tight coupling between objects, which makes your app more difficult to maintain over time and harder to unit-test your components. Dependency injection is, in very rough terms, when you would use a framework or container to "inject" the collaborating objects into the respective objects that need them (see a more formal definition). This ends up being a very powerful pattern. You can develop your objects and test them independently (mocking out collaborators if any) and "wire" them together to build your application. The Spring framework is a ubiquitous DI framework in the Java development world.

(通常认为好的面向对象的设计里,一个类只负责处理一件事情,为了遵守这个规则,几乎所有的对象都必须合作才能完成它们的职责,但是那些合作的对象来自哪里?典型的,没有依赖注入,你只能实例化合作的对象在你的组件里,这样就会产生一个紧的藕合在你的对象之间.久而久之会让你的应用难于维护并且你的组件很难进行单元测试.此时依赖注入就应运而生.当你想使用一个框架或者一个容器来注入那些合作的对象至那些需要这些对象的对象里时,)这是一种非常强大的模式,你能开发你想要的那些对象并且独立地测试它们并且将它们捆绑到一起来构建你的应用.SPRING框架就是一个很常见的依赖注入框架在JAVA开发的世界里.)

I'll use a subset of the Spring Framework, Spring-MVC, as an example. In Spring-MVC you have Controller objects that handle inbound requests from a client; in this case, assume a web-browser. In most web applications today, there is a database or datastore on the back end of the application containing information that needs to be retrieved and displayed to the user. Suppose a user enters a search to list books by author. In the simplest terms, the Controller object accepts the request, pulls the author name out of the request parameter, hands that name off to the BookService object and expects a list of book titles in return, and then sends that list of titles back in response to the user request. The Spring framework handles setting the BookService object into your Controller object (configured through XML or annotations, but that is beyond the scope of this article). And the fact that your BookService object implements a Java interface means that changes to the implementation of the service object are invisible to the Controller object. That is a very good thing. As long as the parameters and return type of the method don't change, the Controller object should not care at all how the service does its job. But what does that have to do with Flex development?

(我将使用SPRING框架的一个子集,SPRING-MVC 作为一个例子. SPRING-MVC中你能控制那些从客户端进入的请求对象,在这种情况下,假设一个WEB浏览器,现在普遍的WEB应用,在系统的后端都有一个数据库或一个数据仓库包含了一些信息能够被取出并且显示给用户.假设用户进入了一个按作者搜索的书籍列表,在最简单的情况下,控制器对象要接受一个请求,从请求参数里取出作者名称,交给BookService对象来处理这个名称并且期望它返回一个书籍标题的清单列表,接着在响用户的请求时返回这个标题清单,SPRING框架能够设置BookService对象进入到控制器中(通过配置XML文件或者注释声明,当然这个在另一篇文章里讲),事实上你的Bookservice对象实现在JAVA接口,这就意味着它能够改变服务对象的实现让控制器对象不可见.这是一件好事情.和参数一样,并且返回类型都不用改变.控制对象也不用关心service对象如何处理它自己的事情,但是在FLEX开里又能够做些什么呢?)

DEPENDENCY INJECTION THROUGH ACTIONSCRIPT 3.0 INTERFACES(通过AS3接口进行依赖注入)

The Interface construct is what gives dependency injection its power. You have a bunch of objects that rely on each other to do their work, and instead of expecting a particular implementation of an object, which, if changed, would required changes in all dependent objects, the objects are only concerned with the contract specified by the interface, so implementation changes can be made seemlessly and testing is a much easier task. Well, guess what: ActionScript 3.0 supports the same Interface construct that makes writing loosely coupled code so much easier.

(接口式的依赖注入是非常强大的,你有一系列对象要互相依赖来完成他们的工作,并且你希望有一个特别实现的对象能够取代那种如果一旦改变就要求改变里面的所有依赖对象的状况,而对象仅仅只是关心特别指定的接口.这样改变实现就能做到XX并且测试也更加容易,,来猜下,AS3支持同样的接口构造并且能写出松散的代码如此容易吗?)

One would use Interfaces the same way you would in Java:

(一种方式你可以像在JAVA里面一样利用同样方式的接口)

1.                  Define your interface. An ActionScript 3.0 interface could look like:

定义你的接口,AS3中接口应该是这样的:

 

2.	package com.example {
3.	    public interface Controller {
4.	        public function search(searchParams:Object, callback:Function=null):void
5.	    }
}

 

1.                   Define a class that implements the interface.

(定义一个类实现接口)

 

7.	package com.example { 
8.	    public class HttpController implements Controller{
9.	        private var _httpService:HTTPService= new HTTPService();
10.	 
11.	        public function search(searchParams:Object, callback:Function=null):void {
12.	            details omitted for clarity
13.	        }
14.	    }
}

 

In the example above, you have a Controller interface and an implementation, HttpController, that will be used to communicate with the back end of your application to execute searches. HttpController is using the Flex object HTTPService to do the work of sending the search request and handling the response. You can see that the HttpService object is instantiated in the HttpController class. So why not inject the HttpService class as well? The point to having an interface is that you can change the implementation seamlessly to suit the needs of your application. In this case, if you needed another mechanism for searching, you would simply write a new implementation or modify the existing one.

(在上面的例子中,你有一个控制器接口和一个实现HttpController将被用来执行searchs方法来与你应用中的后台进行通信,HttpController是用Flex HttpService对象来完成发送搜索请求并处理响应工作的. 你可以看到HttpService对象是在HttpController类中被实例化的,所以为什么不注入HttpService类呢?这里指出一个接口你可以改变它的实现来适合于你的应用,在这种情况下,如果你需要另一种搜索结构,你可以简单写一个新的实现或者修改已存在的那个即可).

DEPENDENCY INJECTION, FLEX-STYLE(依赖注入,FLEX样式)

Here's where the rubber meets to road, so to speak. You have seen what dependency injection is and its importance in good application design. But how do you achieve that in a Flex application? As an example, say you have created a component to accept user input and display search results. This component is really a container of sorts, so you have extended the Panel class to hold the various objects to complete your search functionality. Additionally, you have defined your component's behavior entirely in an ActionScript class (no <script></script>tags in your MXML files!). Here's an example of what an extended Panel object could look like (with details omitted for clarity) in an ActionScript file:

(你见识过那些依赖注入并且比较重要的好的系统设计,)

public class SearchPanel extends Panel{
    private _searchController:Controller;
    public function search(text:String):void {
        this._searchController.search(text, this.callBackFunction);
    }
    public function set searchController(searchController:Controller):void {
        this._searchController = searchController;
    }
    public function get searchController():Conroller {
        return this._searchController;
    }
}

 

The salient point from the above example is the instance variable, _searchController of type Controller and the getter and setter methods that have the same name as the variable sans the "_" character. Next, take a look at the MXML file called SearchPanelView.mxml representing the visual properties for your component (again, some details omitted for clarity):

(上面的例子中的关键点儿是实例变量, Controller 类型的_searchController和它的gettersetter方法名称除了”_”之外名称是相同的,下面来看一下取名为SearchPanelView.mxml MXML文件,它代表组件的可视化属性)

<?xml version="1.0" encoding="utf-8"?>
    <SearchPanel xmlns="bbejeck.example.*" 
        xmlns:comp="bbejeck.example.*"
        xmlns:mx="http://www.adobe.com/2006/mxml">
        <comp:HttpController id="searchController" />
    </SearchPanel>

 

From the example MXML file above, there are a couple of key points:

(在上面的MXML文件中,这儿有几个关键点:)

1.                   The line <comp:HttpController id="searchController" /> defines the controller component to be used by the SearchPanel class. Take note that the class name in the definition is "HttpController," while the type of the _searchController instance variable only expects a type of Controller, it does not matter what the implementation is.

(1.   <comp:HttpController id="searchController" />这一行,定义了controller组件被SearchPanel类使用,注意定义的类名是HttpController,当这种类型的变量_searchController 实例变量只要是一个Controller类型,它不做被实现的任何事情.)

2.                  The id attribute matches the name of the getter and setter functions in the SearchPanel ActionScript class.

(id属性匹配SearchPanel 类中的set get方法的函数名称)

3.                  When the SearchPanelView.mxml file is loaded by the Flex application, the "HttpController" object is instantiated then the corresponding setter method in the SearchPanel class that matches the id attribute is called, injecting your Controller implementation into the SearchPanel class

(SearchPanelView.mxml文件被Flex 应用加载时, "HttpController"对象实例化后,SearchPanel类中的Id属性相匹配的setter方法会被调用,注入到你的Controller类的实现到SearchPanel类中)

4.                  When you need to change your search functionality, you can either change the HttpController object, or define a new implementation and modify the SearchPanelView.mxml file, either way your SearchPanel code does not need to change at all.

(当你需要改变你的搜索功能时,你可以不用改变HttpController对象,或者定义一个新的实现并且修改SearchPanelView.mxml文件,总之你的SearchPanel不需要做任何改动)

CONCLUSION(结论)

You have seen how to use dependency injection in Flex. The Flex application will inject into your components what objects are defined in the MXML file where the id matches a corresponding setter method. We are essentially getting a basic DI container for free by using Flex.
(
你已经看到了在FLEX中怎样进行依赖注入,FLEX应用中将会把那些你定义到MXML中的ID与对象中同名的setter方法注入到你的组件中,我们实质上已经自由地得到了一个基本的依赖注入容器在FLEX).

This leads us to some conclusions:
(
引导我们得出一些结论)

·                             When building applications, instead of focusing on building the application itself, you focus on what individual components are needed to build the application and what these components need to do their jobs. What you end up with is a large amount of "ready to go" components that just need some "callback" or service objects implemented in order to build the application. Code reuse is very high and the time it takes to build new applications is substantially reduced.
(
当构建一个应用时,替代构建应用自身的关注点,你关注那些在构建项中所需要的个别的组件并且这些组件需要做哪些工作.你可以结束大量的在构建项目中需要的一些实现回调或服务对象的组件的准备工作,代码重用是非常高并且可以减少很多时间来构建一个新的项目)

·                             Unit testing is much easier. Now, if you have components that have interface-based collaborators, when you are testing those components you can manually call the appropriate setter methods and supply mock objects that implement the correct interface but provides expected behavior for the test.
(
单元测试是非常简单的,现在如果你的组件是基于接口合作(交互),当你在测试这些组件时你可以手工调用相应的setter方法并且提供模拟对象实现正确地接口,但是提供了需要的行为供你测试.)

 

3
0
分享到:
评论
2 楼 地狱牢笼 2011-07-21  
建议分成两篇,一篇英文,一篇中文,这样看着不太舒服。
1 楼 momoko8443 2009-05-21  
AS3的IOC很容易实现。我最近想试着实现下AOP

相关推荐

    ansys maxwell

    ansys maxwell

    matlab基于不确定性可达性优化的自主鲁棒操作.zip

    matlab基于不确定性可达性优化的自主鲁棒操作.zip

    pytest-2.8.0.zip

    文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。

    信息安全课程实验C++实现DES等算法源代码

    信息安全课程实验C++实现DES等算法源代码

    基于知识图谱的医疗诊断知识问答系统python源码+项目说明.zip

    环境 python >= 3.6 pyahocorasick==1.4.2 requests==2.25.1 gevent==1.4.0 jieba==0.42.1 six==1.15.0 gensim==3.8.3 matplotlib==3.1.3 Flask==1.1.1 numpy==1.16.0 bert4keras==0.9.1 tensorflow==1.14.0 Keras==2.3.1 py2neo==2020.1.1 tqdm==4.42.1 pandas==1.0.1 termcolor==1.1.0 itchat==1.3.10 ahocorasick==0.9 flask_compress==1.9.0 flask_cors==3.0.10 flask_json==0.3.4 GPUtil==1.4.0 pyzmq==22.0.3 scikit_learn==0.24.1 效果展示 为能最简化使用该系统,不需要繁杂的部署各种七七八八的东西,当前版本使用的itchat将问答功能集成到微信做演示,这需要你的微信能登入网页微信才能使用itchat;另外对话上下文并没

    一个高品质的音乐共享和流媒体轻量音乐程序网站在线音乐源码

    一个高品质的音乐共享和流媒体轻量音乐程序网站在线音乐源码,是创建您自己的音乐流媒体网站的最佳方式! 最新版本: 添加插件系统,现在开发人员可以为程序制作插件并在更新后保留您的自定义设置。 固定的2 个以上的小错误。 安装所需:nginx/apache,mysql5.6+,php7+ 搭建说明:看源码内详细说明

    实现的金融风控贷款违约预测python源码.zip

    实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip实现的金融风控贷款违约预测python源码.zip

    麦肯锡—xx数码公司发展战略咨询报告.ppt

    麦肯锡—xx数码公司发展战略咨询报告.ppt

    FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

    FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写

    基于sklearn实现线性回归模型对波士顿房价进行预测源码.zip

    基于sklearn实现线性回归模型对波士顿房价进行预测源码.zip

    pytest-3.5.0.tar.gz

    文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。

    pytest-4.5.0.tar.gz

    文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。

    基于相干衍射成像模拟的matlab源码.zip

    基于相干衍射成像模拟的matlab源码.zip

    基于CS的远程监控系统软件项目(免费提供全套java开源项目源码+论文)

    项目介绍 背景 在当今的数字化时代,远程监控系统已经成为企业和个人必不可少的工具。随着物联网(IoT)技术的发展,监控系统的需求不断增加,不仅仅局限于视频监控,还包括数据监控、设备状态监控等。基于CS(Client-Server)架构的远程监控系统应运而生,旨在提供高效、实时、可靠的监控服务,帮助用户实现远程管理和控制。 目的 基于CS的远程监控系统软件项目旨在为用户提供一个综合性的监控平台,通过该平台,用户可以实时监控各类设备和数据,实现远程控制和管理,提高工作效率,降低运营成本。同时,该系统还可以用于安全防护、生产过程监控等多种场景,具有广泛的应用前景。 模块说明 前端模块 前端模块是用户与系统交互的界面,负责展示监控数据和接收用户指令。前端模块的主要功能包括: 用户登录与认证:通过安全的登录机制,确保只有授权用户才能访问系统。 实时数据展示:以图表、仪表盘等形式展示实时监控数据,包括视频流、传感器数据等。 报警通知:当监控系统检测到异常情况时,前端模块会通过弹窗、声音等方式通知用户。 远程控制:用户可以通过前端界面对设备进行远程控制,例如开关设备、调整参数等。

    网课专注度监测预警系统基于yolov5目标检测的网课专注度检测系统源码+模型+pyqt5界面.zip

    网课专注度监测预警系统基于yolov5目标检测的网课专注度检测系统源码+模型+pyqt5界面.zip

    matlab基于标签歧义的深度标签分布学习.zip

    matlab基于标签歧义的深度标签分布学习.zip

    九型人格测试题.144题dr.xls

    九型人格测试题.144题dr.xls

    麦肯锡—xx科技业务流程改造报告.ppt

    麦肯锡—xx科技业务流程改造报告.ppt

    pytest-8.2.0-py3-none-any.whl

    文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。

    1-8.py

    1-8

Global site tag (gtag.js) - Google Analytics