Last active
December 1, 2024 09:44
-
-
Save Nirajpaul2/b9b0129ae99b42fd0fecc2bba0b60de9 to your computer and use it in GitHub Desktop.
CompressionStrategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Step 1: Define the Compression Strategy Protocol | |
| // This protocol ensures all compression strategies have a common interface. | |
| protocol CompressionStrategy { | |
| func compress(files: [String]) -> String | |
| } | |
| // Step 2: Concrete Compression Strategies | |
| // ZIP compression strategy | |
| class ZipCompression: CompressionStrategy { | |
| func compress(files: [String]) -> String { | |
| // Simulate compression using ZIP | |
| print("Compressing files using ZIP format...") | |
| return "Compressed \(files.count) files into a ZIP archive." | |
| } | |
| } | |
| // RAR compression strategy | |
| class RarCompression: CompressionStrategy { | |
| func compress(files: [String]) -> String { | |
| // Simulate compression using RAR | |
| print("Compressing files using RAR format...") | |
| return "Compressed \(files.count) files into a RAR archive." | |
| } | |
| } | |
| // TAR compression strategy | |
| class TarCompression: CompressionStrategy { | |
| func compress(files: [String]) -> String { | |
| // Simulate compression using TAR | |
| print("Compressing files using TAR format...") | |
| return "Compressed \(files.count) files into a TAR archive." | |
| } | |
| } | |
| // Step 3: Context Class | |
| // The context class manages the compression strategies and delegates compression to the selected strategy. | |
| class FileCompressor { | |
| private var compressionStrategy: CompressionStrategy? // Holds the current strategy | |
| // Method to set the compression strategy dynamically | |
| func setCompressionStrategy(_ strategy: CompressionStrategy) { | |
| self.compressionStrategy = strategy | |
| } | |
| // Method to compress files using the current strategy | |
| func compressFiles(_ files: [String]) -> String { | |
| guard let strategy = compressionStrategy else { | |
| fatalError("Compression strategy is not set") // Ensure a strategy is set before compressing | |
| } | |
| return strategy.compress(files: files) | |
| } | |
| } | |
| // Step 4: Demonstration of Usage | |
| // Main function to simulate the file compression tool | |
| func main() { | |
| // List of files to compress | |
| let files = ["file1.txt", "file2.jpg", "file3.pdf"] | |
| // Create the context (FileCompressor) | |
| let fileCompressor = FileCompressor() | |
| // Use ZIP compression | |
| fileCompressor.setCompressionStrategy(ZipCompression()) // Set the strategy to ZipCompression | |
| print(fileCompressor.compressFiles(files)) // Compress files and print the result | |
| // Use RAR compression | |
| fileCompressor.setCompressionStrategy(RarCompression()) // Change strategy to RarCompression | |
| print(fileCompressor.compressFiles(files)) // Compress files and print the result | |
| // Use TAR compression | |
| fileCompressor.setCompressionStrategy(TarCompression()) // Change strategy to TarCompression | |
| print(fileCompressor.compressFiles(files)) // Compress files and print the result | |
| } | |
| // Execute the main function | |
| main() | |
| Compressing files using ZIP format... | |
| Compressed 3 files into a ZIP archive. | |
| Compressing files using RAR format... | |
| Compressed 3 files into a RAR archive. | |
| Compressing files using TAR format... | |
| Compressed 3 files into a TAR archive. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment