Thursday, June 3, 2010

Eager Initialization Pattern

As name suggests Eager Initialization approach creates/initialize an object with anticipation, regardless of whether or not it will be required in program.

Definition: The Eager Initialization pattern describes how run-time acquisition and initialization of resources can be made predictable and fast by eagerly acquiring and initializing resources before their actual usage.

Eager initialization pattern at first glance seems an un-efficient pattern, but in situation where application is required to be reliable and predictable with no downtime this pattern comes handy. As and when an object/variable is declared or when the object in which it lives is created [declaration or constructor] object/variable is initialized.

Eager initialization approach should only be used

  • When initialization of the instance variable would consume large amount of time and downtime of application is not acceptable.
  • In pooling solution where it will require pre-acquire a number of resources in order to quickly serve first requests.
  • A Hamster application. Like hamster which acquires as many fruits as possible before eating them in its cave, Hamster application will acquires as many resources before it’s actually execute application.

The benefits of this approach include:

  • Increase in predictability of application as user requests for resource acquisition are intercepted and served instantly.
  • Application response time is very low as resources are pre acquired.
  • Assurance of the variables been initialized before they are used.

The Consequences of this approach include:

  • As all the resources are eagerly acquired at the start of the application, the start-up time for the application is longer.
  • Eager Initialization might initialize many objects upfront, which may not be required by system. This may introduce unnecessary exhaustion or overuse of memory resources.
  • Management of eagerly initialized object becomes an important aspect as not all the resources might require immediately in application.

No comments:

Post a Comment