Skip to main content
added 127 characters in body
Source Link
Ray
  • 49
  • 2
  • 5

I understand that having circular dependency can be bad design. However, I have a question regarding a certain class structure.

As an example:

ocean.ts

import {Boat} from './boat'; export class Ocean { boats: Array<Boat> = []; getWaterLevel() { return 5; } createBoats() { for (let i = 0; i < 10; i++) { const boat = new Boat(); boat.ocean = this; boat.engineRunning = true; this.boats.push(boat); } } } 

boat.ts

import {Ocean} from './ocean'; export class Boat { engineRunning: boolean; ocean: Ocean; canMove() { return this.ocean.getWaterLevel() > 5 && this.engineRunning; } } 

In Typescript this can't be done without a circular reference problem from the imports. I've also read people conclude that its a sign of bad design. The only other solution I could see is to create a third layer which is something like OceanBoat and manage the two resources. Is this bad design or a bad limitation of Typescript? Are there any better solutions for handling this without merging files into one or creating a abstract layer like "OceanBoat"? Is there anything like require_once() for Typescript?

I understand that having circular dependency can be bad design. However, I have a question regarding a certain class structure.

As an example:

ocean.ts

import {Boat} from './boat'; export class Ocean { boats: Array<Boat> = []; getWaterLevel() { return 5; } createBoats() { for (let i = 0; i < 10; i++) { const boat = new Boat(); boat.ocean = this; boat.engineRunning = true; this.boats.push(boat); } } } 

boat.ts

import {Ocean} from './ocean'; export class Boat { engineRunning: boolean; ocean: Ocean; canMove() { return this.ocean.getWaterLevel() > 5 && this.engineRunning; } } 

In Typescript this can't be done without a circular reference problem from the imports. I've also read people conclude that its a sign of bad design. The only other solution I could see is to create a third layer which is something like OceanBoat and manage the two resources. Is this bad design or a bad limitation of Typescript?

I understand that having circular dependency can be bad design. However, I have a question regarding a certain class structure.

As an example:

ocean.ts

import {Boat} from './boat'; export class Ocean { boats: Array<Boat> = []; getWaterLevel() { return 5; } createBoats() { for (let i = 0; i < 10; i++) { const boat = new Boat(); boat.ocean = this; boat.engineRunning = true; this.boats.push(boat); } } } 

boat.ts

import {Ocean} from './ocean'; export class Boat { engineRunning: boolean; ocean: Ocean; canMove() { return this.ocean.getWaterLevel() > 5 && this.engineRunning; } } 

In Typescript this can't be done without a circular reference problem from the imports. I've also read people conclude that its a sign of bad design. The only other solution I could see is to create a third layer which is something like OceanBoat and manage the two resources. Is this bad design or a bad limitation of Typescript? Are there any better solutions for handling this without merging files into one or creating a abstract layer like "OceanBoat"? Is there anything like require_once() for Typescript?

Source Link
Ray
  • 49
  • 2
  • 5

Is circular reference with Typescript array properties bad design?

I understand that having circular dependency can be bad design. However, I have a question regarding a certain class structure.

As an example:

ocean.ts

import {Boat} from './boat'; export class Ocean { boats: Array<Boat> = []; getWaterLevel() { return 5; } createBoats() { for (let i = 0; i < 10; i++) { const boat = new Boat(); boat.ocean = this; boat.engineRunning = true; this.boats.push(boat); } } } 

boat.ts

import {Ocean} from './ocean'; export class Boat { engineRunning: boolean; ocean: Ocean; canMove() { return this.ocean.getWaterLevel() > 5 && this.engineRunning; } } 

In Typescript this can't be done without a circular reference problem from the imports. I've also read people conclude that its a sign of bad design. The only other solution I could see is to create a third layer which is something like OceanBoat and manage the two resources. Is this bad design or a bad limitation of Typescript?