`
hacker47
  • 浏览: 336270 次
  • 性别: 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

相关推荐

    Angular-php-sf-flex-webpack-encore-vuejs.zip

    Angular-php-sf-flex-webpack-encore-vuejs.zip,一个简单的应用程序框架,试图使每个组件协同工作:...它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计目的是全面解决开发人员的web应用程序工作流。

    arcgis server flex 中的依赖注入

    • ActionScript 3.0中的接口结构和它是怎样与Java中接口功能上相关的。 • 在ActionScript 3.0中怎样使用接口来实现依赖注入。 • 怎样在Flex中使用一个依赖注入的简单形式。

    Angular-flex-layout.zip

    Angular-flex-layout.zip,为角度应用程序提供html ui布局;使用flexbox和响应式apiangular flex布局,...它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计目的是全面解决开发人员的web应用程序工作流。

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public ...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public class ...

    flex 最佳实践教程

    它提供了依赖注入、对象生命周期的管理和消息的支持. 它与其它框架最主要的不同是框架的作用域: 它像其它小型的 IOC 框架一样为简单程序提供了易用性,而且也为大型、复杂和模块化的程序提供了许多特性,如:支持 ...

    Flex IOC 框架概览

    控制反转(inversion of Control,IOC),也称为依赖注入(Dependency injection,DI),在过去几年中已经成为流行的软件设计模式,从而导致许多Flex开发者投入到此类框架的探索,其中就包括Spring ActionScript,Parsley...

    Angular-VSCode-Angular-TypeScript-Snippets.zip

    Angular-VSCode-Angular-TypeScript-Snippets.zip,Visual Studio代码类型脚本片段(typescript、HTML、...它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计目的是全面解决开发人员的web应用程序工作流。

    flex parsley IOC框架笔记

    flex actionscript 开发 对flex mxml技术的掌握 对java数据库后台框架 spring+hibernate ejb 3.0+jpa 等等后台技术

    ArcGIS_FlexView指南(中文)

    26 5.2 依赖注入(DI,也叫控制反转)...................................................................................... 29 5.3 国际化 ....................................................................

    robotlegs-framework:适用于Flash和Flex的ActionScript 3应用程序框架

    它提供: 依赖注入模块管理指令管理查看管理即插即用扩展下载文档和支持框架文档在存储库中以README文件的形式存在。 最好的阅读方式是通过GitHub:使用IntelliJ IDEA轻松开发Robotlegs 2(新增功能) 流利的API使...

    asp.net知识库

    体验.net2.0的优雅(四):Provider、策略、控制反转和依赖注入 泛型最佳实践 asp.net 2.0下嵌套masterpage页的可视化编辑 C# 2.0与泛型 动态调用对象的属性和方法——性能和灵活性兼备的方法 泛型技巧系列:用泛型...

    最新AngularJS开发宝典视频教程 后盾网AngularJS培训视频教程 后盾网.txt

    ├最新AngularJS开发宝典—第004讲 声明模块与控制器规范与依赖注入分析.mp4 ├最新AngularJS开发宝典—第005讲 $scope的基本使用方法.mp4 ├最新AngularJS开发宝典—第006讲 表达式与ng-bind及ng-cloak解决闪屏问题...

    Flicc-开源

    Flicc是用于Flex的基于Mxml的依赖注入框架。 Flicc允许您使用常量定义对象及其依赖项,以提供更多的编译时检查,配置视图组件并快速编写特定于域的注入标签。

    Parsley:欧芹示例

    我选择欧芹是因为它的依赖注入和控制性质的逆向。 这有助于以更好的方式实现表示模型架构。 ##Dependency Injection and Inverse of Control 所有依赖对象都将在 parsley 上下文中配置,并且可以在不创建新实例的...

    Ember:基于Flash组件的游戏开发实体系统框架

    Ember利用依赖项注入来简化编码而不会影响速度。 灰烬启用: 基于组件的游戏架构 游戏系统中的自动注入 自由选择合适的方式实施游戏 全模块化 代码 其他资源 执照 特此免费授予获得该软件和相关文档文件(“软件...

    Spring攻略(第二版 中文高清版).part2

    10.7 将依赖注入带给你的ActionScript客户 434 10.7.1 问题 434 10.7.2 解决方案 434 10.7.3 工作原理 435 10.8 小结 439 第11章 Grails 441 11.1 获取和安装Grails 441 11.1.1 问题 441 11.1.2 ...

    Spring攻略(第二版 中文高清版).part1

    10.7 将依赖注入带给你的ActionScript客户 434 10.7.1 问题 434 10.7.2 解决方案 434 10.7.3 工作原理 435 10.8 小结 439 第11章 Grails 441 11.1 获取和安装Grails 441 11.1.1 问题 441 11.1.2 ...

Global site tag (gtag.js) - Google Analytics