- 浏览: 154384 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
zyq070:
version 自动增长的 你手动设值 相比之前的值已经变化 ...
Row was updated or deleted by another transaction (or unsaved-value mapping was -
中华神韵:
...
Java中从一个ArrayList删除重复的元素 -
Menuz:
logcat慢慢调,终会找到的。
The application has stopped unexpectedly -
右转随缘:
好抽象。。。
The application has stopped unexpectedly -
tsmg:
您好,我zend也配了,怎么就是不能正常调试呢?是不会用在EP ...
安装EPP的调试Zend Debugger
[Forward]Selected Design Patterns - Data Transfer Object
- 博客分类:
- Flex
Data transfer objects are also known as value objects (VOs) and are used for data exchanges between various application components, which can be either colocated in the same process or on remote computers. These DTOs can even be written in different programming languages, for example, Java and ActionScript. First, modify the application from the previous section and encapsulate the order details in a simple OrderDTO that will be placed in the event object and will happily travel between price and order panels. When this is done, you will spend some time with more advanced DTOs that you may want to use in Flex remoting. Example 2-12 is a simple OrderDTO.as that will be passed between the price and order panels. Example 2-12. OrderDTO.as In Example 2-13 ’s second version of the price panel, add a function startDataFeed(), emulating the real data feed that may be bringing the market data to the pricing panel. Please note that the PricePanel now displays the data from this “external” feed by binding the UI controls to the properties of the currentData object “received” from a remote server. Example 2-13. PricePanel2.mxml In Example 2-14 , the function placeOrder()dispatches the OrderEvent2with a packaged DTO inside. There is no need to declare multiple variables, as this was done in Example 2-9 . Example 2-14. OrderEvent2.as The new version of your driving application, Trading2.mxml (Example 2-15), does not assign the symbol, bid, and ask values to the price panel, as this was done for simplicity in Example 2-11 . Now the PricePanel is being populated by its own data feed. Example 2-15. The driving application, Trading2.mxml Even though you haven’t yet seen the code of the OrderPanel2, you can still use it, as long as its API is known—in this case, you know that it listens to the OrderEvent2. As a matter of fact, in many cases you’ll be using components without having any knowledge about how they operate inside. But to go easy on you, Example 2-16 shows you the source code of OrderPanel2.mxml, which receives the OrderEvent2, extracts the OrderDTO, and populates its UI controls. Example 2-16. OrderPanel2.mxml Examples 2-12 through 2-16 illustrated an application that used a DTO as a sort of exchangeable currency in the interaction between colocated Flex components. But DTOs also play an important role during the exchange of data with the server-side application using Flex remoting or Data Management Services. In such enterprise applications, the server-side team provides a DTO coded in one of the programming languages (this example uses Java), and the Flex team has to provide a similar Action-Script DTO. Flex RemoteObjector DataServiceclasses will serialize/deserialize these DTOs into each other, regardless of which direction they travel. If you don’t define DTOs on the Flex side, the data will be wrapped into ObjectProxy instances, which has a negative effect on performance. If you do, annotate Flex DTOs with the [RemoteClass...] meta tag or via the registerClassAlias() function call. We highly recommend using strongly typed data transfer objects, as opposed to dynamic objects or XML for data exchange between the client and server tiers. If you are working with a Java Server, make your Java (methods) accept/return custom classes and not generic map objects. The following list gives you some generic recommendations about creating DTOs that are meant for communication with a remote subsystem, and then offers a solution that can automate the process of creating ActionScript DTOs from their Java peers. This is yet another technique (remember wrapping up an object in a proxy?) for customizing the behavior of the objects when the data is being changed. Imagine that you need to create your own version of a data management service and want to maintain a collection of changed objects that remember all modifications in a DataGrid that uses a collection of OrderDTO objects as a data provider. You can maintain a collection of changed objects that remember all old and new values. If you are creating DTOs for the data exchange between Java and ActionScript classes using subclassing, both ActionScript classes will have the meta tag [RemoteClass] pointing to the same Java DTO. This won’t be an issue; Flex is smart enough to use the subclass for serialization. In the real world, an enterprise project’s Flex and Java developers often belong to different teams and if Java folks change the structure of their DTOs, Flex developers need to ensure that the structure of their classes is updated accordingly. There are different ways of automating this process, as shown in Example 2-17 . DTO2Fx is a free plug-in that’s available at http://www.myflex.org . It generates ActionScript DTO classes using the subclassing technique described earlier. Consider the Java DTO in Example 2-17 . Example 2-17. Annotated OrderDTO2.java The DTO2Fx plug-in uses Java annotations in the process of generating ActionScript classes, and @FXClass is such an annotation. The rest of the process is simple. As soon as you create or modify this class, it automatically regenerates a couple of ActionScript classes: _OrderDTO2.as and OrderDTO2.as. You can find more details about this process in the User Guide of DTO2Fx, but for now just examine the generated code in Example 2-18. Example 2-18. Superclass _OrderDTO2.as Example 2-18 is a superclass that will always be regenerated by DTO2Fx anytime the Java class changes. This class has a unique object identifier (uid) and includes getters and setters that will dispatch propertyChange events when the time comes. The code of the class OrderDTO2 is shown in Example 2-19 . This class is generated only once and is a subclass of _OrderDTO2.as. This is a place for an application developer to add application-specific customization, such as the addition of new properties and/ or functions. This class will never be overridden by DTO2Fx, regardless of what was changed in OrderDTO2.java. Example 2-19. Subclass OrderDTO2.as We hope that our message to you is clear now: the use of DTOs is a preferred way of designing interobject communications.
package com.farata.dto {
// [RemoteClass] meta tag goes here if this DTO
// is used in Flex Remotin
[Bindable]
public class OrderDTO {
public var symbol:String;
public var bid:String;
public var ask:String;
public var buy:Boolean;
//a buy/sell flag
public function OrderDTO(symbol:String, bid:String, ask:String,
buy:Boolean=false) {
this.symbol=symbol;
this.bid=bid;
this.ask=ask;
this.buy=buy;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="230" height="100"
backgroundColor="#D4E5D9">
<mx:TextInput x="0" y="-1" width="228" backgroundColor="#0DF113"
text="{currentData.symbol}" fontWeight="bold" fontSize="19"
textAlign="center"/> <mx:Label x="39" y="31" text="Bid" fontWeight="bold" fontSize="14"/> <mx:TextArea x="1" y="49" width="109" height="47" backgroundColor="#EBF4A2"
text="{currentData.bid}" fontSize="22" fontStyle="normal" fontWeight="bold" click="placeOrder(true)" editable="false" creationComplete="startDataFeed()"/>
<mx:Label x="154" y="31" text="Ask" fontWeight="bold" fontSize="14"/>
package com.farata.events{
import com.farata.dto.OrderDTO; import flash.events.Event;
public class OrderEvent2 extends Event {
public var orderInfo: OrderDTO; public var eventType:String;
public static const PREPARE_ORDER_EVENT:String ="OrderEvent"; public static const PLACE_ORDER_EVENT:String ="PlaceOrderEvent";
public function OrderEvent2(eventType:String, order:OrderDTO ) {
super(eventType,true, true); // let it bubble this.orderInfo=order; // store the orderDTO
this.eventType=eventType;
}
override public function clone():Event {
return new OrderEvent2(eventType,orderInfo);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:comp="com.farata.components.*" backgroundColor="white" applicationComplete="this.addEventListener(
OrderEvent2.PREPARE_ORDER_EVENT,orderEventHandler)" >
<mx:Label text="Price Panel" y="4" height="23" x="69" fontSize="16"
fontWeight="bold"/>
<mx:Label text="Order Panel" y="4" height="23" x="290" fontSize="16"
fontWeight="bold"/>
<comp:PricePanel2 y="31" x="7"/>
<comp:OrderPanel2 id="ordPanel" x="245" y="30"/>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import com.farata.events.OrderEvent2;
private function orderEventHandler(evt:OrderEvent2):void{
// The mediator decides what to do with the received event
// In this case it forwards the order received
// from PricePanel to OrderPanel
var orderEvt: OrderEvent2= new OrderEvent2(OrderEvent2.PLACE_ORDER_EVENT,evt.orderInfo);
ordPanel.dispatchEvent(orderEvt);
}
]]>
</mx:Script>
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="230" height="100"
backgroundColor="#4CF3D2" creationComplete="this.addEventListener(OrderEvent2.PLACE_ORDER_EVENT,orderEventHandler)">
<mx:Text id="sym" x="0" y="10" width="61" fontWeight="bold" fontSize="19"/>
<mx:Text id="operation" x="81" y="10" fontSize="19"/>
<mx:Text id="price" x="48" y="37" width="91" fontWeight="bold" fontSize="16"/>
<mx:Label x="5" y="65" text="Qty:" fontSize="19" fontWeight="bold"/>
<mx:TextInput id="qty" x="70" y="69" width="71" text="100" fontSize="16" selectionBeginIndex="0" selectionEndIndex="5"/>
Use an ArrayCollectionof such bindable DTOs as a dataProviderin your DataGrid, List, and similar components. Imagine a collection of objects with complex structure, with class variables of non-primitive data types—for example, a collection of Employee objects in which each object has a variable of type WorkHistory, which is a class with its own properties. If a variable declared in the WorkHistory class gets modified, the collection of Employee objects won’t know about this change unless you explicitly dispatch the propertyChange event.
For instance, instead of sorting orders by the DTO’s property symbol, you’d sort by symbol and uuid. In this case, the autogenerated hash value of each DTO will be unique for each record, which will result in better performance.
[Bindable(event="propertyChange")]
There’s a difference between the [Bindable(event="propertyChange")] and [Bindable]meta tags. The former syntax instructs the Flex compiler to generate code watching the propertyChange events. The latter syntax forces the Flex compiler to generate the event—it replaces the property with a setter/getter pair in which the setter’s role is to dispatch the event. But if your code has taken care of event dispatching already, you may wind up with events being dispatched twice!
[Bindable(event="propertyChange"]
public function get totalOrderAmount():Number {
return price*totalShares;
}
package com.farata.dto;
import com.farata.dto2fx.annotations.FXClass;
@FXClass
publicclass OrderDTO2 {
public String symbol;
public String bid;
public String ask;
public Boolean buy;
public OrderDTO2(String symbol, String bid,String ask, Boolean buy) {
this.symbol=symbol;
this.bid=bid;
this.ask=ask;
this.buy=buy;
}
}
package com.farata.dto {
import mx.events.PropertyChangeEvent;
import flash.events.EventDispatcher; import mx.core.IUID; import mx.utils.UIDUtil;
/* [ExcludeClass] */
public class _OrderDTO2 extends flash.events.EventDispatcher implements mx.core.IUID {
/* Constructor */
public function _OrderDTO2():void {
super();
}
// implementors of IUID must have a uid property
private var _uid:String;
[Transient]
[Bindable(event="propertyChange")]
public function get uid():String {
// If the uid hasn't been assigned a value, just create a new one.
if (_uid == null) {
_uid = mx.utils.UIDUtil.createUID();
}
return _uid;
}
public function set uid(value:String):void {
const previous:String = _uid;
if (previous != value) {
_uid = value;
dispatchEvent( mx.events.PropertyChangeEvent.createUpdateEvent( this, "uid", previous, value));
}
}
/* Property "ask" */
private var _ask:String;
[Bindable(event="propertyChange")]
public function get ask():String {
return _ask;
}
public function set ask(value:String):void {
const previous:String = this._ask;
if (previous != value) {
_ask = value;
const ev:mx.events.PropertyChangeEvent =
mx.events.PropertyChangeEvent.createUpdateEvent(
this, "ask", previous, _ask);
dispatchEvent(ev);
}
}
/* Property "bid" */
private var _bid:String;
[Bindable(event="propertyChange")]
public function get bid():String {
return _bid;
}
public function set bid(value:String):void {
const previous:String = this._bid;
if (previous != value) {
_bid = value;
constev:mx.events.PropertyChangeEvent = mx.events.PropertyChangeEvent.createUpdateEvent( this, "bid", previous, _bid); dispatchEvent(ev);
}
}
/* Property "buy" */
package com.farata.dto {
[RemoteClass(alias="com.farata.dto.OrderDTO2")]
public class OrderDTO2 extends com.farata.dto._OrderDTO2 {
/* Constructor */
public function OrderDTO2():void {
super();
}
}
}
发表评论
-
js调用flash的方法时报错:Error calling method on NPObject!
2012-12-27 15:53 3140如题; uncaught exception: Error ... -
AIR 2.6 NativeProcess is not supported
2012-03-28 11:46 1879使用Flex AIR2.6开发桌面程序,添加程序自己重启或启动 ... -
AIR application killed when check camera device
2011-07-25 19:16 746I met this problem and spen ... -
UIComponent中的parentDocument和parent
2011-06-26 17:26 1579ParentAndParentDocument.mxml ... -
[Forward]How to compile CSS file in Flash Builder
2010-12-10 00:39 839It's so easy.Step1. Right click ... -
[Forward]Selected Design Patterns - Asynchronous Token
2010-12-05 00:32 960Consider an enterprise applic ... -
[Forward]Selected Design Patterns - Mediator
2010-12-04 23:51 916Almost any complex screen of ... -
[Forward]Selected Design Patterns - Proxy
2010-12-04 23:20 774A proxy is an object that re ... -
[Forward]Selected Design Patterns - Singleton
2010-12-04 23:06 771From http://oreilly.com ... -
Debugging with Google Chrome and Flash Player 10.1
2010-09-19 11:53 1035http://polygeek.com/2780_flex_d ... -
If the porgram is already running, close it before attempting to run.
2010-08-24 17:29 1752Launch Failed! If the program ... -
DataGrid选不中行
2010-07-15 17:01 777I have noticed a strange behavi ... -
Avoiding duplicate session detected errors in LCDS (and BlazeDS)
2010-07-01 18:34 1445Original article path:http://ww ... -
Server.Processing.DuplicateSessionDetected
2010-07-01 16:58 1948Earlier i faced one issue ... -
USING FLEX 3 ADVANCEDDATAGRID IN FLASHDEVELOP
2010-05-14 16:54 1352Those of you attempting to u ... -
[转记]AS3中的continue和break新用法
2010-04-06 20:25 2727AS3中的continue(continue [label]) ... -
BlazeDS & Hibernate lazy loading in n-tier arhitecture
2010-03-01 10:42 901原文链接:http://forum.springsource. ... -
连接FMS,Hello Hailin
2009-11-13 18:50 848FMS 虽然已经升级到3.5版本, 但是仍然是支持AS1.5, ... -
从Flex3到Flex4的转变一览(样式应用、主题等)
2009-11-13 11:43 4587文章转载http://devilkirin.iteye.com ... -
关于flex事件的讲解
2009-09-20 17:03 1061文章来自:http://www.riachina.com/sh ...
相关推荐
Design Patterns - Elements of Reusable Object-Oriented Software [English] Design Patterns 英文版 带书签 解压密码:123456
这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...
Design Patterns-Elements of Reusable Object-Oriented Software 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源...
《设计模式 - 可复用面向对象软件的基础》是一本由Erich Gamma、Richard Helm、Ralph Johnson和John Vlissides合著的经典著作,通常被称为“Gang of Four”(GoF)的设计模式书籍。这本书是面向对象设计的重要参考...
Design Patterns-Elements of Reusable Object-Oriented Software + 源代碼
JAVA设计模式一直是JAVA最考验内功的技术点。有句话说的很好,理解吃透设计模式概念如果是3分的难度,那么自己能写出来就是10分的...java-design-patterns-master是github上比较优秀的设计模式项目,这里与大家分享!
Kasampalis -- Mastering Python Design Patterns -- 2015 -- code.7z
本书设计实例从面向对象的设计中精选出23个设计模式,总结了面向对象设计中最有价值的经验,并且用简洁可复用的形式表达出来。本书分类描述了一组设计良好,表达清楚的软件设计模式,这些模式在实用环境下有特别有用...
head first design patterns-head first 设计模式的英文原版;高清英文原版,非扫描
With Learning JavaScript Design Patterns, you’ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to...
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
Bible for C++ software engineers! All basic design patterns presentation. English version Thank you for download!!!
《Head First设计模式》是一本深受开发者喜爱的设计模式入门书籍,其官方源码库"Head-First-Design-Patterns-master.zip"包含了书中所讲解的各种设计模式的实际代码示例,旨在帮助读者更深入地理解并应用这些模式。...
《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...
"Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...
b站李建忠讲的C/C+设计模式的ppt, ... 设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。
又一本设计模式神书本书共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。 ★前言介绍了这本书的用法 ★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract ...