Structure of classes is:
/* Common class */
interface Tree {
}
class TreeBase {
// Commmon methods for all successors
}
Concrete classes:
interface TreeLayersActions { do(): void; }
interface TreeMapActions { make(): void; }
TreeLayers extends TreeBase implements TreeMapActions {
}
TreeMap extends TreeBase implements TreeLayersActions{
}
So, I try to inject concrete implementation to component:
export class TreeComponent implements OnInit, OnDestroy, TreeLayersActions, TreeMapActions {
constructor(private tree: TreeBase) {
}
make(): void {
this.tree.getNodeStyles(node); // Here problem
}
do(): void {}
}
Problem is that getNodeStyles
is not presented in private tree: TreeBase
. Because it is concrete implementation.
I have tried this:
constructor(private tree: TreeLayers | TreeMap) {
}
Component should work with different service implementations to deligate execution to service.
I have decided to create a service that create a specific tree instance and implements all interfaces from all concrete classes:
class TreeService implements TreeLayerActions, TreeMapActions {
public tree: TreeLayers | TreeSearch;
constructor(type: string) {
switch (type) {
case "layers":
this.tree = new TreeLayers(null);
break;
case "map":
this.tree = new TreeSearch(null);
}
}
/* Realization interfaces TreeLayerActions and TreeMapActions that deligates calls to service
changeTileLayer(treeNode: TreeNode): void {
this.tree.changeTileLayer(treeNode: TreeNode);
}
// Other methods
}
So, then in component I can inject the service TreeService
and implement also TreeLayerActions, TreeMapActions, Tree
interfaces, that deligates calls to service.
I think it is dirty solution, because I pollute the component by all methods from each implementations.