-
Arabic Video: https://youtu.be/N6ogtRbKusw
-
PHP code: https://github.com/mohammedabdoh/design_patterns_course/tree/master/src/Patterns/Structural/Bridge
- UML Source: https://refactoring.guru/design-patterns/bridge
abstract class Report {
String showPrimaryGradeReport();
String showSecondaryGradeReport();
String showGrade3Report();
}
class HTMLReport implements Report {
String showPrimaryGradeReport() =>
"<h1>This is a grade Primary report</h1>\n";
String showSecondaryGradeReport() =>
"<h1>This is a grade Secondary report</h1>\n";
String showGrade3Report() => "<h1>This is a grade 3 report</h1>\n";
}
class PlainTextReport implements Report {
String showPrimaryGradeReport() => "This is a grade Primary report\n";
String showSecondaryGradeReport() => "This is a grade Secondary report\n";
String showGrade3Report() => "This is a grade 3 report\n";
}
class XMLReport implements Report {
String showPrimaryGradeReport() =>
"<StudentReport>This is a grade Primary report</StudentReport>\n";
String showSecondaryGradeReport() =>
"<StudentReport>This is a grade Secondary report</StudentReport>\n";
String showGrade3Report() =>
"<StudentReport>This is a grade 3 report</StudentReport>\n";
}
Create an abstract class Grade
abstract class Grade {
Report report;
Grade(this.report);
String showReport();
}
class PrimaryGrade extends Grade {
PrimaryGrade(Report report) : super(report);
@override
String showReport() => report.showPrimaryGradeReport();
}
class SecondaryGrade extends Grade {
SecondaryGrade(Report report) : super(report);
@override
String showReport() => report.showSecondaryGradeReport();
}
class Grade3Report extends Grade {
Grade3Report(Report report) : super(report);
@override
String showReport() => report.showGrade3Report();
}
// Show any Report Type You Need
// here I choosed Html Report
Report HtmlReport = HTMLReport();
// Then inject Report type to what grade you want to show
// here I choosed Primary
Grade primaryGrade = PrimaryGrade(HtmlReport);
print(primaryGrade.showReport());
<h1>This is a grade Primary report</h1>