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

相关推荐

    flex-spring-blazeds demo

    2. 在Spring中定义服务接口和实现,利用依赖注入提供给Flex客户端。 3. 在Flex中创建用户界面,通过BlazeDS调用Spring服务。 4. 理解AMF数据传输的优势,以及如何优化Flex与Java之间的数据交换性能。 5. 实践Spring...

    整合flex-spring-mybatis

    通过整合这三种技术,可以充分利用Flex丰富的用户界面特性、Spring的依赖注入与事务管理功能以及MyBatis的持久层操作能力,从而提高开发效率并降低维护成本。 #### 二、技术栈简介 - **Flex**:Adobe Flex是一种...

    整合Flex和Java--配置篇

    在实际开发中,我们还可以结合Spring框架来管理后台服务,实现更复杂的依赖注入和控制反转,提高代码的可维护性和可扩展性。 总之,配置Flex与Java的交互涉及多个步骤,包括BlazeDS的集成、Web工程的配置、服务的...

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

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

    flex-ibatis-sping项目的创建

    -- 定义具体的Bean及其依赖注入 --&gt; &lt;!-- 数据库连接配置 --&gt; &lt;!-- 更多Bean的定义 --&gt; ``` 通过以上步骤,我们成功地创建了一个基于Flex、Spring和MyBatis的项目。这种架构不仅能够充分利用各技术...

    Flex-Spring-JAVA-BLAZEDS.rar_Flex spring_flex_flex java

    Spring则是一个广泛使用的Java企业级应用开发框架,提供了依赖注入、面向切面编程等功能,是后端服务的核心。BlazeDS是Adobe提供的一个服务器端数据推送技术,它允许Flex应用与Java应用服务器进行实时通信。 描述中...

    spring-flex-balzeds-CRUD

    1. **Spring框架**:Spring 是Java开发中的一个核心框架,以其依赖注入(DI)和面向切面编程(AOP)而著名。在本项目中,Spring 作为后端服务的提供者,负责处理业务逻辑、数据访问以及与前端的交互。Spring MVC ...

    spring-flex集成-demo

    1. **Spring框架**:Spring是Java平台上的一个核心框架,它提供了依赖注入(DI)、面向切面编程(AOP)、事务管理等核心功能,以及对其他各种框架的集成支持。在这个示例中,Spring将作为服务层,处理业务逻辑和数据...

    spring+mybatis+flex4框架组合,适合初学者

    首先,Spring框架是Java平台上的一个全面的企业级应用框架,以其依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)为核心,提供了丰富的功能,包括但不限于: IoC容器、数据...

    spring整合flex所需jar包

    它提供了依赖注入(DI)和面向切面编程(AOP)的核心特性。 - Spring MVC是Spring框架的一部分,用于构建Web应用程序。它提供了一个模型-视图-控制器(MVC)架构,使得后端服务可以与前端分离。 2. Flex: - Flex...

    Flex入门-Flex整合Servlet

    Servlet可以使用Spring框架或其他依赖注入工具来管理业务对象和服务。 5. **定义服务**:在Flex项目中,使用RemoteObject或WebService组件来调用Servlet。配置相应的服务端点,指定AMF通道和目标方法。 6. **测试...

    arcgis server flex 中的依赖注入

    ### 依赖注入在ArcGIS Server Flex中的应用 #### 一、理解依赖注入...对于ArcGIS Server这样的应用场景而言,利用Flex中的依赖注入机制能够更加高效地管理和组织与地图服务相关的逻辑,提高整个系统的健壮性和扩展性。

    flexJar包和配置文件

    3. Spring Framework:Spring是一个广泛使用的Java企业级应用开发框架,提供了依赖注入、面向切面编程、事务管理等核心功能。SpringSide是基于Spring的一个项目模板,旨在简化Spring的使用,提供更好的开发体验。 4...

    flex-spring blazeds integration基本框架搭建记录

    Spring框架作为Java企业级应用的主流选择,提供了强大的依赖注入和面向切面编程功能。BlazeDS则是Adobe推出的一个轻量级数据通信中间件,用于Flex与Java服务器之间的实时双向通信。本文将详细介绍如何将这三个技术...

    flex-fqcn-finder

    这个工具在开发过程中尤其有用,特别是在处理依赖注入、自动加载或重构大型代码库时。 项目名为“flex-fqcn-finder”的Flex FQCN Finder正处于持续开发阶段(标记为[wip],工作进行中),这意味着它可能尚未发布...

    spring-flex

    Spring Flex 是一个开源框架,它将 Adobe Flex 与 Spring 框架紧密集成,使得开发人员可以使用 Flex 技术构建富互联网应用程序(RIA),同时利用 Spring 的强大功能,如依赖注入、AOP(面向切面编程)以及企业级服务...

    flex-spring.pdf

    这种整合方式不仅可以利用Spring提供的强大功能,如依赖注入、事务管理等,还可以使Flex前端与后端服务更加紧密地协作,提高整个应用系统的灵活性和可维护性。同时,Spring的轻量级特性也使得整合过程相对简单,降低...

    Flex--主程序与弹出窗口之间传递数据

    如果项目采用了依赖注入框架(如Spring Flex),可以通过注入服务或数据提供者来传递数据。 三、示例代码 由于提供的压缩包文件名`Test.swf`没有源码,无法展示具体实现。但通常,一个简单的数据传递示例可能包括...

    WEB项目-集成Flex3+BlazeDS3.2+Spring2.5.6集成方式一

    在本教程中,我们将探讨如何将...通过Spring的控制反转(IoC)和依赖注入,我们可以更好地组织和管理代码,同时保持Flex客户端的交互性和性能。这使得开发人员能够专注于业务逻辑,而无需过多关注底层基础设施的细节。

Global site tag (gtag.js) - Google Analytics