Introduction

Flash is a lightweight cross-platform runtime for rich media, enterprise applications and mobile applications, as well as an integrated development environment. Flash can be programmed in ActionScript 1/2/3.

Archive for the ‘Flash’ Category

Wednesday, May 24th, 2006

Xml Dialect for Flash User Interfaces

cheap cialis pill certified cialis cheap viagra in canada cialis buy drug buy generic cialis viagra buy 25mg viagra cheap viagra without prescription buy cheapest viagra on line purchase viagra cialis 10mg buying generic viagra cialis pills viagra from india cheapest sildenafil citrate cheap cialis no rx viagra india cialis bangkok viagra for order buy sildenafil internet buy generic viagra online buying cialis online where to order cialis tablet cialis find cialis no prescription required viagra cheap drug order cialis cheap online online pharmacy cialis cialis no rx order generic cialis price of cialis viagra soft drug viagra cheap viagra from uk order cialis no prescription order cheap viagra viagra drug order cheap cialis cheap cialis pharmacy best price for viagra cheap viagra from usa cost cialis cialis overnight shipping cheapest generic cialis online generic viagra online online viagra viagra sales cheap cialis in canada compare cialis prices online cialis online drug viagra online purchase discount cialis without prescription no rx viagra cialis overnight viagra uk cialis order cheap cialis from usa buying cialis cialis overnight delivery cialis in bangkok buy and purchase sildenafil online impotence treatment cheap price viagra viagra sale cheap cialis tablet drug cialis generic cialis online cheap viagra pharmacy find discount cialis online viagra malaysia cialis without a prescription buy cialis online cheap viagra rx buy no rx viagra cialis 20mg viagra in malaysia discount viagra online buy sildenafil cheap buy viagra low price buy cialis cialis cheap price cialis cheap generic viagra cialis canada low cost viagra buy cheap viagra cialis vs viagra order cialis from us cialis tablets find no rx cialis buy generic cialis online buy viagra overnight delivery cheapest cialis price buy cheapest cialis on line order cialis in canada viagra tablet viagra no online prescription find cheap cialis online viagra price order viagra no prescription cheap generic cialis buy viagra online cheap cialis uk cialis without rx generic cialis cheap viagra vs cialis order cialis on internet viagra tablets viagra purchase impotence drugs buy cialis generic cialis tablet cialis cheapest price order viagra from canada viagra generic cheap viagra from canada order cialis compare viagra prices online find cheap cialis impotence cure pfizer viagra find discount cialis cheapest cialis buy cialis from india impotence buy cheapest viagra online cialis side effects viagra order discount cialis online cialis in malaysia cialis in uk viagra in uk cialis online without prescription cialis online pharmacy order viagra buy viagra online viagra side effects cialis sale discount cialis no rx cheapest viagra find cialis order cialis no rx buy cialis low price buy viagra cheap drug cialis online purchase order discount viagra online 50 mg viagra 100 mg viagra 10mg cialis cost of cialis cheapest cialis prices buy discount viagra online cialis sales 50mg viagra cialis price buy viagra on internet cialis pill cheapest cialis online purchase viagra overnight delivery cheap cialis from canada cheapest viagra price cialis 20 mg buy sildenafil low cost order viagra without prescription buy viagra lowest price no prescription cialis order viagra on internet discount cialis overnight delivery cialis cheap drug viagra approved viagra no rx required compare viagra prices no rx cialis cheap cialis on internet buy viagra from india buy discount cialis online viagra pharmacy online order viagra from us cialis free delivery cialis for order buy cialis from canada viagra without rx viagra online review 10 mg cialis cheap viagra no rx cheapest viagra prices viagra prices cialis pharmacy order no rx cialis buy cialis in us buy cialis no prescription required order cialis from canada lowest price cialis cheap cialis internet online pharmacy viagra cheapest generic cialis generic drugs cialis india find cialis without prescription best price cialis buy viagra without prescription cheap cialis in uk where to buy viagra 20 mg cialis cheap cialis from uk buy sildenafil canada cialis no rx required cialis in us buy cialis overnight delivery cialis cheap price order cheap viagra online 20mg cialis buy cheap viagra online viagra internet viagra without prescription free cialis buy cialis us cialis buy buy viagra in canada order viagra cheap online find viagra without prescription viagra pills cheap cialis no prescription viagra online without prescription order generic viagra cialis discount viagra cheapest price purchase viagra no rx viagra no rx viagra cheap discount viagra overnight delivery sale cialis cialis pharmacy online purchase cialis without prescription pharmacy online cialis medication discount viagra buy cheap cialis impotence medication viagra medication find cialis on internet impotence pills cialis prices discount viagra without prescription cialis online cheap cialis online review find cheap viagra online buy viagra us purchase cialis online certified viagra where to order viagra buy cheapest viagra buy cialis internet order cialis online buy sildenafil online buy cialis cheap cheap viagra purchase cialis find discount viagra buy cialis on internet cialis buy online buy sildenafil online without a prescription viagra buy online order cheap cialis online viagra information no prescription viagra cost of viagra buy cialis in canada buy cialis online buy viagra cheapest generic viagra cialis us cialis australia fda approved cialis lowest price for viagra viagra bangkok cialis prescription cialis cost buy no rx cialis buy viagra internet viagra discount order viagra overnight delivery generic cialis viagra australia 25 mg viagra order viagra online viagra overnight cialis rx order cialis in us order viagra no rx order discount cialis online viagra vendors order viagra in us buy sildenafil in uk viagra us buy generic viagra viagra canada viagra no prescription viagra cheap price cheap viagra tablet viagra free delivery overnight viagra purchase viagra online find cheap viagra cialis malaysia best price viagra cialis free sample find viagra on internet cialis generic buy sildenafil in canada order cialis no prescription required cheapest viagra online purchase cialis no rx viagra in us order discount cialis cheap viagra internet free viagra cialis approved best price for cialis cialis from india find no rx viagra generic viagra viagra from canada viagra online pharmacy buy viagra from canada cheapest generic viagra online buy cheapest cialis discount cialis viagra overnight delivery cialis without prescription 100mg viagra cialis in australia price of viagra order cialis overnight delivery cheap viagra in uk buying generic cialis viagra pill buy cialis on line low cost cialis find discount viagra online buying viagra cheap cialis overnight delivery pharmacy cialis cheap viagra pill viagra prescription find viagra online buy cialis lowest price discount viagra no rx online cialis viagra free sample cheap viagra in usa find viagra cheap viagra online buy viagra no rx generic viagra cheap buy cialis without prescription buy viagra in us cheap viagra overnight delivery cheap cialis in usa cheap cialis online viagra order no rx viagra viagra soft tab find cialis online lowest price viagra cialis drug cialis vendors viagra online stores erectile dysfunction order viagra in canada buy viagra on line viagra overnight shipping viagra online cheap lowest price for cialis approved viagra pharmacy cialis 10 mg cialis no online prescription cialis purchase cialis from canada order cialis without prescription viagra for sale viagra in australia approved cialis pharmacy buy viagra generic buy sildenafil in spain find viagra no prescription required cialis no prescription buy viagra from us order viagra no prescription required cost viagra purchase viagra without prescription buy cialis no rx cialis cheap cialis internet tablet viagra cheap viagra on internet viagra cost pharmacy viagra cialis soft tab cialis information buy cheap cialis internet purchase cialis overnight delivery cheap cialis without prescription buy viagra no prescription required compare cialis prices buy cheap cialis online overnight cialis where to buy cialis cheap cialis buy cheap viagra internet buy discount cialis viagra buy drug cheap viagra no prescription buy sildenafil citrate buying viagra online buy discount viagra fda approved viagra cialis online stores cheap cialis tablets buy cheapest cialis online cheap viagra tablets order discount viagra sale viagra viagra online cialis for sale cialis soft viagra pharmacy buy cialis from us viagra without a prescription viagra in bangkok

