1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    27
    Vote Rating
    0
    sudhanshubest is on a distinguished road

      0  

    Default lazy vs direct initialization

    lazy vs direct initialization


    Hi ,

    I just want to know when we should use lazy instantiation and what are the pros and cons of lazy
    instantiation.

    Thanks,
    sudhanshu

  2. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,590
    Vote Rating
    322
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    This is a very involved subject and it's quite difficult to summarize in a brief forum post.

    Lazy initialization is widely misunderstood. Personally I don't like the term as it's misleading, there's not really any such thing. Consider this example:

    Code:
    Ext.create('Ext.container.Container', {
        items: [
            Ext.create('Ext.button.Button')
        ]
    });
    The button is instantiated first and it is then passed to the container as part of its config. Now consider the xtype version:

    Code:
    Ext.create('Ext.container.Container', {
        items: [{
            xtype: 'button'
        }]
    });
    Here the button is instantiated during the instantiation of the container. The end result is almost exactly the same but the order the objects are created is slightly different.

    So in the examples above the 'laziness' is only fleeting, it doesn't really make any difference. However, using xtype configs often makes code much easier to read so generally I'd advise using it wherever possible.

    Sometimes it isn't possible to use xtypes, such as in this example:

    Code:
    Ext.create('Ext.window.Window', {
        autoShow: true,
        ...
    });
    Floating component like a windows usually aren't added to a container so you need to instantiate them yourself.

    There are concrete examples where the xtype approach has real benefits beyond tidy code. I wouldn't call them 'lazy instantiation' though, it's just config manipulation.

    Consider this class:

    Code:
    Ext.define('MyClass', {
        extend: 'Ext.panel.Panel',
    
        initComponent: function() {
            this.items = [Ext.create('Ext.button.Button', ...)];
            this.callParent();
        }
    });
    The problem with this class is that subclasses can't change the items. The solution looks like this:

    Code:
    Ext.define('MyClass', {
        extend: 'Ext.panel.Panel',
    
        initComponent: function() {
            this.items = this.createItems();
            this.callParent();
        },
    
        createItems: function() {
            return [Ext.create('Ext.button.Button', ...)];
        }
    });
    Now a subclass can override createItems to change the items. There is still a problem though. If the subclass wants to change the button it can prove difficult. Say we want to change it from a button to a splitbutton, we'd have to create a totally new button in the subclass. Instead we could implement it this way:

    Code:
    Ext.define('MyClass', {
        extend: 'Ext.panel.Panel',
    
        initComponent: function() {
            this.items = this.createItems();
            this.callParent();
        },
    
        createItems: function() {
            return [{
                xtype: 'button',
                ...
            }];
        }
    });
    With this version we can now override createItems in a subclass and change the config before the button is instantiated. It makes it much easier to change arbitrary config options, even the xtype. xtypes provide a way of making the class of an object a config option just like any other. It's a very technique powerful but try not to think of it as 'lazy instantiation', that'll just skew your understanding of what's really going on.

    Some further reading:

    http://skirtlesden.com/articles/conf...-the-prototype

Thread Participants: 1