Skip to content

Instantly share code, notes, and snippets.

@bubnenkoff
Created July 7, 2020 09:23
Show Gist options
  • Save bubnenkoff/3d4537824c88dc65220c08ca40713069 to your computer and use it in GitHub Desktop.
Save bubnenkoff/3d4537824c88dc65220c08ca40713069 to your computer and use it in GitHub Desktop.
main() {
Map json = jsonDecode('{"id": 1, "price": 100, "type": "fruits", "color": "red"}');
ProductFactory productFactory = ProductFactory();
productFactory.MakeFactory(json);
}
class ProductFactory {
MakeFactory(Map json)
{
if (json['type'] == 'bottles')
{
return Bottles(json);
}
if (json['type'] == 'fruits')
{
return Apples(json);
}
}
}
abstract class CurrentProduct
{
int id;
num price;
CurrentProduct(Map json)
{
this.id = json['id'];
this.price = json['price'];
}
}
class Bottles extends CurrentProduct {
String content;
Bottles(Map json): super(json)
{
this.content = json['content'];
}
}
class Apples extends CurrentProduct {
String color;
Apples(Map json): super(json)
{
this.color = json['color'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment