Created
July 7, 2020 09:23
-
-
Save bubnenkoff/3d4537824c88dc65220c08ca40713069 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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