|
1 | 1 | # File Converter Example
|
2 | 2 |
|
| 3 | +## UML |
3 | 4 | <img src ="file_converter.png" />
|
4 | 5 | <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