Skip to content

Instantly share code, notes, and snippets.

@kwalrath
Forked from miquelbeltran/main.dart
Created March 11, 2021 18:37
Show Gist options
  • Save kwalrath/75fa871ed532225710fa537d983e775e to your computer and use it in GitHub Desktop.
Save kwalrath/75fa871ed532225710fa537d983e775e to your computer and use it in GitHub Desktop.
Java-to-Dart codelab: CircleMock example
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);
}
class CircleMock implements Circle {
num area = 0;
num radius = 0;
}
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