Problem: Compress Files with Switchable Encoders
Problem statement
Your app processes large log files before uploading them to cloud storage. Different environments require different compression types:
Development uses no compression for easy debugging.
Staging uses Gzip for quick compression.
Production uses Brotli for maximum space savings.
Compression systems often hardcode algorithm selection with if statements, making them fragile as new rules are added or modified. Each change risks breaking existing behavior. Design a file compression utility using the Strategy Pattern so that each compression algorithm is encapsulated, ensuring a consistent file-processing workflow.
Goal
Implement both pieces of the pattern:
Encoders (Strategies):
GzipEncoder: Simulate compression with a delay and return"gzipped:<data>".BrotliEncoder: Simulate compression with a longer delay and return"brotli:<data>".NoCompression: Return the original data immediately.Each must define an async
.compress(data)method.
Compressor (Context):
Implements
.compress(data)that delegates to the current encoder’s method.Must support runtime switching through
.setEncoder().
Constraints
The
.compress()methods must return aPromise.Compressormust not useiforswitchlogic to select encoders.Simulate async behavior using
setTimeoutinsidePromises.The output should clearly reflect which encoder was used.
Sample output
The examples below illustrate what the output should look like:
(async () => {const compressor = new Compressor(new GzipEncoder());console.log(await compressor.compress('log data'));/* Expected output:gzipped:log data*/compressor.setEncoder(new BrotliEncoder());console.log(await compressor.compress('log data'));/* Expected output:brotli:log data*/compressor.setEncoder(new NoCompression());console.log(await compressor.compress('log data'));/* Expected output:log data*/})();
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Compress Files with Switchable Encoders
Problem statement
Your app processes large log files before uploading them to cloud storage. Different environments require different compression types:
Development uses no compression for easy debugging.
Staging uses Gzip for quick compression.
Production uses Brotli for maximum space savings.
Compression systems often hardcode algorithm selection with if statements, making them fragile as new rules are added or modified. Each change risks breaking existing behavior. Design a file compression utility using the Strategy Pattern so that each compression algorithm is encapsulated, ensuring a consistent file-processing workflow.
Goal
Implement both pieces of the pattern:
Encoders (Strategies):
GzipEncoder: Simulate compression with a delay and return"gzipped:<data>".BrotliEncoder: Simulate compression with a longer delay and return"brotli:<data>".NoCompression: Return the original data immediately.Each must define an async
.compress(data)method.
Compressor (Context):
Implements
.compress(data)that delegates to the current encoder’s method.Must support runtime switching through
.setEncoder().
Constraints
The
.compress()methods must return aPromise.Compressormust not useiforswitchlogic to select encoders.Simulate async behavior using
setTimeoutinsidePromises.The output should clearly reflect which encoder was used.
Sample output
The examples below illustrate what the output should look like:
(async () => {const compressor = new Compressor(new GzipEncoder());console.log(await compressor.compress('log data'));/* Expected output:gzipped:log data*/compressor.setEncoder(new BrotliEncoder());console.log(await compressor.compress('log data'));/* Expected output:brotli:log data*/compressor.setEncoder(new NoCompression());console.log(await compressor.compress('log data'));/* Expected output:log data*/})();
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.