As mentioned in my previous article, the IoC container for Flash supports two XML dialects: a simple one to wire up business objects, and another less verbose one to build user interfaces (with any user interface framework or component library, including in-house ones, that follow some common coding practices).

Following is the ErrorWindow.xml from the As2lib Chat sample application:

  1. <beans xmlns:chat="org.as2lib.sample.chat" xmlns:aw="org.aswing">
  2.    
  3.     <chat:ErrorWindow id="errorWindow" style="window">
  4.         <messageLabel>r{messageLabel}</messageLabel>
  5.         <aw:JPanel style="title">
  6.             <aw:JLabel>{messageSource.message[title.error]}</aw:JLabel>
  7.         </aw:JPanel>
  8.         <aw:Container style="content">
  9.             <aw:JLabel id="messageLabel"/>
  10.             <aw:JButton actionListener="d{errorWindow.hide}">
  11.                 <text>{messageSource.message[button.ok]}</text>
  12.             </aw:JButton>
  13.         </aw:Container>
  14.     </chat:ErrorWindow>
  15.    
  16. </beans>

Namespaces are declared for packages that contain needed components: namespace ‘chat’ is mapped to package ‘org.as2lib.sample.chat’ and ‘aw’ to ‘org.aswing’.

Beans can now be declared as elements with one of the declared namespaces and the class name as local name: <chat:ErrorWindow …/> or <aw:JPanel …/>

