...
/Solution: Compress Files with Switchable Encoders
Solution: Compress Files with Switchable Encoders
Implement asynchronous encoder strategies and delegate file compression through a configurable compressor context.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 2–22: We define three encoder strategies:
GzipEncoder,BrotliEncoder, andNoCompression.Each implements an asynchronous
.compress()method that returns aPromise.GzipEncodersimulates fast compression with a short delay.BrotliEncodertakes longer, reflecting higher compression cost.NoCompressionresolves immediately, returning the data unchanged.This demonstrates asynchronous strategies that share the same interface.
Lines 25–38: The
Compressorclass is the context.It stores a reference to the current encoder strategy. ...