Skip to content

Difference between Using init() and configure()

thofrey edited this page Apr 1, 2014 · 4 revisions

Table of Contents

  1. init()
  2. configure()

init()

As you may know, it has become a best practice to define an init() method within CFC's to handle any setup or configuration process required by the component.

Within a Mach-II application, each of your model components should employ this technique of defining and implementing an init() method, as your model should always be framework agnostic and not at all coupled with Mach-II. This allows great flexibility and total reuse of the model if part or all of the application's presentation and/or controller layer were ever to drastically change (to a Flex front end for example). For this reason, all your model objects including service layer objects, DAO's, gateways, value objects (beans), and any other objects in your model will indeed define and implement an init() method.

However, the controller layer of a Mach-II application is indeed aware of the Mach-II framework, as each controller layer component (Filters, Plugins, Properties, and Listeners) gains its unique and powerful functionality by extending a superclass which is built into Mach-II itself.

Component Parent Parent
Filters extend MachII.framework.EventFilter extend MachII.framework.BaseComponent
Plugins extend MachII.framework.Plugin extend MachII.framework.BaseComponent
Properties extend MachII.framework.Property extend MachII.framework.BaseComponent
Listeners extend MachII.framework.Listener extend MachII.framework.BaseComponent

The superclasses (above) for each of these controller layer components already define and use the init() method for framework related processes. As a result, developer defined controller layer objects should NOT implement an init() method, as doing so would override the init() method defined in the parent class.

configure()

Even though controller layer objects should not implement and override their parent's init() method, there still may be the need for configuration setup or logic in the component. As its name would suggest, the configure() method serves this role and is automatically called on application start-up to load an instance of the component into application memory. This way, the object will be available wherever it is needed throughout the Mach-II application. To be clear, the configure() method is required in each of the controller layer components outlined above, and can be used to perform any necessary setup for the component. It is worth nothing, however, that even though the configure() method is required, it will sometimes not need to perform any logic whatsoever (as illustrated below).

    <cffunction name="configure" access="public" returntype="void" output="true"
        displayname="Listener Constructor">

        <!--- I initialize this Listener object as part of the framework startup. --->
    </cffunction>
Clone this wiki locally