Skip to content

Commit c52e12a

Browse files
committed
commits decorator design pattern.
1 parent 15d0f56 commit c52e12a

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.designpatterns.structural.decorator;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.util.zip.GZIPOutputStream;
7+
8+
/**
9+
* this is one of the concrete decorators
10+
*/
11+
public class CompressingDecorator extends SenderDecorator {
12+
13+
public CompressingDecorator(Sender sender) {
14+
super(sender);
15+
}
16+
17+
@Override
18+
public String send(String content) {
19+
String compressedContent = new String(compressContent(content));
20+
super.send(compressedContent);
21+
return compressedContent;
22+
}
23+
24+
private byte[] compressContent(String content) {
25+
try{
26+
ByteArrayOutputStream baostream = new ByteArrayOutputStream();
27+
OutputStream outStream = new GZIPOutputStream(baostream);
28+
outStream.write(content.getBytes("UTF-8"));
29+
outStream.close();
30+
byte[] compressedBytes = baostream.toByteArray(); // toString not always possible
31+
return compressedBytes;
32+
} catch (IOException e) {
33+
throw new RuntimeException("exception happened while compressing email content");
34+
35+
}
36+
}
37+
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.designpatterns.structural.decorator;
2+
3+
/**
4+
* this is the class which should be decorated without any modification
5+
*/
6+
public class EmailSender implements Sender {
7+
@Override
8+
public String send(String content) {
9+
System.out.println("sending \"" + content + "\" as email");
10+
return content;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.designpatterns.structural.decorator;
2+
3+
import java.util.Base64;
4+
5+
/**
6+
* this is another concrete decorator.
7+
*/
8+
public class EncodingDecorator extends SenderDecorator {
9+
10+
public EncodingDecorator(Sender sender) {
11+
super(sender);
12+
}
13+
14+
@Override
15+
public String send(String content) {
16+
String encodedContent = encodeContent(content);
17+
super.send(encodedContent);
18+
return encodedContent;
19+
}
20+
21+
private String encodeContent(String content){
22+
return Base64.getEncoder().encodeToString(content.getBytes());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.designpatterns.structural.decorator;
2+
3+
public interface Sender {
4+
String send(String content);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.designpatterns.structural.decorator;
2+
3+
/**
4+
* this is the base decorator.
5+
*/
6+
public class SenderDecorator implements Sender {
7+
private Sender sender;
8+
9+
public SenderDecorator(Sender sender) {
10+
this.sender = sender;
11+
}
12+
13+
@Override
14+
public String send(String content) {
15+
return this.sender.send(content);
16+
}
17+
}

0 commit comments

Comments
 (0)