Properties can either be elements with the element name as property name (<messageLabel>…</messageLabel>) or attributes (actionListener=”…”).
Note that there are some reserved attributes like ‘id’, ’style’ and ‘class’. If a property has the name of one of these reserved attributes, it must be declared with an element.
You may have noticed that not all inner bean definitions are wrapped by a property element. For example the ‘JLabel’ and ‘JButton’ beans inside the ‘Container’ bean. So to which property of the ‘Container’ bean are they set? To the default property! If you parse the ErrorWindow.xml with the ‘AsWingBeanDefinitionParser’ the default property is ‘append’ which fits most needs. If you just use the ‘UiBeanDefinitionParser’ there is no default property, but you may specify it with the ‘default-property’ attribute in the root element as global default-property and/or in any bean definition as local default-property.

Property values with one of the following patterns are interpreted in a special way:
* p{} or {} is a property get access: {messageSource.message[title.error]} is interpreted as messageSource.getMessage(”title.error”)
* r{} is a bean reference: r{messageLabel}
* d{} creates a delegate: d{errorWindow.hide}
* v{} retrieves a variable: v{org.aswing.WindowLayout.TITLE}
* m{} invokes a method

The reserved ’style’ attribute maps a bean definition to a style class in a style sheet. Here’s an excerpt from the style.css file of the As2lib Chat (how it is loaded was shortly mentioned in the previous article):

  1. @namespace {
  2.     aw: org.aswing;
  3.     ab: org.aswing.border;
  4. }
  5. .window {
  6.     x: 200;
  7.     y: 150;
  8.     width: 400;
  9.     height: 110;
  10.     resizable: false;
  11.     modal: true;
  12. }
  13. .window .content {
  14.     layout: aw|SoftBoxLayout(axis=v[org.aswing.SoftBoxLayout.Y_AXIS], gap=5);
  15.     border: ab|EmptyBorder(insets=aw|Insets(5, 5, 5, 5));
  16. }
  17. .window .content JPanel {
  18.     layout: aw|SoftBoxLayout(axis=v[org.aswing.SoftBoxLayout.X_AXIS], gap=5);
  19. }

The ‘.window’ style class is quite simple. It just declares the position and size and whether the window is resizable and modal.

The ‘.content’ style class is a little more complex. It declares the layout and border of the window’s content, which are both instances in AsWing.

A bean (instance) can be declared with a namespace to specify the package (optional) and a class name followed by left and right parenthesis: aw|SoftBoxLayout(…). Between the parenthesis are either constructor argument or property definitions separated by commas: aw|Insets(5, 5, 5, 5) or aw|SoftBoxLayout(axis=v[org.aswing.SoftBoxLayout.Y_AXIS], gap=5) respectively. Constructor argument definitions are simple values; property definitions consist of the property name followed by ‘=’ and the property value.

