Skip to content

Instantly share code, notes, and snippets.

@Sfshaza
Last active May 3, 2018 23:13
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 Sfshaza/db7b77005d61f01264f9b8938e4ac4ee to your computer and use it in GitHub Desktop.
Save Sfshaza/db7b77005d61f01264f9b8938e4ac4ee to your computer and use it in GitHub Desktop.
Java-to-Dart codelab: Top-level factory function
import 'dart:math';
abstract class Shape {
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);
}
Shape shapeFactory(String type) {
if (type == 'circle') return new Circle(2);
if (type == 'square') return new Square(2);
throw 'Can\'t create $type.';
}
main() {
var circle = shapeFactory('circle');
var square = shapeFactory('square');
print(circle.area);
print(square.area);
}
@kwalrath
Copy link

kwalrath commented May 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment