Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart Creational Design Patterns Added #44

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ cython_debug/

#OS
.DS_Store

#Dart
.dart_tool/
analysis_options.yaml
pubspec.lock
pubspec.yaml
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -983,8 +983,8 @@ class Lesson
{
public int id;
public string name;
public string price;
public string discountedPrice;
public double price;
public double discountedPrice;
public bool discountApplied;
public string lessonNote;
}
Expand Down
3 changes: 3 additions & 0 deletions abstract-factory/dart/command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class Command {
void executeCommand(String query);
}
4 changes: 4 additions & 0 deletions abstract-factory/dart/connection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
abstract class Connection {
bool openConnection();
bool closeConnection();
}
21 changes: 21 additions & 0 deletions abstract-factory/dart/custom_operation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'command.dart';
import 'connection.dart';
import 'i_database_factory.dart';

final class CustomOperation {
late final IDatabaseFactory _databaseFactory;
late final Connection _connection;
late final Command _command;

CustomOperation(IDatabaseFactory databaseFactory) {
_databaseFactory = databaseFactory;
_command = _databaseFactory.createCommand();
_connection = _databaseFactory.createConnection();
}

void removeById(int id) {
_connection.openConnection();
_command.executeCommand('DELETE ...');
_connection.closeConnection();
}
}
7 changes: 7 additions & 0 deletions abstract-factory/dart/i_database_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'command.dart';
import 'connection.dart';

abstract class IDatabaseFactory {
Connection createConnection();
Command createCommand();
}
7 changes: 7 additions & 0 deletions abstract-factory/dart/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'custom_operation.dart';
import 'oracle_database_factory.dart';

void main() {
final CustomOperation customOperation = CustomOperation(const OracleDatabaseFactory());
customOperation.removeById(1);
}
10 changes: 10 additions & 0 deletions abstract-factory/dart/mysql_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'command.dart';

final class MysqlCommand implements Command {
const MysqlCommand();

@override
void executeCommand(String query) {
print('MysqlCommand: $query');
}
}
15 changes: 15 additions & 0 deletions abstract-factory/dart/mysql_connection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'connection.dart';

final class MysqlConnection implements Connection {
const MysqlConnection();

@override
bool openConnection() {
return true;
}

@override
bool closeConnection() {
return true;
}
}
19 changes: 19 additions & 0 deletions abstract-factory/dart/mysql_database_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'command.dart';
import 'connection.dart';
import 'i_database_factory.dart';
import 'mysql_command.dart';
import 'mysql_connection.dart';

final class MysqlDatabaseFactory implements IDatabaseFactory {
const MysqlDatabaseFactory();

@override
Command createCommand() {
return const MysqlCommand();
}

@override
Connection createConnection() {
return const MysqlConnection();
}
}
10 changes: 10 additions & 0 deletions abstract-factory/dart/oracle_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'command.dart';

final class OracleCommand implements Command {
const OracleCommand();

@override
void executeCommand(String query) {
print('OracleCommand: $query');
}
}
15 changes: 15 additions & 0 deletions abstract-factory/dart/oracle_connection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'connection.dart';

final class OracleConnection implements Connection {
const OracleConnection();

@override
bool openConnection() {
return true;
}

@override
bool closeConnection() {
return true;
}
}
19 changes: 19 additions & 0 deletions abstract-factory/dart/oracle_database_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'command.dart';
import 'connection.dart';
import 'i_database_factory.dart';
import 'oracle_command.dart';
import 'oracle_connection.dart';

final class OracleDatabaseFactory implements IDatabaseFactory {
const OracleDatabaseFactory();

@override
Command createCommand() {
return const OracleCommand();
}

@override
Connection createConnection() {
return const OracleConnection();
}
}
17 changes: 17 additions & 0 deletions builder/dart/lesson.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
final class Lesson {
int? id;
String? name;
double? price;
double? discountedPrice;
bool? discountApplied;
String? lessonNote;

Lesson({
this.id,
this.name,
this.price,
this.discountedPrice,
this.discountApplied,
this.lessonNote,
});
}
9 changes: 9 additions & 0 deletions builder/dart/lesson_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'lesson.dart';

abstract class LessonBuilder {
Lesson get lesson;
void getLesson();
void applyDiscount();
void addLessonNote();
Lesson getResult();
}
15 changes: 15 additions & 0 deletions builder/dart/lesson_director.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'lesson_builder.dart';

final class LessonDirector {
late final LessonBuilder _builder;

LessonDirector(LessonBuilder builder) {
_builder = builder;
}

void make() {
_builder.getLesson();
_builder.applyDiscount();
_builder.addLessonNote();
}
}
15 changes: 15 additions & 0 deletions builder/dart/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'lesson.dart';
import 'lesson_builder.dart';
import 'lesson_director.dart';
import 'new_student_lesson_builder.dart';

void main() {
final LessonBuilder lessonBuilder = NewStudentLessonBuilder();

final LessonDirector lessonDirector = LessonDirector(lessonBuilder);
lessonDirector.make();

final Lesson lesson = lessonBuilder.getResult();

print('${lesson.name} - ${lesson.price} - ${lesson.discountedPrice}');
}
31 changes: 31 additions & 0 deletions builder/dart/new_student_lesson_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'lesson.dart';
import 'lesson_builder.dart';

final class NewStudentLessonBuilder implements LessonBuilder {
@override
late Lesson lesson;

@override
void getLesson() {
lesson = Lesson();
lesson.id = 1;
lesson.name = 'Artificial Intelligence - Beginner to Advanced in 10 Minute.';
lesson.price = 49.99;
}

@override
void applyDiscount() {
lesson.discountedPrice = lesson.price! * 0.5;
lesson.discountApplied = true;
}

@override
void addLessonNote() {
lesson.lessonNote = 'Hey, welcome. Your discount code has been applied!';
}

@override
Lesson getResult() {
return lesson;
}
}
31 changes: 31 additions & 0 deletions builder/dart/old_student_lesson_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'lesson.dart';
import 'lesson_builder.dart';

final class OldStudentLessonBuilder implements LessonBuilder {
@override
late Lesson lesson;

@override
void getLesson() {
lesson = Lesson();
lesson.id = 1;
lesson.name = 'Artificial Intelligence - Beginner to Advanced in 10 Minute.';
lesson.price = 49.99;
}

@override
void applyDiscount() {
lesson.discountedPrice = lesson.price;
lesson.discountApplied = false;
}

@override
void addLessonNote() {
lesson.lessonNote = '';
}

@override
Lesson getResult() {
return lesson;
}
}
20 changes: 20 additions & 0 deletions factory/dart/i_notify.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'mail_notify.dart';
import 'sms_notify.dart';
import 'user.dart';

abstract class INotify {
void sendNotification(User user);

// Alternatif
//? Ayrıca bakınız: https://dart.dev/language/constructors#factory-constructors
factory INotify.createNotify(String type) {
switch (type) {
case 'MAIL':
return const MailNotify();
case 'SMS':
return const SmsNotify();
default:
throw Exception('Geçersiz bildirim türü.');
}
}
}
9 changes: 9 additions & 0 deletions factory/dart/mail_notify.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'i_notify.dart';
import 'user.dart';

final class MailNotify implements INotify {
const MailNotify();

@override
void sendNotification(User user) {}
}
14 changes: 14 additions & 0 deletions factory/dart/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'i_notify.dart';
import 'notify_factory.dart';
import 'user.dart';

void main() {
const NotifyFactory notifyFactory = NotifyFactory();

final INotify? notify = notifyFactory.createNotify('MAIL');
notify?.sendNotification(const User());

// Alternatif
final INotify notify2 = INotify.createNotify('SMS');
notify2.sendNotification(const User());
}
16 changes: 16 additions & 0 deletions factory/dart/notify_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'i_notify.dart';
import 'mail_notify.dart';
import 'sms_notify.dart';

final class NotifyFactory {
const NotifyFactory();

INotify? createNotify(String notifyType) {
if (notifyType == 'SMS') {
return const SmsNotify();
} else if (notifyType == 'MAIL') {
return const MailNotify();
}
return null;
}
}
9 changes: 9 additions & 0 deletions factory/dart/sms_notify.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'i_notify.dart';
import 'user.dart';

final class SmsNotify implements INotify {
const SmsNotify();

@override
void sendNotification(User user) {}
}
3 changes: 3 additions & 0 deletions factory/dart/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
final class User {
const User();
}
13 changes: 13 additions & 0 deletions prototype/dart/employee.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'i_cloneable_prototype.dart';

class Employee implements ICloneablePrototype<Employee> {
final String firstName;
final String lastName;

const Employee(this.firstName, this.lastName);

@override
Employee clone() {
return Employee(firstName, lastName);
}
}
3 changes: 3 additions & 0 deletions prototype/dart/i_cloneable_prototype.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class ICloneablePrototype<TPrototype> {
TPrototype clone();
}
7 changes: 7 additions & 0 deletions prototype/dart/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'employee.dart';

void main() {
const Employee gulizar = Employee('Gülizar', 'Yılmaz');
final Employee cloneGulizar = gulizar.clone();
print(gulizar == cloneGulizar);
}
5 changes: 5 additions & 0 deletions singleton/dart/database.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
final class Database {
const Database._privateConstructor();

static const Database instance = Database._privateConstructor();
}
Loading