As you can see in the property definition example, it is also possible to retrieve constants: v[org.aswing.SoftBoxLayout.Y_AXIS]

Applications mostly have multiple screens and windows. Defining all these in a single file would be unmaintainable. Every screen and window is normally defined in its own file. For example the error window is needed by the chat screen to show errors to the users. The chat screen can import the error window as follows:

  1. <beansxmlns="*.xml">
  2.    
  3.     <chat:Chat id="chat" init-method="init" style="frame">
  4.         <errorWindow>r{errorWindow}</errorWindow>
  5.         …
  6.     </chat:Chat>
  7.    
  8.     …
  9.    
  10.     <ErrorWindow id="errorWindow" class="org.as2lib.context.support.AsWingApplicationContext"/>
  11.    
  12. </beans>

Declare a namespace for importing external definitions: xmlns=”*.xml”
Declare an imported bean as any other bean with the import namespace and the file name, and the class of the context which shall manage the imported bean: <ErrorWindow id=”errorWindow” class=”org.as2lib.context.support.AsWingApplicationContext”/>
You can give the imported bean an ‘id’ to reference it: <errorWindow>r{errorWindow}</errorWindow>
You may also declare properties for the imported bean: <Chat width=”800″ height=”600″ visible=”true” …/>

The use cases explained above are the most common ones; they probably fit 90% of all needs. I’m going to explain more special use cases in the next article.

Download the As2lib Chat sample application with all needed libraries: As2lib Chat

Monday, May 22nd, 2006

IoC Container for Flash

After taking a closer look at dependency injection and discussing some theory about inversion of control containers in the previous articles, we are now going to examine the As2lib Inversion of Control Container which is largely inspired by the Spring Framework.

Note that I use the name bean to refer to application objects managed by bean factories (they may be defined by a bean definition and may provide setters and getters to access properties).

Bean Wrapper (org.as2lib.bean.BeanWrapper): Bean wrappers are used by bean factories to access properties on beans. They provide methods to check whether properties with a given name are readable and/or writable, to set property values or to get property values and to convert property values to required types.

Bean Factory (org.as2lib.bean.factory.BeanFactory): The bean factory is a central registry for application objects. It holds bean definitions identified by unique names. If a bean is requested the bean factory will either create a new bean on each request with the information provided by the bean definition (prototype beans) or create the bean once and return always the same shared bean (singleton beans). The bean factory invokes lifecycle callbacks on a bean during its creation and destruction; creation involves instantiating the bean, populating it (setting property values) and resolving bean references. Most bean factories also support bean post-processors which can modify new beans before they are returned.

Factory Bean (org.as2lib.bean.factory.FactoryBean): Factory beans can be used as any other bean in a bean factory; but the bean returned by a bean factory on a bean request is not the factory bean itself, but the bean it creates. This mechanism can be used to retrieve constants that shall be used as property values or to create typed proxies for remoting or web services.

Runtime Bean Reference (org.as2lib.bean.factory.config.RuntimeBeanReference): Placeholder holding information to resolve bean references at run-time (dependency injection).

Bean Definition Parser (org.as2lib.bean.factory.parser.BeanDefinitionParser): Parses bean definitions which may for example be described with a XML dialect.

Application Context (org.as2lib.context.ApplicationContext): Application contexts build on the functionalities provided by bean factories and add some new ones that are typically needed by applications. These additional functionalities are: message sources for internationalization, application events, aspect oriented programming support and process support to execute asynchronous processes on start-up.

