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); }
public static void Main() { var userProfile = new UserProfile(); var validationDecorator = new ValidationDecorator(userProfile); // with invalid input try { validationDecorator.Username = "******"; } catch (ArgumentException e) { Console.WriteLine(e.Message); } try { validationDecorator.Password = "******"; } catch (ArgumentException e) { Console.WriteLine(e.Message); } // with valid input validationDecorator.Username = "******"; validationDecorator.Password = "******"; Console.WriteLine("Username: "******"Password: "******"ivan"; } catch (ArgumentException e) { Console.WriteLine(e.Message); } try { encryptionDecorator.Password = "******"; } catch (ArgumentException e) { Console.WriteLine(e.Message); } encryptionDecorator.Username = "******"; encryptionDecorator.Password = "******"; Console.WriteLine("Username: "******"Encrypted password: " + userProfile.Password); }