PDA

View Full Version : strategy pattern implementation



skyboyer
17 Sep 2012, 10:26 PM
i would like to implement strategy patter but i have no idea how to do this.
Let there is one class(controller for example) and there are dozes of conditional flow "if(some field of model) <do this> else if <do this>" and so on. So i have extracted all such flows in separated methods and now i am stuck. what i need it is to make some conditional mixing — so i could be able to move all methods in different mixins and apply them in init. But mixins are applied unconditionally(almost statically — at the moment of class' definition). So as for now i have only variant with Ext.Object.apply(this, new <more suitable mixin>) but as for me it looks ugly.
Is there some other approach?

skyboyer
19 Sep 2012, 3:00 AM
it has been implemented with Ext.apply and it looks like this:

applyMixins: function() {
var toApply = {};
Ext.apply(toApply, new MainViewMixins.DefaultMixin());
if (<some condition>) {
Ext.apply(toApply, new MainViewMixins.SomeConditionMixin());
}
if(<some condition 2>) {
Ext.apply(toApply, new MainViewMixins.SomeMixinIf());
} else {
Ext.apply(toApply, new MainViewMixins.SomeMixinElse());
}
Ext.applyIf(this, toApply);
}
that is executed while initComponent is invoking
apply and additional object are used because there are same-named methods in different mixins. And applyIf is used to avoid applying such properties like $base and super.