Let’s take a look at the applicationContext.xml file of the As2lib Chat sample.

  1. <beans xmlns="*.xml">
  2.    
  3.     <bean class="org.as2lib.bean.factory.config.StyleSheetConfigurer">
  4.         <property name="styleSheetUri">style.css</property>
  5.     </bean>
  6.    
  7.     <bean id="smartFoxClient" class="it.gotoandplay.smartfoxserver.SmartFoxClient"/>
  8.    
  9.     <Login id="login" width="800" height="600" ip="127.0.0.1" port="9339" zone="simpleChat"
  10.             class="org.as2lib.context.support.AsWingApplicationContext">
  11.         <property name="smartFoxClient"><ref local="smartFoxClient"/></property>
  12.         <property name="chat"><ref local="chat"/></property>
  13.     </Login>
  14.    
  15.     <Chat id="chat" width="800" height="600" visible="true"
  16.             class="org.as2lib.context.support.AsWingApplicationContext">
  17.         <property name="smartFoxClient"><ref local="smartFoxClient"/></property>
  18.     </Chat>
  19.    
  20.     <bean id="localeManager" class="org.as2lib.lang.LocaleManager" factory-method="getInstance">
  21.         <property name="defaultLocale">en</property>
  22.         <property name="locales">
  23.             <array>
  24.                 <bean class="org.as2lib.lang.GermanLocale"/>
  25.                 <bean class="org.as2lib.lang.EnglishLocale"/>
  26.             </array>
  27.         </property>
  28.     </bean>
  29.    
  30.     <bean id="messageSource" class="org.as2lib.context.support.ResourceBundleMessageSource"
  31.             depends-on="localeManager">
  32.         <property name="baseName">messages</property>
  33.     </bean>
  34.    
  35. </beans>

Beans are defined with bean elements. Every bean element must at least provide the information needed to instantiate it. This is in the simplest case just a bean class. Properties are defined with property elements. They have a name attribute and optionally a type attribute to convert the property value to. The definition of the message source bean can be interpreted as (the depends-on attribute forces the locale manager to be instantiated prior to the message source):

  1. var messageSource:ResourceBundleMessageSource = new ResourceBundleMessageSource();
  2. messageSource.setBaseName("messages");

The instantiation can also be done by a per-class or per-instance factory method. This approach is used by the locale manager whose declaration can be interpreted as:

  1. var localeManager:LocaleManager = LocaleManager.getInstance();
  2. localeManager.setDefaultLocale("en");
  3. var locales:Array = new Array();
  4. locales.push(new GermanLocale());
  5. locales.push(new EnglishLocale());
  6. localeManager.addLocales(locales);

While beans can be defined with bean and property elements, such a notation is too verbose and inconvenient to use for defining whole user interfaces. There is thus also another XML dialect which you can see a glimpse of in the above example: The definition of the login and chat beans. We’ll take a closer look at this more convenient dialect in the next article. Let’s now think about what the beans are doing.

The message source loads properties files for the target locale with the base name “messages” on start-up: messages_de.properties, messages_en.properties, … . After the properties files have been loaded, the message source can be used to get internationalized messages by name (”title.welcome”, “button.submit”, … ).

The locale manager defines a default locale and a list of available locales. If the language of the operating system does not match one of the available locales, the default locale will be used as target locale, whose properties file is loaded by the message source.

The unnamed style sheet configurer loads the cascading style sheet style.css to format the user interface with.

The smart fox client is used to log-in, create rooms and send messages.

The two beans login and chat are user interface screens. Their definition is done in separate xml files: Login.xml and Chat.xml respectively; these files are loaded and parsed on start-up.

We now know what the bean definitions mean, but how can the application be started? The main.Mtasc class is responsible for starting the application in the As2lib Chat sample. It first specifies the parser to use, then creates a new loading application context with the parser and a bean definition URI, adds a listener to the application context to be notified if the application context has finished loading and at the end starts the loading process.

  1. var beanDefinitionParser:BeanDefinitionParser = new UiBeanDefinitionParser();
  2. applicationContext = new LoadingApplicationContext(APPLICATION_CONTEXT_URI, beanDefinitionParser);
  3. applicationContext.addListener(this);
  4. applicationContext.start();

As soon as the application context has finished loading and parsing the Mtasc class gets the login screen from the application context and shows it.

  1. try {
  2.     var login:Login = applicationContext.getBean("login");
  3.     login.show();
  4. }
  5. catch (exception) {
  6.     trace("Initializing chat failed with error:\n" + StringUtil.addSpaceIndent(exception.toString(), 2));
  7. }

