Skip to content

Commit c6498fe

Browse files
committed
Merge branch 'adding-facade'
2 parents 128d556 + ee6433b commit c6498fe

16 files changed

+289
-3
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Dart Programming Language
22

33
## OOP
4-
- <strong><a href="1_oop/README.md" target="_blank">What Is OOP ?!</a></strong>
4+
- <strong><a href="1_oop/" target="_blank">What Is OOP ?!</a></strong>
55

66
## Design Patterns
77
reusable solution to a commonly occurring problem within a given context in software design.
@@ -57,7 +57,9 @@
5757
- Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
5858

5959

60-
- <strong><a href="structural_design_pattern/Adapter/README.md" target="_blank">Adapter</a></strong>
60+
- <strong><a href="structural_design_pattern/Adapter/" target="_blank">Adapter</a></strong>
6161

62-
- <strong><a href="structural_design_pattern/bridge/README.md" target="_blank">Bridge</a></strong>
62+
- <strong><a href="structural_design_pattern/bridge/" target="_blank">Bridge</a></strong>
63+
64+
- <strong><a href="structural_design_pattern/facade/" target="_blank">Facade</a></strong>
6365

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Facade Structural Design Pattern
2+
> hides the complexities of the system by a class that provide a simple interface to a complex subsystem which contains lots of moving parts.
3+
4+
<center><table>
5+
<tr>
6+
<td><img src="assets/Facade_Design_Pattern_Class_Diagram_UML_wikipedia_2.png"></td>
7+
<td><img src="assets/Facade_Design_Pattern_Sequence_Diagram_UML_wikipeda.png"><td>
8+
<td><img src="assets/Example_of_Facade_design_pattern_in_UML.png"></td>
9+
</tr>
10+
</table></center>
11+
12+
<center><img width=75% src="assets/facade-diagram.png"></center>
13+
14+
15+
16+
## Sections
17+
- [Definitions](#Definitions)
18+
- [What Problem Facade Solve?](#What-Problem-Facade-Solve?)
19+
- [Examples](#Examples)
20+
- [file converter example ](#file-converter-example)
21+
- [Shape Marker](#Shape-Marker) Tutorial point Example
22+
- [Sources](#Sources)
23+
24+
25+
## **Definitions**
26+
<dl>
27+
<dt><b>tutorialspoint </b></dt>
28+
<dd>
29+
<li>is a structural pattern.</li>
30+
<li> this pattern adds an interface to existing system to hide its complexities.</li>
31+
<li> and provides an interface to the client using which the client can access the system.</li>
32+
</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>
39+
<dt><b>Wikipedia </b></dt>
40+
<dd>
41+
<li>a structural pattern</li>
42+
<li>provides a simplified interface to a library, a framework, or any other complex set of classes.</li>
43+
</br>
44+
<li>Facade describe how to solve recurring design problems</li>
45+
<li>to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.</li>
46+
</dd>
47+
</dl>
48+
49+
</br>
50+
51+
## **What Problem Facade Solve?**
52+
53+
- 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.
55+
56+
> 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.
57+
58+
---
59+
60+
### Solution
61+
- A facade is a `class` that provides a simple interface to a complex subsystem which contains lots of moving parts.
62+
- A facade might provide limited functionality in comparison to working with the subsystem directly.
63+
- However, it includes only those features that clients really care about.
64+
65+
- Having a facade is handy when you need to integrate your app with a sophisticated library that has dozens of features, but you just need a tiny bit of its functionality.
66+
67+
68+
69+
## **Examples**
70+
71+
### 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>
76+
77+
### Shape Marker
78+
- 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>
80+
81+
82+
83+
## **Sources**
84+
85+
- <a href="https://en.wikipedia.org/wiki/Facade_pattern" target="_blank"> wikipedia.org Facade_pattern</a>
86+
- <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>
88+
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'compress.dart';
2+
import 'converter.dart';
3+
import 'file_scanner.dart';
4+
import 'normalizer.dart';
5+
6+
class FileConverterFacade {
7+
String _file;
8+
bool _isAudio = true;
9+
10+
late IFileScanner _fileScanner;
11+
late INormalizer _normalizer;
12+
late IConverter _converter;
13+
late ICompress _fileCompress;
14+
15+
FileConverterFacade(String filePath) : _file = filePath {
16+
_detectType();
17+
_load();
18+
}
19+
20+
void _detectType() {
21+
String fileExtension = _file.split('.').last;
22+
if (["mp3", "aa", "aac"].contains(fileExtension)) {
23+
_fileScanner = MP3FileScanner(_file);
24+
_normalizer = AudioNormalizer();
25+
_converter = AudioConverter();
26+
_fileCompress = ZipCompress();
27+
} else {
28+
_fileScanner = MP4FileScanner(_file);
29+
_normalizer = VideoNormalizer();
30+
_converter = VideoConverter();
31+
_fileCompress = RarCompress();
32+
_isAudio = false;
33+
}
34+
}
35+
36+
void _load() {
37+
if (_fileScanner.scan()) {
38+
_normalizer.normalize(_file);
39+
} else {
40+
// let this be an CorruptedFileException ;
41+
// but for now we will print just error
42+
print("error from file Scan");
43+
}
44+
}
45+
46+
convert() {
47+
if (_isAudio) {
48+
_converter.convert(
49+
_file, AudioTypes.AUDIO_MP3.name, AudioTypes.AUDIO_AAC.name);
50+
} else {
51+
_converter.convert(
52+
_file, VideoTypes.AUDIO_MP4.name, VideoTypes.AUDIO_AVI.name);
53+
}
54+
_fileCompress.compress(_file);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# File Converter Example
2+
3+
<img src ="file_converter.png" />
4+
<img src ="../assets/Facade_Design_Pattern_Sequence_Diagram_UML_wikipeda.png" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
abstract class ICompress {
2+
void compress(String fileName);
3+
}
4+
5+
class RarCompress implements ICompress {
6+
@override
7+
void compress(String fileName) =>
8+
print("File has been compressed to a rar file\n");
9+
}
10+
11+
class ZipCompress implements ICompress {
12+
@override
13+
void compress(String fileName) =>
14+
print("File has been compressed to a zip file\n");
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
abstract class IConverter {
2+
void convert(String fileName, String fromType, String toType);
3+
}
4+
5+
enum AudioTypes { AUDIO_MP3, AUDIO_AA, AUDIO_AAC }
6+
enum VideoTypes { AUDIO_MP4, AUDIO_AVI, AUDIO_FLV }
7+
8+
class AudioConverter implements IConverter {
9+
void convert(String fileName, String fromType, String toType) => print(
10+
"I am converting audio file $fileName from $fromType to $toType \n");
11+
}
12+
13+
class VideoConverter implements IConverter {
14+
void convert(String fileName, String fromType, String toType) =>
15+
print("I am converting the file $fileName from $fromType to $toType \n");
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile host="app.diagrams.net" modified="2022-04-02T13:25:40.183Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55" etag="jHKP1zxxewkc5JKrZXjo" version="17.1.5" type="device"><diagram id="C5RBs43oDa-KdzZeNtuy" name="Page-1">7V1bd5u4Fv41XpM+mAWIi3mMnabTnrSTaXraOeflLMXINqeAPJjEcX/9SCBxkYSNLySOY6+sxGwkIWl/+2Nrb0F6YBQ9fUjgfPYZ+yjsmbr/1ANXPdM0gaGTP1SyyiXuwMgF0yTwc1FFcBf8QkzI6k0fAh8tagVTjMM0mN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Qp49Id0XanTnnVrb7cc+s2Bt28dmv7YBQV1sB/IEsrDcQzAfcLwa8w3c2lVstxuXQB7Nht0/68t3RJVyrunNUuX6V0OdBFVut2PyBaiyc+dPsQnypWkPSPtoDuLwAQ9o9cWPLYYGDsR01uA1MJ2ci3qzTLcpTnsCTNfi7VPH4RQaVZdw0+MUndMjaEuPDQ9pdkCPfYEtLOcgXqFhaq5lkyW3DjyTLLqdOuKBmEo6EFf2hWcGbZZZ6pb75OzVm+U+9/S5T/X+hTP3beQ+8wi5T3y1qKNrwktmWj8RU3+hg20JvHkofhN6zK/TKb/xIOOZ35jxv1J+I4flv0zOi5f/lxq8/wc=</diagram></mxfile>
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:math' as math;
2+
3+
abstract class IFileScanner {
4+
String file;
5+
IFileScanner(String this.file);
6+
bool scan();
7+
}
8+
9+
class MP3FileScanner extends IFileScanner {
10+
MP3FileScanner(String file) : super(file);
11+
12+
@override
13+
bool scan() => math.Random().nextInt(10) > 5;
14+
}
15+
16+
class MP4FileScanner extends IFileScanner {
17+
MP4FileScanner(String file) : super(file);
18+
// Do a lot of stuff here then return the file status
19+
@override
20+
bool scan() => math.Random().nextInt(10) > 5;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'FileConverterFacade.dart';
2+
3+
void main(List<String> args) {
4+
FileConverterFacade fileConverterFacade = FileConverterFacade('wild.mp3');
5+
fileConverterFacade.convert();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
abstract class INormalizer {
2+
void normalize(String fileName);
3+
}
4+
5+
class AudioNormalizer implements INormalizer {
6+
@override
7+
void normalize(String fileName) => print(
8+
"I am normalizing the file $fileName due to some data corruption\n");
9+
}
10+
11+
class VideoNormalizer implements INormalizer {
12+
@override
13+
void normalize(String fileName) => print(
14+
"I am normalizing the video $fileName due to some data corruption\n");
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//!
2+
//! BAD EXAMPLE FROM TUTORIAL POINT
3+
//! Source:: https://www.tutorialspoint.com/design_pattern/facade_pattern.htm
4+
5+
// ShapeMaker class uses the concrete classes to delegate user calls to these classes.
6+
// main, our demo class, will use ShapeMaker class to show the results.
7+
8+
// Step 1
9+
// Create an interface.
10+
abstract class IShape {
11+
void draw();
12+
}
13+
14+
// Step 2
15+
// Create concrete classes implementing the same interface.
16+
class Rectangle implements IShape {
17+
@override
18+
void draw() => print("Rectangle::draw()");
19+
}
20+
21+
class Square implements IShape {
22+
@override
23+
void draw() => print("Square::draw()");
24+
}
25+
26+
class Circle implements IShape {
27+
@override
28+
void draw() => print("Circle::draw()");
29+
}
30+
31+
// Step 3
32+
// Create a facade class.
33+
class ShapeMaker {
34+
IShape _circle;
35+
IShape _rectangle;
36+
IShape _square;
37+
38+
ShapeMaker()
39+
: _circle = Circle(),
40+
_rectangle = Rectangle(),
41+
_square = Square();
42+
43+
void drawCircle() => _circle.draw();
44+
void drawRectangle() => _rectangle.draw();
45+
void drawSquare() => _square.draw();
46+
}
47+
48+
// Step 4
49+
// Use the facade to draw various types of shapes.
50+
void main() {
51+
ShapeMaker shapeMaker = ShapeMaker();
52+
53+
shapeMaker.drawCircle();
54+
shapeMaker.drawRectangle();
55+
shapeMaker.drawSquare();
56+
}
57+
58+
// Step 5
59+
// output.
60+
// Circle::draw()
61+
// Rectangle::draw()
62+
// Square::draw()

0 commit comments

Comments
 (0)