Home > Software design >  Instantiate a class array within another class - Dart language
Instantiate a class array within another class - Dart language

Time:12-15

I created a Wall class that contains the height and width of a wall, and I need to create a list of four Wall classes inside the Room class, how could I do that? I didn't want to use List would there be another way? Sorry, I'm new to OOP programming.

void main() {
  
  Wall wall = Wall();
  
  print(wall.totalArea);
  wall.height = 4;
  wall.width = 4;
  wall.getAreaTotal();
  print(wall.totalArea);
}

class Room {
 
}


class Wall {
  double height;
  double width;
  late double totalArea;
  
  Wall({
     this.height = 1,
     this.width = 1,
  }) {
    getAreaTotal();
  }
  
  void getAreaTotal(){
    totalArea = height * width;
  }
}

CodePudding user response:

The room has four walls. Probably, if we assume a rectangular room, otherwise things get more complicated.

The most general approach is to not assume four walls, and just say that the room has a list of walls.

class Wall {
  final double height;
  final double width;
  Wall({required this.height, required this.width});
  double get totalArea => height * width;
}
class Room {
  final List<Wall> walls;
  Room(Iterable<Wall> walls) : walls = List.unmodifable(walls);
}

void main() {
  var room = Room([
    Wall(width: 4, height: 4),
    Wall(width: 2, height: 4),
    Wall(width: 4, height: 4),
    Wall(width: 2, height: 4),
  ]);
  var wallArea = 0.0;
  for (var wall in room.walls) wallArea  = totalArea;
}

That allows any combination of walls, for your basic L-shaped rooms with six walls, or heptagonal rooms for style.

In some cases, you can make things easier by making assumptions. Like a rectangular room always have identical opposing walls, and same height everywhere.

Then you can do:

class RectangularRoom implements Room {
  final double length1;
  final double length2;
  final double height;
  final List<Room> rooms;
  RectangularRoom({
      required this.length1, 
      required this.length2, 
      required this.height}) 
      : rooms = _createWalls(length1, length2, height);
  static List<Room> _createWalls(
      double length1, double length2, double height) {
    var wall1 = Wall(length: length1, height: height);
    var wall2 = Wall(length: length2, height: height);
    return List.unmodifiable([wall1, wall2, wall1, wall2]);
  }
}

Or you can assume the room is oriented in some way:

// Also assumed rectangular.
class OrientedRoom extends Room {
  final Wall northWall;
  final Wall westWall;
  final Wall southWall => northWall;
  final Wall eastWall => westWall;
  OrientedRoom({
      required double length1, 
      required double length2, 
      required double height
  }) : this._(Wall(length: length1, height: height), 
              Wall(length: length2, height: height));

  OrientedRoom._(this.northWall, this.westWall) 
      : super([northWall, westWall, northWall, westWall]);
}

That allows you to name the individual walls, and have a class field for each.

So many options. What is right for you depends on your use-case, what you will use the objects for.

CodePudding user response:

Like @Dimon said in comment, you can manage it by adding a property for each Wall you want in your Room.

Something like this :

class Room {
  final Wall leftWall;
  final Wall rightWall;
  final Wall topWall;
  final Wall bottomWall;

  Room(this.leftWall, this.rightWall, this.topWall, this.bottomWall);

}

And creating your Room as follow :

var room = Room(Wall(height: 15, width: 20), Wall(height: 15, width: 8),
        Wall(height: 15, width: 3), Wall(height: 15, width: 22));
  •  Tags:  
  • dart
  • Related