Skip to content

Commit e4d92e4

Browse files
authored
Merge pull request #69 from ilopX/add-conceptual-proxy-pattern
Add conceptual proxy pattern
2 parents 28e5cc0 + 510dae4 commit e4d92e4

File tree

8 files changed

+85
-2
lines changed

8 files changed

+85
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.31.0
2+
- Add conceptual proxy pattern.
3+
14
## 0.30.0
25
- Add strategy pattern: View Strategy.
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
3434
The examples were written in **Dart 2.17**.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'subject.dart';
2+
3+
class RealSubject implements Subject {
4+
@override
5+
String request() {
6+
return 'Real data.';
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abstract class Subject {
2+
String request();
3+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: design_patterns_dart
22
description: Dart examples for all classic GoF design patterns.
3-
version: 0.30.0
3+
version: 0.31.0
44
homepage: https://refactoring.guru/design-patterns
55
repository: https://github.com/RefactoringGuru/design-patterns-dart
66
issue_tracker: https://github.com/RefactoringGuru/design-patterns-dart/issue

0 commit comments

Comments
 (0)