The last question that remains is: How do we get the needed classes into the swf? This is actually quite problematic in Flash; every needed class must be referenced for example in the first frame which is quite inconvenient. When you are free to compile your application with MTASC things are much more convenient: use Ant build scripts in combination with As2ant to compile the application. With As2ant you have the option to specify the application context as XML source. The SWF task steps through the XML file and compiles all needed classes into the swf.

  1. <swf src="${as2lib.dir}/org/as2lib/app/conf/MtascApplication.as" dest="${chat.swf}"
  2.         header="800:600:31" version="8" trace="org.as2lib.env.log.logger.DebugItLogger.trace">
  3.     <classpath>
  4.         <pathelement path="${lib.dir}"/>
  5.         …
  6.     </classpath>
  7.     <srcxml>
  8.         <pathelement path="${context.dir}/applicationContext.xml"/>
  9.         …
  10.     </srcxml>
  11. </swf>

If you use Eclipse or another development environment with integrated Ant support, compiling everything requires just one click.

Download the As2lib Chat sample application with all needed libraries: As2lib Chat

As already mentioned previously, the XML dialect for user interfaces will be presented in the next article (but you can of course take a look at it in the As2lib Chat sample application if you can’t wait).

Saturday, May 20th, 2006

Inversion of Control Containers

An Inversion of Control container is an infrastructural backbone for applications by providing services like lifecycle management, lookup, configuration and dependency resolution.

Lifecycle management: The lifecycle of application objects is managed by the container. The container is able to create new objects, to populate them (applying property values), to invoke initialization and destruction callbacks.

Lookup: References to application objects can be obtained through the container. A container is so to speak a factory.

Configuration: Application objects can be configured in a consistent way (without needing recompilation); for example with an XML dialect.

Dependency resolution: The container should not only configure application objects with simple types, but also manage relationships between them.

While application objects should be able to take advantage of these services, they should NOT depend on the container. This can be achieved by Inversion of Control (and Dependency Injection). Inversion of Control containers are thus also called non-invasive containers.

IoC containers do not impose invasive dependencies on your code, but it is advisable to follow some good coding practices.

* Code to interfaces to maximize pluggability.
* Each application object should have a clear set of responsibilities.
* Favor the Strategy design pattern over inheritance.
* Do not write code to do anything that the container can do.
* Use method prefixes in a consistent manner (Mutator Methods: Naming)

If you follow the preceding recommendations, you can also easily unit test your code and practice test-driven development.

In the next article we are going to take a look at the As2lib IoC container for Flash and its simple XML dialect.

Friday, May 19th, 2006

Dependency Injection

Applications consist of multiple classes which collaborate with each other. For example an instance of class A needs an instance of class B and C to do its job; in other words, class A depends on class B and C; class B and C are thus dependencies of class A.

There are three options to satisfy dependencies:

  1. hard-code dependencies,
  2. look up dependencies,
  3. use the Dependency Injection pattern.

1. Hard-coding dependencies is the easiest strategy: class A simply instantiates its dependencies.

  1. class A {
  2.    
  3.     private var b:B;
  4.     private var c:C;
  5.    
  6.     public function A() {
  7.         b = new B(3, true);
  8.         c = new C();
  9.     }
  10.    
  11.     public function doSomething() {
  12.         var r:String = b.doSomething();
  13.         c.doSomething(r);
  14.     }
  15.    
  16. }

Problems of hard-coding dependencies:

  • Configuration is distributed across the whole application (configuration is in this case wiring up the instances and configuring dependent instances).
  • Class A must be modified to exchange a dependency (tight coupling): the instance of B may be replaced by an instance of SubB which fulfils the same contract as its super-class B. If B were an interface than any implementations could be used to satisfy the dependency.
  • What if a fourth class D has a dependency to the same instance of B as class A?

2. Looking up dependencies requires a context: class A gets the context and looks up its dependencies.

  1. class A {
  2.    
  3.     private var b:B;
  4.     private var c:C;
  5.    
  6.     public function A() {
  7.         var context:Context = Context.getInstance();
  8.         b = context.getB();
  9.         c = context.getC();
  10.     }
  11.    
  12.     …
  13.    
  14. }

