File tree Expand file tree Collapse file tree 8 files changed +85
-2
lines changed
patterns/proxy/conceptual Expand file tree Collapse file tree 8 files changed +85
-2
lines changed Original file line number Diff line number Diff line change 1+ ## 0.31.0
2+ - Add conceptual proxy pattern.
3+
14## 0.30.0
25- Add strategy pattern: View Strategy.
36
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
2828 - [x] ** Decorator** - [[ Data Source Decoder] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/decorator/data_source_decoder )]
2929 - [ ] ** Facade**
3030 - [x] ** Flyweight** - [[ Conceptual] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/flyweight/conceptual )]
31- - [ ] ** Proxy**
31+ - [x ] ** Proxy** - [[ Conceptual ] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/proxy/conceptual )]
3232
3333## Requirements
3434The examples were written in ** Dart 2.17** .
Original file line number Diff line number Diff line change 1+ # Proxy Pattern
2+ Proxy is a structural design pattern that lets you provide a substitute or placeholder for another
3+ object. A proxy controls access to the original object, allowing you to perform something either
4+ before or after the request gets through to the original object.
5+
6+ Tutorial: [ here] ( https://refactoring.guru/design-patterns/proxy ) .
7+
8+ ### Diagram:
9+ ![ image] ( https://user-images.githubusercontent.com/8049534/175926828-d4fed7c6-ea82-4717-a24b-8ad2b23910ba.png )
10+
11+ ### Client code:
12+ ``` dart
13+ void main() async {
14+ final subject = Proxy();
15+ print(subject.request()); // print "Proxy data"
16+
17+ print('Wait for 2 seconds...');
18+ await Future.delayed(Duration(seconds: 2));
19+
20+ print(subject.request()); // print "Real data"
21+ }
22+ ```
23+
24+ ### Output:
25+ ```
26+ Proxy data.
27+ Wait 2 seconds...
28+ Real data.
29+ ```
Original file line number Diff line number Diff line change 1+ import 'pattern/proxy.dart' ;
2+ import 'pattern/subject.dart' ;
3+
4+ void main () async {
5+ final subject = Proxy ();
6+ client (subject); // print "Proxy data"
7+
8+ print ('Wait 2 seconds...' );
9+ await Future .delayed (Duration (seconds: 2 ));
10+
11+ client (subject); // print "Real data"
12+ }
13+
14+ void client (Subject subject) {
15+ print (subject.request ());
16+ }
Original file line number Diff line number Diff line change 1+ import 'subject.dart' ;
2+ import 'real_subject.dart' ;
3+
4+ class Proxy implements Subject {
5+ @override
6+ String request () {
7+ if (isSubjectLoaded) {
8+ return _subject! .request ();
9+ }
10+
11+ _load ();
12+ return 'Proxy data.' ;
13+ }
14+
15+ bool get isSubjectLoaded => _subject != null ;
16+
17+ void _load () async {
18+ Future .delayed (Duration (seconds: 1 ), () {
19+ _subject = RealSubject ();
20+ });
21+ }
22+
23+ Subject ? _subject;
24+ }
Original file line number Diff line number Diff line change 1+ import 'subject.dart' ;
2+
3+ class RealSubject implements Subject {
4+ @override
5+ String request () {
6+ return 'Real data.' ;
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ abstract class Subject {
2+ String request ();
3+ }
Original file line number Diff line number Diff line change 11name : design_patterns_dart
22description : Dart examples for all classic GoF design patterns.
3- version : 0.30 .0
3+ version : 0.31 .0
44homepage : https://refactoring.guru/design-patterns
55repository : https://github.com/RefactoringGuru/design-patterns-dart
66issue_tracker : https://github.com/RefactoringGuru/design-patterns-dart/issue
You can’t perform that action at this time.
0 commit comments