├── Abstract Factory ├── flutter_example_1.dart └── flutter_example_2.dart ├── Adapter └── adapter.dart ├── Factory Method ├── dart_example.dart └── flutter_example.dart ├── Prototype ├── prototype_immutable_classes.dart └── prototype_mutable_classes.dart └── Singleton ├── singleton.dart └── singleton_in_abstract_factory.dart /Abstract Factory/flutter_example_1.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:playground/platform_button.dart'; 3 | import 'package:playground/platform_indicator.dart'; 4 | 5 | // Approach 1 6 | // PlatformButton and PlatformIndicator created using Factory Method Pattern. 7 | 8 | abstract class AbstractFactory { 9 | Widget buildButton(BuildContext context, String text, VoidCallback onPressed); 10 | Widget buildIndicator(BuildContext context); 11 | } 12 | 13 | class AbstractFactoryImpl implements AbstractFactory { 14 | @override 15 | Widget buildButton(BuildContext context, String text, VoidCallback onPressed) { 16 | return PlatformButton(Theme.of(context).platform).build( 17 | onPressed, 18 | Text(text), 19 | ); 20 | } 21 | 22 | @override 23 | Widget buildIndicator(BuildContext context) { 24 | return PlatformIndicator(Theme.of(context).platform).build(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Abstract Factory/flutter_example_2.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:playground/platform_button.dart'; 3 | import 'package:playground/platform_indicator.dart'; 4 | 5 | // Approach 2 6 | // PlatformButton and PlatformIndicator are created using Factory Method Pattern. 7 | 8 | class AbstractFactoryImpl { 9 | static Widget buildButton(BuildContext context, String text, VoidCallback onPressed) { 10 | return PlatformButton(Theme.of(context).platform).build( 11 | onPressed, 12 | Text(text), 13 | ); 14 | } 15 | 16 | static Widget buildIndicator(BuildContext context) { 17 | return PlatformIndicator(Theme.of(context).platform).build(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Adapter/adapter.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | 3 | class PostAPI1 { 4 | String getYouTubePosts() { 5 | return ''' 6 | [ 7 | { 8 | "title": "Automatic Code Generation with Flutter", 9 | "description": "Generate automatically" 10 | }, 11 | { 12 | "title": "Twitter Clone with Flutter", 13 | "description": "Clones" 14 | } 15 | ] 16 | '''; 17 | } 18 | } 19 | 20 | class PostAPI2 { 21 | String getMediumPosts() { 22 | return ''' 23 | [ 24 | { 25 | "header": "Aasasas", 26 | "bio": "Gwqweqwe" 27 | }, 28 | { 29 | "header": "Twitter Clone with Flutter", 30 | "bio": "Clones" 31 | } 32 | ] 33 | '''; 34 | } 35 | } 36 | 37 | abstract class IPostAPI { 38 | List getPosts(); 39 | } 40 | 41 | class Post { 42 | final String title; 43 | final String bio; 44 | Post({ 45 | required this.title, 46 | required this.bio, 47 | }); 48 | } 49 | 50 | // ADAPTER 51 | class PostAPI1Adapter implements IPostAPI { 52 | final api = PostAPI1(); 53 | @override 54 | List getPosts() { 55 | final data = jsonDecode(api.getYouTubePosts()) as List; 56 | return data 57 | .map( 58 | (e) => Post( 59 | title: e['title'], 60 | bio: e['description'], 61 | ), 62 | ) 63 | .toList(); 64 | } 65 | } 66 | 67 | class PostAPI2Adapter implements IPostAPI { 68 | final api = PostAPI2(); 69 | @override 70 | List getPosts() { 71 | final data = jsonDecode(api.getMediumPosts()) as List; 72 | return data 73 | .map( 74 | (e) => Post( 75 | title: e['header'], 76 | bio: e['bio'], 77 | ), 78 | ) 79 | .toList(); 80 | } 81 | } 82 | 83 | class PostAPI implements IPostAPI { 84 | final api1 = PostAPI1Adapter(); 85 | final api2 = PostAPI2Adapter(); 86 | @override 87 | List getPosts() { 88 | return api1.getPosts() + api2.getPosts(); 89 | } 90 | } 91 | 92 | // Usage - final PostAPI postAPI = PostAPI(); 93 | // postApi.getPosts(); 94 | -------------------------------------------------------------------------------- /Factory Method/dart_example.dart: -------------------------------------------------------------------------------- 1 | // DART EXAMPLE 2 | 3 | enum EmployeeType { 4 | programmer, 5 | boss, 6 | hr, 7 | } 8 | 9 | abstract class Employee { 10 | void work(); 11 | // 1st Approach 12 | factory Employee(EmployeeType type) { 13 | switch (type) { 14 | case EmployeeType.programmer: 15 | return Programmer(); 16 | case EmployeeType.hr: 17 | return HRManager(); 18 | case EmployeeType.boss: 19 | return Boss(); 20 | default: 21 | return Programmer(); 22 | } 23 | } 24 | } 25 | 26 | class Programmer implements Employee { 27 | @override 28 | void work() { 29 | print('coding an app'); 30 | } 31 | } 32 | 33 | class HRManager implements Employee { 34 | @override 35 | void work() { 36 | print('recruiting people'); 37 | } 38 | } 39 | 40 | class Boss implements Employee { 41 | @override 42 | void work() { 43 | print('leading the people'); 44 | } 45 | } 46 | 47 | // Alternate Approach 48 | class FactoryMethod { 49 | static Employee getEmployee(String type) { 50 | switch (type) { 51 | case 'programmer': 52 | return Programmer(); 53 | case 'hr': 54 | return HRManager(); 55 | case 'boss': 56 | return Boss(); 57 | default: 58 | return Programmer(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Factory Method/flutter_example.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/cupertino.dart'; 2 | import 'package:flutter/material.dart'; 3 | 4 | abstract class PlatformButton { 5 | Widget build(VoidCallback onPressed, Widget child); 6 | 7 | factory PlatformButton(TargetPlatform platform) { 8 | switch (platform) { 9 | case TargetPlatform.android: 10 | return AndroidButton(); 11 | case TargetPlatform.iOS: 12 | return IOSButton(); 13 | default: 14 | return AndroidButton(); 15 | } 16 | } 17 | } 18 | 19 | class AndroidButton implements PlatformButton { 20 | @override 21 | Widget build(VoidCallback onPressed, Widget child) { 22 | return ElevatedButton( 23 | onPressed: onPressed, 24 | child: child, 25 | ); 26 | } 27 | } 28 | 29 | class IOSButton implements PlatformButton { 30 | @override 31 | Widget build(VoidCallback onPressed, Widget child) { 32 | return CupertinoButton.filled( 33 | onPressed: onPressed, 34 | child: child, 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Prototype/prototype_immutable_classes.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | 3 | @immutable 4 | class Person { 5 | final String name; 6 | final String lastName; 7 | final int age; 8 | final String nation; 9 | final String email; 10 | const Person({ 11 | required this.name, 12 | required this.lastName, 13 | required this.age, 14 | required this.nation, 15 | required this.email, 16 | }); 17 | 18 | Person copyWith({ 19 | String? name, 20 | String? lastName, 21 | int? age, 22 | String? nation, 23 | String? email, 24 | }) { 25 | return Person( 26 | name: name ?? this.name, 27 | lastName: lastName ?? this.lastName, 28 | age: age ?? this.age, 29 | nation: nation ?? this.nation, 30 | email: email ?? this.email, 31 | ); 32 | } 33 | 34 | Person clone() => copyWith(name: name, lastName: lastName, age: age, nation: nation, email: email); 35 | } 36 | 37 | /* 38 | Person person = const Person( 39 | name: 'Rivaan', 40 | lastName: 'Ranawat', 41 | age: 17, 42 | nation: 'IN', 43 | email: '', 44 | ); 45 | Person person1 = person.clone(); 46 | */ 47 | -------------------------------------------------------------------------------- /Prototype/prototype_mutable_classes.dart: -------------------------------------------------------------------------------- 1 | class Person { 2 | String name; 3 | String lastName; 4 | int age; 5 | String nation; 6 | String email; 7 | Person({ 8 | required this.name, 9 | required this.lastName, 10 | required this.age, 11 | required this.nation, 12 | required this.email, 13 | }); 14 | Person clone() => Person(name: name, lastName: lastName, age: age, nation: nation, email: email); 15 | } 16 | 17 | /* 18 | Person person = Person( 19 | name: 'Rivaan', 20 | lastName: 'Ranawat', 21 | age: 17, 22 | nation: 'IN', 23 | email: '', 24 | ); 25 | Person person1 = person.clone(); 26 | */ 27 | -------------------------------------------------------------------------------- /Singleton/singleton.dart: -------------------------------------------------------------------------------- 1 | // Approach 1 -> Singleton1 singleton = Singleton1.getInstance(); 2 | class Singleton1 { 3 | static Singleton1? _instance; // instance of this class 4 | 5 | Singleton1._internal() { 6 | print('PRIVATE CONSTRUCTOR RAN'); 7 | } 8 | 9 | static Singleton1 getInstance() { 10 | _instance ??= Singleton1._internal(); 11 | return _instance!; 12 | } 13 | } 14 | 15 | // Approach 2 -> Singleton2 singleton = Singleton2.instance; 16 | class Singleton2 { 17 | static Singleton2? _instance; // instance of this class 18 | 19 | Singleton2._internal() { 20 | print('PRIVATE CONSTRUCTOR RAN'); 21 | } 22 | 23 | static get instance { 24 | _instance ??= Singleton2._internal(); 25 | return _instance!; 26 | } 27 | } 28 | 29 | // Approach 3 -> Singleton3 singleton = Singleton3(); 30 | class Singleton3 { 31 | static Singleton3? _instance; // instance of this class 32 | 33 | Singleton3._internal() { 34 | print('PRIVATE CONSTRUCTOR RAN'); 35 | } 36 | 37 | factory Singleton3() { 38 | _instance ??= Singleton3._internal(); 39 | return _instance!; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Singleton/singleton_in_abstract_factory.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:playground/platform_button.dart'; 3 | import 'package:playground/platform_indicator.dart'; 4 | 5 | // PlatformButton and PlatformIndicator created using Factory Method Pattern. 6 | abstract class AbstractFactory { 7 | Widget buildButton(BuildContext context, String text, VoidCallback onPressed); 8 | Widget buildIndicator(BuildContext context); 9 | } 10 | 11 | class AbstractFactoryImpl implements AbstractFactory { 12 | // This is Singleton 👇 13 | static AbstractFactoryImpl? _instance; 14 | AbstractFactoryImpl._internal(); 15 | 16 | static AbstractFactoryImpl get instance { 17 | _instance ??= AbstractFactoryImpl._internal(); 18 | return _instance!; 19 | } 20 | 21 | @override 22 | Widget buildButton(BuildContext context, String text, VoidCallback onPressed) { 23 | return PlatformButton(Theme.of(context).platform).build( 24 | onPressed, 25 | Text(text), 26 | ); 27 | } 28 | 29 | @override 30 | Widget buildIndicator(BuildContext context) { 31 | return PlatformIndicator(Theme.of(context).platform).build(); 32 | } 33 | } 34 | --------------------------------------------------------------------------------