samedi 14 mars 2020

Testing open/closed principles design pattern

I was wondering how I can test following open/closed design pattern

import Foundation

protocol Shape {
  var area: Double {get}
}

struct Reactangle: Shape {
  let width: Int
  let height: Int

  var area: Double {
    return Double(width * height)
  }
}

struct Circle: Shape {
  let radius: Int

  var area: Double {
    return Double(radius * radius) * Double.pi
  }
}

class AreaCalculator {
  func calculateArea(shape: Shape) -> Double {
    return shape.area
  }
}


var circle = Circle(radius: 22)
var reactangle = Reactangle(width: 15, height: 22)

var area = AreaCalculator()
area.calculateArea(shape:circle)

I am getting following error:

Expressions are not allowed at the top level Result of call to 'calculateArea(shape:)' is unused

Aucun commentaire:

Enregistrer un commentaire