Accessibility

Table of Contents

Architecting Flex Applications

Loose Coupling of Application Components

Loose coupling of components is another proven best practice in object-oriented development. Loose coupling is a programming technique that consists in avoiding interdependencies between classes as much as possible. Loose coupling increases the reusability of your components throughout and across your applications.

The basic idea is that the more a component knows about other components, the less reusable that component is. In other words, if component A has a strong reference (a typed variable) to component B, you can only reuse component A along with component B. This kind of dependency is generally undesirable.

You can often avoid direct references to other components by using event notifications between components. For example, the CartView class has a Checkout button that allows the user to initiate the checkout process. However, the CartView class has no knowledge of the Checkout class. When the user clicks the Checkout button, the CartView object dispatches a "checkout" event (line 25 in the CartView.mxml example above). The Flex Store application is registered as a listener for that event and displays CheckoutView (line 21 in the flexstore.mxml example above).

Similarly, when you click an item in the product catalog, details for the selected product display in the Product Details View. However, the ThumbnailView class (ThumbnailView.mxml) has no reference to the ProductDetail class (ProductDetail.mxml). When you click a product, the ThumbnailView object dispatches an item Selected event. The Flex Store application is registered as a listener for that event and makes its selectedItem instance variable point to the clicked product (line 14 in Flex Store.mxml above). Because the productData attribute of the ProductDetail object is bound to selectedItem, the details for the clicked product automatically appear in the ProductDetail component.