I currently have a lengthy code in a class that I would like to organize into separate files.
I am considering two ideas: using Mixin and utilizing static methods.
For instance,
class myController {
routeSubView(type: SubViewType, params: any) {
switch(type) {
case SubViewType.A:
this._showA(params);
break;
case SubViewType.B:
this._showB(params);
break;
case SubViewType.C:
this._showC(params);
break;
case SubViewType.D:
this._showD(params);
break;
// ... many more cases
}
}
private _showA() {
// initialize view and render
}
private _showB() {
// initialize view and render
}
private _showC() {
// initialize view and render
}
private _showD() {
// initialize view and render
}
// ... many more methods
}
#idea1 ) Refactor sub-view generation into a static class
### sub_views.ts
class SubViews {
static showA(params: any) {
// initialize view and render
}
static showB(params: any) {
// initialize view and render
}
}
### my_controller.ts
import { SubViews } from './sub_views';
class myController {
routeSubView(type: SubViewType, params: any) {
switch(type) {
case SubViewType.A:
SubViews::showA();
break;
case SubViewType.B:
SubViews::showB();
break;
case SubViewType.C:
SubViews::showC();
break;
case SubViewType.D:
SubViews::showD();
break;
// ... many more cases
}
}
}
#idea2) Implementing Mixin
### mixin.ts
export interface ISubviews {
_showA(params: any): any;
_showB(params: any): any;
_showC(params: any): any;
_showD(params: any): any;
}
export function _showA(param: any){
// initialize view and render
}
export function _showB(param: any){
// initialize view and render
}
export function _showC(param: any){
// initialize view and render
}
export function _showD(param: any){
// initialize view and render
}
### my_controller.ts
import * as { Mixin } from './mixin';
class myController implement Mixin.ISubviews {
_showA(params: any): any;
_showB(params: any): any;
_showC(params: any): any;
_showD(params: any);
/// ...
}
Cocktail.mixin(myController, Mixin);
Which idea do you think is better? Or do you have another suggestion? Your advice is appreciated.