Skip to content

Commit 15ec436

Browse files
committed
updated facade design pattern
1 parent c6498fe commit 15ec436

File tree

2 files changed

+140
-20
lines changed

2 files changed

+140
-20
lines changed

structural_design_pattern/facade/README.md

+13-20
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,29 @@
99
</tr>
1010
</table></center>
1111

12-
<center><img width=75% src="assets/facade-diagram.png"></center>
12+
<center>
13+
<img width=75% src="assets/facade-diagram.png">
14+
</center>
1315

1416

1517

1618
## Sections
1719
- [Definitions](#Definitions)
18-
- [What Problem Facade Solve?](#What-Problem-Facade-Solve?)
20+
- [What Problem Facade Solve](#What-Problem-Facade-Solve)
1921
- [Examples](#Examples)
2022
- [file converter example ](#file-converter-example)
21-
- [Shape Marker](#Shape-Marker) Tutorial point Example
23+
- [Shape Maker](#Shape-Maker) Tutorial point Example
2224
- [Sources](#Sources)
2325

2426

2527
## **Definitions**
26-
<dl>
28+
<dl>
2729
<dt><b>tutorialspoint </b></dt>
2830
<dd>
2931
<li>is a structural pattern.</li>
3032
<li> this pattern adds an interface to existing system to hide its complexities.</li>
3133
<li> and provides an interface to the client using which the client can access the system.</li>
3234
</dd>
33-
<dt><b>refactoring.guru </b></dt>
34-
<dd>
35-
<li>a structural pattern</li>
36-
<li>provides a simplified interface to a library, a framework, or any other complex set of classes.</li>
37-
</dd>
38-
</dd>
3935
<dt><b>Wikipedia </b></dt>
4036
<dd>
4137
<li>a structural pattern</li>
@@ -48,17 +44,17 @@
4844

4945
</br>
5046

51-
## **What Problem Facade Solve?**
47+
## **What Problem Facade Solve**
5248

5349
- Imagine that you must make your code work with a broad set of objects that belong to a sophisticated library or framework.
54-
- Ordinarily, you’d need to initialize all of those objects, keep track of dependencies, execute methods in the correct order, and so on.
50+
- Ordinarily, you’d need to initialize all of those objects, keep track of dependencies, **`execute methods in the correct order`**, and so on.
5551

5652
> As a result, the business logic of your classes would become tightly coupled to the implementation details of 3rd-party classes, making it hard to comprehend and maintain.
5753
5854
---
5955

6056
### Solution
61-
- A facade is a `class` that provides a simple interface to a complex subsystem which contains lots of moving parts.
57+
- A facade is a `class` that provides a `simple interface` to a complex subsystem which contains lots of moving parts.
6258
- A facade might provide limited functionality in comparison to working with the subsystem directly.
6359
- However, it includes only those features that clients really care about.
6460

@@ -69,20 +65,17 @@
6965
## **Examples**
7066

7167
### file converter example
72-
- Example in dart: <a href="file converter example ="_blank">file converter example </a>
73-
- Example Source:
74-
- <a href="https://refactoring.guru/design-patterns/bridge" target="_blank" >https://refactoring.guru/design-patterns/bridge</a>
75-
- <a href="https://www.tutorialspoint.com/design_pattern/bridge_pattern.htm" target="_blank" >https://www.tutorialspoint.com/design_pattern/bridge_pattern.htm</a>
68+
- Example in dart: <a href="file_converter/" target="_blank">file converter example </a>
7669

77-
### Shape Marker
70+
### Shape Maker
7871
- source: <a href="https://www.tutorialspoint.com/design_pattern/facade_pattern.htm" target="_blank">tutorialspoint.com/design_pattern/facade_pattern</a>
79-
- Example in dart: <a href="shape_marker.dart" target="_blank">Shape Marker</a>
72+
- Example in dart: <a href="shape_maker.dart" target="_blank">Shape Maker</a>
8073

8174

8275

8376
## **Sources**
8477

8578
- <a href="https://en.wikipedia.org/wiki/Facade_pattern" target="_blank"> wikipedia.org Facade_pattern</a>
8679
- <a href="https://refactoring.guru/design-patterns/facade" target="_blank"> refactoring.guru design-patterns facade</a>
87-
- <a href="https://www.tutorialspoint.com/design_pattern/facade_pattern.htm" target="_blank"> www.tutorialspoint.com facade_pattern </a>
80+
- <a href="https://www.tutorialspoint.com/design_pattern/facade_pattern.htm" target="_blank"> tutorialspoint - facade_pattern </a>
8881

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,131 @@
11
# File Converter Example
22

3+
## UML
34
<img src ="file_converter.png" />
45
<img src ="../assets/Facade_Design_Pattern_Sequence_Diagram_UML_wikipeda.png" />
6+
7+
## Code
8+
9+
### ICompress
10+
```dart
11+
abstract class ICompress {
12+
void compress(String fileName);
13+
}
14+
15+
class RarCompress implements ICompress {
16+
@override
17+
void compress(String fileName) =>
18+
print("File has been compressed to a rar file\n");
19+
}
20+
21+
```
22+
### IConverter
23+
```dart
24+
abstract class IConverter {
25+
void convert(String fileName, String fromType, String toType);
26+
}
27+
28+
enum AudioTypes { AUDIO_MP3, AUDIO_AA, AUDIO_AAC }
29+
30+
class AudioConverter implements IConverter {
31+
void convert(String fileName, String fromType, String toType) => print(
32+
"I am converting audio file $fileName from $fromType to $toType \n");
33+
}
34+
35+
```
36+
### IFileScanner
37+
38+
```dart
39+
40+
abstract class IFileScanner {
41+
String file;
42+
IFileScanner(String this.file);
43+
bool scan();
44+
}
45+
46+
class MP3FileScanner extends IFileScanner {
47+
MP3FileScanner(String file) : super(file);
48+
49+
@override
50+
bool scan() => math.Random().nextInt(10) > 5;
51+
}
52+
53+
```
54+
### INormalizer
55+
56+
```dart
57+
abstract class INormalizer {
58+
void normalize(String fileName);
59+
}
60+
61+
class AudioNormalizer implements INormalizer {
62+
@override
63+
void normalize(String fileName) => print(
64+
"I am normalizing the file $fileName due to some data corruption\n");
65+
}
66+
67+
```
68+
69+
### Facade Class
70+
```dart
71+
class FileConverterFacade {
72+
String _file;
73+
bool _isAudio = true;
74+
75+
late IFileScanner _fileScanner;
76+
late INormalizer _normalizer;
77+
late IConverter _converter;
78+
late ICompress _fileCompress;
79+
80+
FileConverterFacade(String filePath) : _file = filePath {
81+
_detectType();
82+
_load();
83+
}
84+
85+
void _detectType() {
86+
String fileExtension = _file.split('.').last;
87+
if (["mp3", "aa", "aac"].contains(fileExtension)) {
88+
_fileScanner = MP3FileScanner(_file);
89+
_normalizer = AudioNormalizer();
90+
_converter = AudioConverter();
91+
_fileCompress = ZipCompress();
92+
} else {
93+
_fileScanner = MP4FileScanner(_file);
94+
_normalizer = VideoNormalizer();
95+
_converter = VideoConverter();
96+
_fileCompress = RarCompress();
97+
_isAudio = false;
98+
}
99+
}
100+
101+
void _load() {
102+
if (_fileScanner.scan()) {
103+
_normalizer.normalize(_file);
104+
} else {
105+
// let this be an CorruptedFileException ;
106+
// but for now we will print just error
107+
print("error from file Scan");
108+
}
109+
}
110+
111+
convert() {
112+
if (_isAudio) {
113+
_converter.convert(
114+
_file, AudioTypes.AUDIO_MP3.name, AudioTypes.AUDIO_AAC.name);
115+
} else {
116+
_converter.convert(
117+
_file, VideoTypes.AUDIO_MP4.name, VideoTypes.AUDIO_AVI.name);
118+
}
119+
_fileCompress.compress(_file);
120+
}
121+
}
122+
123+
```
124+
125+
### Main Method
126+
```dart
127+
void main(List<String> args) {
128+
FileConverterFacade fileConverterFacade = FileConverterFacade('wild.mp3');
129+
fileConverterFacade.convert();
130+
}
131+
```

0 commit comments

Comments
 (0)