/**
* {@link <a href=
* "https://[Log in to view URL]"
* target= "_blank"></a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
public static interface DataSourceComponent {
public void writeData(String text);
public String readData();
}
public static class FileDataSourceConcreteComponent implements DataSourceComponent {
public FileDataSourceConcreteComponent(String fileName) {
this.fileName = fileName;
}
private String fileName;
public void writeData(String text) {
System.out.println("LOG -- file:" + fileName + " operation: write" + " text: " + text);
}
public String readData() {
String text = "text";
System.out.println("LOG -- file:" + fileName + " operation: read" + " text: " + text);
return text;
}
}
public static class DataSourceDecorator implements DataSourceComponent {
public DataSourceDecorator(DataSourceComponent wrappee) {
this.wrappee = wrappee;
}
protected DataSourceComponent wrappee;
public void writeData(String text) {
wrappee.writeData(text);
}
public String readData() {
return wrappee.readData();
}
}
public static class EncryptionDecorator extends DataSourceDecorator {
public EncryptionDecorator(DataSourceComponent wrappee) {
super(wrappee);
}
public void writeData(String text) {
text = "Encryption -- " + text;
wrappee.writeData(text);
}
public String readData() {
String text = wrappee.readData();
return "Encryption -- " + text;
}
}
public static class CompressDecorator extends DataSourceDecorator {
public CompressDecorator(DataSourceComponent wrappee) {
super(wrappee);
}
public void writeData(String text) {
text = "Compression -- " + text;
wrappee.writeData(text);
}
public String readData() {
String text = wrappee.readData();
return "Decompression -- " + text;
}
}
public static void main(String args[]) {
// Unit test - uso del design pattern: decorator
FileDataSourceConcreteComponent source = new FileDataSourceConcreteComponent("somefile.dat");
source.writeData("salary");
DataSourceDecorator decorator = new CompressDecorator(source);
decorator.writeData("salary");
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: