-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBridge.dart
71 lines (53 loc) · 1.53 KB
/
Bridge.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import 'package:design_pattern_dart/Display/Example.dart';
class Bridge extends Example {
Bridge([String filePath = "lib/Structural/Bridge.dart"]) : super(filePath);
@override
String testRun() {
final lightTheme = LightTheme();
final darkTheme = DarkTheme();
final homePage = HomePage(lightTheme);
final aboutPage = AboutPage(darkTheme);
return """
// Check our home page with light theme.
${homePage.content()}
// Check our about page with dark theme.
${aboutPage.content()}
""";
}
}
// 我們有兩種 Theme 可以使用。
abstract class MyTheme {
String getColor();
String getFont();
}
class DarkTheme implements MyTheme {
@override
String getColor() => "Dark black";
@override
String getFont() => "Arial";
}
class LightTheme implements MyTheme {
@override
String getColor() => "Light white";
@override
String getFont() => "Times New Roman";
}
// 我們的網站則有 "主頁"還有 "關於我頁面",若使用繼承,需要製作四種不同網頁。
// 但這裡使用 Bridge pattern 。
abstract class WebPage {
MyTheme theme;
WebPage(this.theme);
String content();
}
class HomePage extends WebPage {
HomePage(MyTheme theme) : super(theme);
@override
String content() =>
"This is home page in ${theme.getColor()} color and ${theme.getFont()} font.";
}
class AboutPage extends WebPage {
AboutPage(MyTheme theme) : super(theme);
@override
String content() =>
"This is about page in ${theme.getColor()} color and ${theme.getFont()} font.";
}