Lazy ain’t Always Bad…

You can fire up Microsoft Word, type something up quick, save (and/or publish) then close the app down, in a couple of minutes. Only the relatively quick splash screen would force you to wait as the program loads before you can start writing.

As a developer, you should (as a rule) make your software as quick as possible, and one of the more popular tactics to do this is by delaying to load an object until you actually need it. You can see this approach in Microsoft Word: Behind the splash screen, the app loads only the necessary UI (the document view, the ribbon, etc.) and after everything’s good, shows it the user. Since the user doesn’t always need, say, the font dialog box, then it isn’t initially loaded. A click in the right button however, would load the form on the fly and show it to the user. This concept is called Lazy Initialization.

Design Pattern: Lazy Initialization
Pattern Type: Creational
First Stated in: Patterns of Enterprise Application Architecture
Description: Lazy Initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed
    public class DataManager
{
private Server _ServerOne = null;
public Server ServerOne
{
get
{
if (_ServerOne == null)
this._ServerOne = new Server();
return this._ServerOne;
}
}
}

As you can see from the code above, first it checks if the object has already been initialized. If it is, then it hands over the already live object, but if not- then it initializes the object on the fly before handing it over. This means that the second time the object is needed, it would be quicker since you don’t have to initialize it again.

Now, before I hit publish on this post, I just want to say that the topic is not, in any way, connected to why there wasn’t a blog post yesterday =P I'll try to get two posts up today to make up for it~

0 comments:

Post a Comment