- 浏览: 153944 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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
Almost any complex screen of a business application consists of a number of containers and components. The era of developers being responsible for both functionality and visuals is coming to an end, and a large portion of the enterprise RIA is created in a collaboration between professional UI designers and developers.
Typically, a UI designer gives you a UI wireframe that he puts together using one of the design tools. In the best-case scenario, the UI designer knows how to use Flash Builder in the design mode or even uses Adobe Flash Catalyst to autogenerate MXML for the UI. But even in this case, you, the developer, will need to rip this code apart and decide what components to build to create this view and how they are going to communicate with each other—you need to refactor the code.
Let’s see how you can arrange communication between custom Flex components. The diagram in Figure 2-4 consists of a number of nested components and containers that are numbered for easier reference.
For simplicity and better abstraction, this example does not use the actual components, like panels and drop-downs, but you can extrapolate this image onto the wireframe of the actual view you are about to start developing.
A simple (but wrong) approach is to just put all these components in one container (number 1 in Figure 2-4), program the business logic and communications among these components, and be done with it. This would produce a monolithic application with tightly coupled components that know about each other and where removal of one component would lead to multiple code changes in the application. Talk about strings attached!
The better approach is to create loosely coupled custom components that are self-contained, do not know about one another’s existence, and can communicate with the “outside world” by sending and receiving events.
Adobe Flex was designed for creating event-driven applications, and it has a good component model, allowing you to create custom components if need be. But after custom components are designed, they need to communicate with each other. This section covers the use of the Mediator design pattern as it applies to UIs created with Flex.
Think of a single Lego from a Lego toy set. Now, some kid (i.e., the mediator) may decide to use that Lego piece to build a house. Tomorrow, the mediator may decide to use that same Lego piece in a boat.
In the diagram from Figure 2-4 , containers play the role of the mediators. The top-level mediator is the container marked as 1, which is responsible for making sure that the components 2, 3, and 6 can communicate if need be. On the other hand, the number 2 is a mediator for 4 and 5. The number 3 is the mediator for 7 and 8.
Being a mediator is a very honorable mission, but it comes with responsibilities. The mediator must listen for events from one of the Lego parts and possibly fire an event on the other one(s).
For example, if you are building an online store, the number 6 can be a component where you select an item to purchase, the number 4 can be the button named Add to Shopping Cart, and the number 5 can be a shopping cart.
Let’s forget about the number 6 for a moment and examine the content of the mediator, number 2. It contains the button 4, which has a specific look and feel and can do just one thing—broadcast a custom event called AddItemClicked. To whom? To whomever’s interested in receiving such an event. So expect to have the line:
dispatchEvent(new Event("AddItemClicked"))
somewhere inside the code of the component 4.
Because mediator number 2 is interested in receiving this event from number 4, it will define an event listener for such an event, which will receive the event and in turn will dispatch another event right on the number 5:
addEventListener("AddItemClicked", addItemClickedEventHandler)
...
private function addItemClickedEventHandler ():void {
Number5.dispatchEvent(new Event("Add2ShoppingCart"));
}
In this pseudocode, the mediator is choreographing the show by defining how its components will communicate.
We’d like to stress that in the previous example, the number 4 is like shooting an event up into the sky—anyone who wants to can listen. On the other hand, the number 5 is just sitting quietly and listening to the incoming event. From whom? It has no idea. This is what loose coupling of components means. The number 4 mediator does not know about the number 5, but they talk anyway through the mediator.
But as a developer of this screen, you have to take care of mediator-to-mediator communications as well. For instance, if the number 6 is a widget where you can select your Sony TV, the mediator 1 will be notified about it and need to talk to the mediator 2, which in turn will arrange the flow between 4 and 5.
Let’s build a concrete example showing how to build these components and establish their communication using the Mediator design pattern. This is an oversimplified trading screen to buy/sell equities at the stock market. This application will have price and order panels. In the real world, the price panel would get an external feed about the prices and deals for all securities that are being traded on the market.
The web designer might give you the two screenshots shown in Figures 2-5 and 2-6 (we hope that your designer has better artistic talent than we do).
This is a pretty simple window. You will design it as two components that communicate with each other without having any knowledge about each other. The Flex application will play role of the mediator here. When the user sees the right price to buy or sell IBM shares, she clicks on the bid or ask price; this action will create a custom event with the current data from the price panel bid and ask prices, the stock symbol, and whether this is a request to buy or sell.
In brokerage, bid means the highest price that the trader is willing to pay for the stock or other financial product, and ask is the lowest price the seller is willing to accept.
Example 2-8 shows the PricePanel component. It has three public variables—symbol, bid, and ask. When the trader clicks on one of the numbers in the price panel, the code creates an instance of the custom event of the type OrderEvent.PREPARE_ORDER_EVENT, and all public variables and the name of the requested operation are nicely packaged inside of this event. Then the PricePanel component dispatches this event. To whom? It has no idea.
Example 2-8. PricePanel.mxml
And Example 2-9 shows the definition of the custom OrderEvent. In this version, it declares several variables for storing the order data, but the section on data transfer objects simplifies this event a little bit. Please note that this event defines two event types. The OrderEvent of the type PREPARE_ORDER_EVENT is being sent by the PricePanel; the mediator receives it and forwards it to the OrderPanel as PLACE_ORDER_EVENT. Example 2-9. OrderEvent.as The OrderPanel shown in Example 2-10 listens to the event of the OrderEvent.PLACE_ORDER_EVENT type. When this event arrives (this panel has no idea from whom), the OrderPanel populates the fields with the order data extracted from the event object. Example 2-10. OrderPanel.mxml Here comes the mediator (Example 2-11), which includes two components— PricePanel and OrderPanel. The mediator listens to the event from the PricePanel and forwards it to the OrderPanel in the function orderEventHandler. Example 2-11. A test application: Trading1.mxml Once again, components don’t know about one another and can be reused in another context, too.
The mediator is one of the most useful patterns for any programming environment that includes components communicating with each other—even more so if you program in an event-driven environment such as Flex. Use this pattern before implementing the UI design. Identify your mediators and custom reusable components and decide what events these components will broadcast or listen to. After you have made all these decisions, select the format of the data that will travel between the components. This is where the data transfer pattern comes into the picture.
<?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="{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="{bid}" fontSize="22" fontStyle="normal" fontWeight="bold"
click="placeOrder(true)" editable="false" />
<mx:Label x="154" y="31" text="Ask" fontWeight="bold" fontSize="14"/> <mx:TextArea x="118" y="49" width="109" height="47"
backgroundColor="#A2BFF4" text="{ask}" fontSize="22" fontStyle="normal" fontWeight="bold" click="placeOrder(false)" editable="false"/> <mx:Script>
<![CDATA[
import com.farata.events.OrderEvent;
public var symbol:String;
[Bindable]
public var bid:String;
[Bindable]
public var ask:String;
// Dispatch the OrderEvent to be picked by a Mediator<
private function placeOrder(buy:Boolean):void {
dispatchEvent(new OrderEvent(OrderEvent.PREPARE_ORDER_EVENT,symbol,bid,ask,buy));
}
]]> </mx:Script>
</mx:Canvas>
package com.farata.events{
import flash.events.Event; public class OrderEvent extends Event {
public var symbol:String; public var bid:String; public var ask:String; public var buy:Boolean; public var eventType:String;
public static const PREPARE_ORDER_EVENT:String ="OrderEvent"; public static const PLACE_ORDER_EVENT:String="PlaceOrderEvent";
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
width="230" height="100" backgroundColor=quot;#4CF3D2" creationComplete=
"this.addEventListener(OrderEvent.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"/>
<mx:Button id="go" x="147" y="7" label="GO!" height="60" width="74" fontSize="22" click="placeOrder()" enabled="false"/>
<mx:Button x="148" y="75" label="Cancel" width="72"
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import com.farata.events.OrderEvent;
private function orderEventHandler(evt:OrderEvent) {
go.enabled=true;
sym.text=evt.symbol;
operation.text=evt.buy?"Buy":"Sell";
price.text=operation.text=="Buy"?evt.bid:evt.ask;
qty.setFocus();
}
private function placeOrder():void{
Alert.show(operation.text + " " + qty.text +
" shares of " + sym.text +
" at" + price.text + " per share", "Placing order");
// call a remote service to place this order
}
private function cancelOrder():void{
sym.text="";
operation.text="";
price.text="";
go.enabled=false;
}
]]>
</mx:Script>
</mx:Canvas>
<?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(OrderEvent.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:PricePanel symbol="IBM" bid="117.45" ask="117.48" y="31" x="7"/>
<comp:OrderPanel id="ordPanel" x="245" y="30"/>
<mx:Script> <![CDATA[ import mx.controls.Alert;
import com.farata.events.OrderEvent;
private function orderEventHandler(evt:OrderEvent):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: OrderEvent= new
OrderEvent(OrderEvent.PLACE_ORDER_EVENT,
evt.symbol, evt.bid, evt.ask, evt.buy);
ordPanel.dispatchEvent(orderEvt);
}
]]>
</mx:Script>
</mx:Application>
发表评论
-
js调用flash的方法时报错:Error calling method on NPObject!
2012-12-27 15:53 3127如题; uncaught exception: Error ... -
AIR 2.6 NativeProcess is not supported
2012-03-28 11:46 1871使用Flex AIR2.6开发桌面程序,添加程序自己重启或启动 ... -
AIR application killed when check camera device
2011-07-25 19:16 741I met this problem and spen ... -
UIComponent中的parentDocument和parent
2011-06-26 17:26 1573ParentAndParentDocument.mxml ... -
[Forward]How to compile CSS file in Flash Builder
2010-12-10 00:39 835It's so easy.Step1. Right click ... -
[Forward]Selected Design Patterns - Asynchronous Token
2010-12-05 00:32 955Consider an enterprise applic ... -
[Forward]Selected Design Patterns - Data Transfer Object
2010-12-05 00:18 928Data transfer objects are als ... -
[Forward]Selected Design Patterns - Proxy
2010-12-04 23:20 771A proxy is an object that re ... -
[Forward]Selected Design Patterns - Singleton
2010-12-04 23:06 767From http://oreilly.com ... -
Debugging with Google Chrome and Flash Player 10.1
2010-09-19 11:53 1028http://polygeek.com/2780_flex_d ... -
If the porgram is already running, close it before attempting to run.
2010-08-24 17:29 1747Launch Failed! If the program ... -
DataGrid选不中行
2010-07-15 17:01 773I have noticed a strange behavi ... -
Avoiding duplicate session detected errors in LCDS (and BlazeDS)
2010-07-01 18:34 1440Original article path:http://ww ... -
Server.Processing.DuplicateSessionDetected
2010-07-01 16:58 1940Earlier i faced one issue ... -
USING FLEX 3 ADVANCEDDATAGRID IN FLASHDEVELOP
2010-05-14 16:54 1347Those of you attempting to u ... -
[转记]AS3中的continue和break新用法
2010-04-06 20:25 2723AS3中的continue(continue [label]) ... -
BlazeDS & Hibernate lazy loading in n-tier arhitecture
2010-03-01 10:42 899原文链接:http://forum.springsource. ... -
连接FMS,Hello Hailin
2009-11-13 18:50 844FMS 虽然已经升级到3.5版本, 但是仍然是支持AS1.5, ... -
从Flex3到Flex4的转变一览(样式应用、主题等)
2009-11-13 11:43 4580文章转载http://devilkirin.iteye.com ... -
关于flex事件的讲解
2009-09-20 17:03 1059文章来自:http://www.riachina.com/sh ...
相关推荐
这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...
Kasampalis -- Mastering Python Design Patterns -- 2015 -- code.7z
JAVA设计模式一直是JAVA最考验内功的技术点。有句话说的很好,理解吃透设计模式概念如果是3分的难度,那么自己能写出来就是10分的...java-design-patterns-master是github上比较优秀的设计模式项目,这里与大家分享!
Design Patterns - Elements of Reusable Object-Oriented Software [English] Design Patterns 英文版 带书签 解压密码:123456
head first design patterns-head first 设计模式的英文原版;高清英文原版,非扫描
Design Patterns-Elements of Reusable Object-Oriented Software 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源...
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
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...
《Head First设计模式》是一本深受开发者喜爱的设计模式入门书籍,其官方源码库"Head-First-Design-Patterns-master.zip"包含了书中所讲解的各种设计模式的实际代码示例,旨在帮助读者更深入地理解并应用这些模式。...
"Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...
Design Patterns-Elements of Reusable Object-Oriented Software + 源代碼
b站李建忠讲的C/C+设计模式的ppt, ... 设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。
《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...
Leverage the power of Python design patterns to solve real-world problems in software architecture and design
Design the core areas of the Azure Execution Model Work with storage and data management Create a health endpoint monitoring pattern Automate early detection of anomalies Identify and secure ...
"php设计模式-designpatterns-php.zip"这个压缩包很可能包含了关于如何在PHP项目中应用设计模式的资料,特别是针对"designpatterns-php-master"这个文件名,我们可以推测这可能是一个关于PHP设计模式的开源项目或...
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...