Created
September 21, 2021 07:43
-
-
Save LittleFireflies/964f0d524f96a19a28d6fb4777237cf8 to your computer and use it in GitHub Desktop.
SOLID in Dart
This file contains 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
class Repository { | |
String getData() { | |
return dataSource(); | |
} | |
String dataSource() { | |
return 'data from data source'; | |
} | |
} | |
void main() { | |
final repo = Repository(); | |
print(repo.dataSource()); | |
} |
This file contains 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
class DataSource { | |
String getData() { | |
return 'SRP - data from data source'; | |
} | |
} | |
class Repository { | |
final DataSource dataSource; | |
Repository(this.dataSource); | |
String getData() { | |
return dataSource.getData(); | |
} | |
} | |
void main() { | |
final dataSource = DataSource(); | |
final repo = Repository(dataSource); | |
print(repo.getData()); | |
} |
This file contains 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
enum Shipping { JNE, JNT } | |
class Product { | |
num calculateShippingPrice(int price, Shipping shipping) { | |
switch (shipping) { | |
case Shipping.JNE: | |
return 12 / 100 * price; | |
case Shipping.JNT: | |
return 20 / 100 * price; | |
} | |
} | |
} | |
void main() { | |
final product = Product(); | |
final price = product.calculateShippingPrice(10000, Shipping.JNE); | |
print(price); | |
} |
This file contains 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
abstract class Shipping { | |
num calculateShippingPrice(int price); | |
} | |
class JNE implements Shipping { | |
@override | |
num calculateShippingPrice(int price) { | |
return 12 / 100 * price; | |
} | |
} | |
class JNT implements Shipping { | |
@override | |
num calculateShippingPrice(int price) { | |
return 20 / 100 * price; | |
} | |
} | |
class Product { | |
num calculateShippingPrice(int price, Shipping shipping) { | |
return shipping.calculateShippingPrice(price); | |
} | |
} | |
void main() { | |
final product = Product(); | |
final price = product.calculateShippingPrice(10000, JNE()); | |
print(price); | |
} |
This file contains 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
abstract class Product { | |
late String name; | |
late DateTime expiredDate; | |
} | |
class Banana implements Product { | |
@override | |
String name = 'Sweet Banana'; | |
@override | |
DateTime expiredDate = DateTime.now(); | |
} | |
class Shoes implements Product { | |
@override | |
String name = 'Air Jordan'; | |
@override | |
DateTime expiredDate = DateTime.now(); // Expired Date?? | |
} | |
void main() { | |
final banana = Banana(); | |
print(banana.name); | |
print(banana.expiredDate); | |
} |
This file contains 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
abstract class Product { | |
late String name; | |
} | |
abstract class FoodProduct extends Product { | |
late DateTime expiredDate; | |
} | |
class Banana implements FoodProduct { | |
@override | |
String name = 'Sweet Banana'; | |
@override | |
DateTime expiredDate = DateTime.now(); | |
} | |
class Shoes implements Product { | |
@override | |
String name = 'Air Jordan'; | |
} | |
void main() { | |
final banana = Banana(); | |
print(banana.name); | |
print(banana.expiredDate); | |
final shoes = Shoes(); | |
print(shoes.name); | |
} |
This file contains 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
abstract class Payment { | |
late String paymentName; | |
late int noCard; | |
} | |
class Bri implements Payment { | |
@override | |
String paymentName = 'BRI'; | |
@override | |
int noCard = 12345; | |
} | |
class Ovo implements Payment { | |
@override | |
String paymentName = 'OVO'; | |
@override | |
int noCard = 4312121; // no card?? | |
} | |
void main() { | |
final ovo = Ovo(); | |
print(ovo.paymentName); | |
final bri = Bri(); | |
print(bri.paymentName); | |
} |
This file contains 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
abstract class Payment { | |
late String paymentName; | |
} | |
abstract class Bank extends Payment { | |
late int noCard; | |
} | |
abstract class EWallet extends Payment { | |
late String phoneNumber; | |
} | |
class Bri implements Bank { | |
@override | |
String paymentName = 'BRI'; | |
@override | |
int noCard = 12345; | |
} | |
class Ovo implements EWallet { | |
@override | |
String paymentName = 'OVO'; | |
@override | |
String phoneNumber = '081333333333'; | |
} | |
void main() { | |
final ovo = Ovo(); | |
print(ovo.paymentName); | |
final bri = Bri(); | |
print(bri.paymentName); | |
} |
This file contains 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
class Database { | |
String create() { | |
return 'Create Database'; | |
} | |
String read() { | |
return "Read Database"; | |
} | |
} | |
class Controller { | |
final database = Database(); | |
String createData() => database.create(); | |
String readData() => database.read(); | |
} | |
void main() { | |
final controller = Controller(); | |
final result = controller.readData(); | |
print(result); | |
} |
This file contains 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
abstract class Database { | |
String create(); | |
String read(); | |
} | |
class MySQL implements Database { | |
@override | |
String create() { | |
return 'MySQL Create Database'; | |
} | |
@override | |
String read() { | |
return 'MySQL Read Database'; | |
} | |
} | |
class MongoDB implements Database { | |
@override | |
String create() { | |
return 'MongoDB Create Database'; | |
} | |
@override | |
String read() { | |
return 'MongoDB Read Database'; | |
} | |
} | |
class Controller { | |
final Database database; | |
Controller(this.database); | |
String createData() => database.create(); | |
String readData() => database.read(); | |
} | |
void main() { | |
final controller = Controller(MySQL()); | |
final result = controller.readData(); | |
print(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment