Skip to content

Commit 3fe20e3

Browse files
authored
Merge pull request #1186 from ali4j/Development
Decorator pattern Implementation
2 parents 5ec13c6 + 78f7a80 commit 3fe20e3

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
byte[] compressedBytes = compressContent(content);
20+
String compressedContent = new String(compressedBytes);
21+
super.send(compressedContent);
22+
return compressedContent;
23+
}
24+
25+
private byte[] compressContent(String content) {
26+
try (ByteArrayOutputStream baostream = new ByteArrayOutputStream();
27+
OutputStream outStream = new GZIPOutputStream(baostream)){
28+
outStream.write(content.getBytes());
29+
outStream.close();
30+
return baostream.toByteArray();
31+
} catch (IOException e) {
32+
throw new RuntimeException("exception happened while compressing email content");
33+
}
34+
}
35+
36+
37+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.designpatterns.decorator;
2+
3+
import com.designpatterns.structural.decorator.*;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Base64;
8+
9+
public class DecoratorDemo {
10+
11+
@Test
12+
public void testDecorator_sendEmailAsPlainText() {
13+
String message = "test message";
14+
EmailSender sender = new EmailSender();
15+
String content = sender.send(message);
16+
Assertions.assertEquals(content, message);
17+
}
18+
19+
@Test
20+
public void testDecorator_sendEmailAsEncodedText() {
21+
String message = "test message";
22+
Sender sender = new SenderDecorator(
23+
new EncodingDecorator(
24+
new EmailSender()
25+
)
26+
);
27+
String encodedContent = sender.send(message);
28+
Assertions.assertEquals(
29+
new String(Base64.getDecoder().decode (encodedContent)),
30+
message
31+
);
32+
}
33+
34+
@Test
35+
public void testDecorator_sendEmailAsCompressedText() {
36+
String message = "Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.[16] Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but it has fewer low-level facilities than either of them. As of 2019, Java was one of the most popular programming languages in use according to GitHub,[17][18] particularly for client-server web applications, with a reported 9 million developers.";
37+
Sender sender = new SenderDecorator(
38+
new CompressingDecorator(
39+
new EmailSender()
40+
)
41+
);
42+
String compressedContent = sender.send(message);
43+
Assertions.assertTrue(message.length()>= compressedContent.length());
44+
}
45+
46+
@Test
47+
public void testDecorator_sendEmailAsEncodedCompressedText() {
48+
String message = "Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.[16] Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but it has fewer low-level facilities than either of them. As of 2019, Java was one of the most popular programming languages in use according to GitHub,[17][18] particularly for client-server web applications, with a reported 9 million developers.";
49+
Sender sender = new SenderDecorator(
50+
new EncodingDecorator(
51+
new CompressingDecorator(
52+
new EmailSender()
53+
)
54+
)
55+
56+
);
57+
String encodedCompressedContent = sender.send(message);
58+
59+
String decodedCompressed = new String( Base64.getDecoder().decode (encodedCompressedContent));
60+
Assertions.assertTrue(message.length()>= decodedCompressed.length());
61+
}
62+
}

0 commit comments

Comments
 (0)