Skip to content

Instantly share code, notes, and snippets.

@marti1125
Created January 21, 2019 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marti1125/6c9c9a5f96f0d29dd31d8ae6942ad6d9 to your computer and use it in GitHub Desktop.
Save marti1125/6c9c9a5f96f0d29dd31d8ae6942ad6d9 to your computer and use it in GitHub Desktop.
Shape
import 'dart:math';
abstract class Shape {
factory Shape(String type) {
if (type == 'circle') return Circle(2);
if (type == 'square') return Square(2);
throw 'Can\'t create $type.';
}
num get area;
}
class Circle implements Shape {
final num radius;
Circle(this.radius);
num get area => pi * pow(radius, 2);
}
class Square implements Shape {
final num side;
Square(this.side);
num get area => pow(side, 2);
}
main() {
final circle = Shape('circle');
final square = Shape('square');
print(circle.area);
print(square.area);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment