File tree Expand file tree Collapse file tree 7 files changed +117
-0
lines changed
patterns/factory_method/concaptual_platform_dialog Expand file tree Collapse file tree 7 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 1+ /// EN: Common interface for all buttons.
2+ ///
3+ /// RU: Общий интерфейс для всех продуктов.
4+ abstract class Button {
5+ final void Function () onClick;
6+
7+ Button (this .onClick);
8+
9+ void render ();
10+ }
Original file line number Diff line number Diff line change 1+ import 'button.dart' ;
2+
3+ /// EN: HTML button implementation.
4+ ///
5+ /// RU: Реализация HTML кнопок.
6+ class HtmlButton extends Button {
7+ HtmlButton (void Function () onClick) : super (onClick);
8+
9+ @override
10+ void render () {
11+ print ('<button>Test Button</button>' );
12+ onClick ();
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ import 'button.dart' ;
2+
3+ /// EN: Windows button implementation.
4+ ///
5+ /// RU: Реализация нативных кнопок операционной системы.
6+ class WindowsButton extends Button {
7+ WindowsButton (void Function () onClick) : super (onClick);
8+
9+ @override
10+ void render () {
11+ print ('Windows Button' );
12+ onClick ();
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ import '../buttons/button.dart' ;
2+
3+ abstract class Dialog {
4+ void renderWindow () {
5+ /// EN: ... other code ...
6+ ///
7+ /// RU: ... остальной код диалога ...
8+
9+ Button okButton = createButton (() {
10+ print ('Click! Button says - "Hello World!"' );
11+ });
12+ okButton.render ();
13+ }
14+
15+ /// EN: Subclasses will override this method in order to create specific
16+ /// button objects.
17+ ///
18+ /// RU: Подклассы будут переопределять этот метод, чтобы создавать конкретные
19+ /// объекты продуктов, разные для каждой фабрики.
20+ Button createButton (void Function () onClick);
21+ }
Original file line number Diff line number Diff line change 1+ import '../buttons/button.dart' ;
2+ import '../buttons/html_button.dart' ;
3+ import 'dialog.dart' ;
4+
5+ /// EN: HTML Dialog will produce HTML buttons.
6+ ///
7+ /// RU: HTML-диалог.
8+ class HtmlDialog extends Dialog {
9+ @override
10+ Button createButton (void Function () onClick) => HtmlButton (onClick);
11+ }
Original file line number Diff line number Diff line change 1+ import '../buttons/button.dart' ;
2+ import '../buttons/windows_button.dart' ;
3+ import 'dialog.dart' ;
4+
5+ /// EN: Windows Dialog will produce Windows buttons.
6+ ///
7+ /// RU: Диалог на элементах операционной системы.
8+ class WindowsDialog extends Dialog {
9+ @override
10+ Button createButton (void Function () onClick) => WindowsButton (onClick);
11+ }
Original file line number Diff line number Diff line change 1+ import 'dart:io' ;
2+
3+ import 'dialogs_factory/dialog.dart' ;
4+ import 'dialogs_factory/html_dialog.dart' ;
5+ import 'dialogs_factory/windows_dialog.dart' ;
6+
7+ late Dialog dialog;
8+
9+ void main () {
10+ configure ();
11+ runBusinessLogic ();
12+ }
13+
14+ /// EN: The concrete factory is usually chosen depending on configuration or
15+ /// environment options.
16+ ///
17+ /// RU: Приложение создаёт определённую фабрику в зависимости от конфигурации
18+ /// или окружения.
19+ void configure () {
20+ if (Platform .isWindows) {
21+ dialog = WindowsDialog ();
22+ } else {
23+ dialog = HtmlDialog ();
24+ }
25+ }
26+
27+ /// EN: All of the client code should work with factories and products
28+ /// through abstract interfaces. This way it does not care which factory it
29+ /// works with and what kind of product it returns.
30+ ///
31+ /// RU: Весь остальной клиентский код работает с фабрикой и продуктами только
32+ /// через общий интерфейс, поэтому для него неважно какая фабрика была
33+ /// создана.
34+ void runBusinessLogic () {
35+ dialog.renderWindow ();
36+ }
You can’t perform that action at this time.
0 commit comments