static void Main(string[] args) { Console.WriteLine("Decorator Pattern !!!" + Environment.NewLine); BaseDataSource file = new FileDataSource(Environment.CurrentDirectory); // Decorate component BaseDataSourceDecorator encryptedFile = new EncryptionDecorator(file); BaseDataSourceDecorator compressFile = new CompressionDecorator(encryptedFile); var resultFile = compressFile.ReadData(); Console.WriteLine(resultFile); Console.WriteLine(); // Other way BaseDataSource stream = new StreamDataSource("www.example.com"); // Decorate component stream = new EncryptionDecorator(stream); stream = new CompressionDecorator(stream); var resultStream = stream.ReadData(); Console.WriteLine(resultStream); }
static void Main(string[] args) { IDataSource fileDataSource = new FileDataSource("test-file.txt"); IDataSource encryptedDataSource = new EncryptionDecorator(fileDataSource); string data = encryptedDataSource.ReadData(); Console.WriteLine(); encryptedDataSource.WriteData(data); }
static void Main(string[] args) { IDataSource dataSource = new FileDataSource("data.sql"); IDataSource compressedDataSource = new ComporessionDecorator(dataSource); compressedDataSource.ReadData(); compressedDataSource.WriteData(new object()); // output: // data.sql readed. // Data compressed. // Compressed data was written to data.sql. }