Advantages of looking up dependencies:

  • Partially centralized configuration of the context: dependency b is already fully configured; but every instance looks up its dependencies on its own.
  • One instance can be used to satisfy multiple dependencies: an instance of class D can simply use context.getB() to get the same instance.

Problems of looking up dependencies:

  • Class A depends on a context: makes unit testing more difficult and the look ups are disturbing.
  • Code is bloated with dependency look-ups.

3. Using the Dependency Injection pattern is simple and places no requirements: class A provides ways to inject dependencies.

Advantages of dependency injection:

  • Configuration is centralized in one place: both configuration of instances and wiring up instances.
  • Dependencies can be exchanged easily (loose coupling).
  • Wiring up instances is made easy.

Problem of dependency injection: The wiring code may become rather complex if there are a lot of instances with a lot of dependencies to satisfy.

One way to go is to use Constructor Injection. In this approach class A declares a constructor with paramters for both dependencies; the dependencies are given on instantiation.

  1. class A {
  2.    
  3.     private var b:B;
  4.     private var c:C;
  5.    
  6.     public function A(b:B, c:C) {
  7.         this.b = b;
  8.         this.c = c;
  9.     }
  10.    
  11.     …
  12.    
  13. }

Advantages of constructor injection:

  • Dependencies cannot be changed after instantiation.
  • Initialization which requires all dependencies can be done directly in the constructor.
  • Instance is not in illegal state after instantiation.

Problems of constructor injection:

  • Parameter list of constructor may become very long (there may be a lot of dependencies, plus other information to pass to the constructor).
  • Parameters are distinguished by index and not by name, which may make the code harder to understand.

Another way to go is Setter Injection. In this approach class A provides setters for both dependencies; the dependencies are satisfied after instantiation.

  1. class A {
  2.    
  3.     private var b:B;
  4.     private var c:C;
  5.    
  6.     public function A() {
  7.     }
  8.    
  9.     public function setB(b:B):Void {
  10.         this.b = b;
  11.     }
  12.    
  13.     public function setC(c:C):Void {
  14.         this.c = c;
  15.     }
  16.    
  17.     …
  18.    
  19. }

Advantages of setter injection:

  • Constructor can be used to pass other information than dependencies.
  • Code is easier to understand because names rather than indices are used to distinguish dependencies.

Problems of setter injection:

  • Dependencies may not have been satisfied when needed.
  • Dependencies may be changed after ‘official’ initialization.
  • Initialization may have to be done after all dependencies are satisfied: init-method is needed.

While dependency injection is a pattern with many advantages, there are also some problems when used extensively, which can only be solved by an Inversion of Control Container. Such a container provides means of managing instances: lifecycle management (instantiating classes, setting dependencies, invoking init-methods, destroying classes), looking up managed instances, configuring managed instances, resolving dependencies.

In my following articles I’m going to present the As2lib Inversion of Control Container and show you how it solves the problems mentioned above and which other functionalities it provides (for example using its generic XML dialect to create UIs with ActionStep, AsWing, EnFlash or any other component library).

Further Reading: Inversion of Control Containers and the Dependency Injection pattern

Thursday, October 13th, 2005

As2lib 0.9.3, New Version

A new version of the as2lib is finally available for download.

Major changes are:

  • A completely rewritten File framework that offers various file format representations, like properties files, and support for loading and parsing these files - org.as2lib.io.file
  • Support for configuring the Logging framework via XML - org.as2lib.env.log.parser
  • Improved Process and Unit Testing frameworks - org.as2lib.app.exec, org.as2lib.test.unit
  • Many bug fixes in the AOP framework - org.as2lib.aop
  • New Regular Expression (RegExp) framework (note that there is still much documentation missing) - org.as2lib.regexp

As you can see, it is definitely worth updating to this new version.

Download As2lib 0.9.3.

View API Documentation

If you find any bugs, report it, if you want a new feature, tell us about it, if you need support, ask us: Response.

The next releases will contain a Bean Factory and Application Context framework and of course - a documented Regular Expression (RegEx) framework. ;)
So, stay tuned!