Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3fe20e3

Browse files
authored
Merge pull requestTheAlgorithms#1186 from ali4j/Development
Decorator pattern Implementation
2 parents5ec13c6 +78f7a80 commit3fe20e3

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.designpatterns.structural.decorator;
2+
3+
importjava.io.ByteArrayOutputStream;
4+
importjava.io.IOException;
5+
importjava.io.OutputStream;
6+
importjava.util.zip.GZIPOutputStream;
7+
8+
/**
9+
* this is one of the concrete decorators
10+
*/
11+
publicclassCompressingDecoratorextendsSenderDecorator {
12+
13+
publicCompressingDecorator(Sendersender) {
14+
super(sender);
15+
}
16+
17+
@Override
18+
publicStringsend(Stringcontent) {
19+
byte[]compressedBytes =compressContent(content);
20+
StringcompressedContent =newString(compressedBytes);
21+
super.send(compressedContent);
22+
returncompressedContent;
23+
}
24+
25+
privatebyte[]compressContent(Stringcontent) {
26+
try (ByteArrayOutputStreambaostream =newByteArrayOutputStream();
27+
OutputStreamoutStream =newGZIPOutputStream(baostream)){
28+
outStream.write(content.getBytes());
29+
outStream.close();
30+
returnbaostream.toByteArray();
31+
}catch (IOExceptione) {
32+
thrownewRuntimeException("exception happened while compressing email content");
33+
}
34+
}
35+
36+
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packagecom.designpatterns.structural.decorator;
2+
3+
/**
4+
* this is the class which should be decorated without any modification
5+
*/
6+
publicclassEmailSenderimplementsSender {
7+
@Override
8+
publicStringsend(Stringcontent) {
9+
System.out.println("sending\"" +content +"\" as email");
10+
returncontent;
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagecom.designpatterns.structural.decorator;
2+
3+
importjava.util.Base64;
4+
5+
/**
6+
* this is another concrete decorator.
7+
*/
8+
publicclassEncodingDecoratorextendsSenderDecorator {
9+
10+
publicEncodingDecorator(Sendersender) {
11+
super(sender);
12+
}
13+
14+
@Override
15+
publicStringsend(Stringcontent) {
16+
StringencodedContent =encodeContent(content);
17+
super.send(encodedContent);
18+
returnencodedContent;
19+
}
20+
21+
privateStringencodeContent(Stringcontent){
22+
returnBase64.getEncoder().encodeToString(content.getBytes());
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
packagecom.designpatterns.structural.decorator;
2+
3+
publicinterfaceSender {
4+
Stringsend(Stringcontent);
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packagecom.designpatterns.structural.decorator;
2+
3+
/**
4+
* this is the base decorator.
5+
*/
6+
publicclassSenderDecoratorimplementsSender {
7+
privateSendersender;
8+
9+
publicSenderDecorator(Sendersender) {
10+
this.sender =sender;
11+
}
12+
13+
@Override
14+
publicStringsend(Stringcontent) {
15+
returnthis.sender.send(content);
16+
}
17+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
packagecom.designpatterns.decorator;
2+
3+
importcom.designpatterns.structural.decorator.*;
4+
importorg.junit.jupiter.api.Assertions;
5+
importorg.junit.jupiter.api.Test;
6+
7+
importjava.util.Base64;
8+
9+
publicclassDecoratorDemo {
10+
11+
@Test
12+
publicvoidtestDecorator_sendEmailAsPlainText() {
13+
Stringmessage ="test message";
14+
EmailSendersender =newEmailSender();
15+
Stringcontent =sender.send(message);
16+
Assertions.assertEquals(content,message);
17+
}
18+
19+
@Test
20+
publicvoidtestDecorator_sendEmailAsEncodedText() {
21+
Stringmessage ="test message";
22+
Sendersender =newSenderDecorator(
23+
newEncodingDecorator(
24+
newEmailSender()
25+
)
26+
);
27+
StringencodedContent =sender.send(message);
28+
Assertions.assertEquals(
29+
newString(Base64.getDecoder().decode (encodedContent)),
30+
message
31+
);
32+
}
33+
34+
@Test
35+
publicvoidtestDecorator_sendEmailAsCompressedText() {
36+
Stringmessage ="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+
Sendersender =newSenderDecorator(
38+
newCompressingDecorator(
39+
newEmailSender()
40+
)
41+
);
42+
StringcompressedContent =sender.send(message);
43+
Assertions.assertTrue(message.length()>=compressedContent.length());
44+
}
45+
46+
@Test
47+
publicvoidtestDecorator_sendEmailAsEncodedCompressedText() {
48+
Stringmessage ="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+
Sendersender =newSenderDecorator(
50+
newEncodingDecorator(
51+
newCompressingDecorator(
52+
newEmailSender()
53+
)
54+
)
55+
56+
);
57+
StringencodedCompressedContent =sender.send(message);
58+
59+
StringdecodedCompressed =newString(Base64.getDecoder().decode (encodedCompressedContent));
60+
Assertions.assertTrue(message.length()>=decodedCompressed.length());
61+
}
62+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp