Skip to content

Commit 586e308

Browse files
committed
composite
1 parent 85afb83 commit 586e308

File tree

12 files changed

+155
-13
lines changed

12 files changed

+155
-13
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
<artifactId>hamcrest-all</artifactId>
2121
<version>1.3</version>
2222
</dependency>
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-lang3</artifactId>
26+
<version>3.1</version>
27+
</dependency>
2328
</dependencies>
2429

25-
30+
2631
</project>

src/main/java/helloworld/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import helloworld.creational.singleton.HelloWorldSingleton;
99
import helloworld.structural.adapter.HelloAdapterDesignPattern;
1010
import helloworld.structural.adapter.HelloWorldAdapter;
11+
import helloworld.structural.bridge.DesignPatternWorldImpl;
12+
import helloworld.structural.bridge.HelloWorldBridge;
13+
import helloworld.structural.composite.CompositeHelloWorld;
1114

1215
/**
1316
* @author yihua.huang@dianping.com
@@ -44,5 +47,10 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
4447
HelloWorld adapterHelloWorld = new HelloWorldAdapter(new HelloAdapterDesignPattern());
4548
System.out.println(adapterHelloWorld.helloWorld());
4649

50+
HelloWorld bridgeHelloWorld = new HelloWorldBridge(new DesignPatternWorldImpl());
51+
52+
HelloWorld compositeHelloWorld = new CompositeHelloWorld(bridgeHelloWorld, new CompositeHelloWorld.DefaultHelloWorld());
53+
System.out.println(compositeHelloWorld.helloWorld());
54+
4755
}
4856
}

src/main/java/helloworld/structural/adapter/HelloAdapterDesignPattern.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* @author yihua.huang@dianping.com
55
*/
6-
public class HelloAdapterDesignPattern implements HelloDesignPattern{
6+
public class HelloAdapterDesignPattern {
77

88
public String helloDesignPattern(){
99
return "Hello Adapter!";

src/main/java/helloworld/structural/adapter/HelloDesignPattern.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/helloworld/structural/adapter/HelloWorldAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88
public class HelloWorldAdapter implements HelloWorld{
99

10-
private HelloDesignPattern helloDesignPattern;
10+
private HelloAdapterDesignPattern helloDesignPattern;
1111

12-
public HelloWorldAdapter(HelloDesignPattern helloDesignPattern) {
12+
public HelloWorldAdapter(HelloAdapterDesignPattern helloDesignPattern) {
1313
this.helloDesignPattern = helloDesignPattern;
1414
}
1515

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package helloworld.structural.bridge;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class DesignPatternWorldImpl implements HelloWorldImpl {
7+
@Override
8+
public String generate() {
9+
return "Hello Bridge!";
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package helloworld.structural.bridge;
2+
3+
import helloworld.HelloWorld;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public class HelloWorldBridge implements HelloWorld{
9+
10+
private HelloWorldImpl helloWorldImpl;
11+
12+
public HelloWorldBridge(HelloWorldImpl helloWorldImpl) {
13+
this.helloWorldImpl = helloWorldImpl;
14+
}
15+
16+
@Override
17+
public String helloWorld() {
18+
return helloWorldImpl.generate();
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package helloworld.structural.bridge;
2+
3+
/**
4+
* Real logic of hello world!
5+
* @author yihua.huang@dianping.com
6+
*/
7+
public interface HelloWorldImpl {
8+
9+
public String generate();
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package helloworld.structural.bridge;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class JavaHelloWorldImpl implements HelloWorldImpl {
7+
@Override
8+
public String generate() {
9+
return "Hello Java!";
10+
}
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package helloworld.structural.composite;
2+
3+
import helloworld.HelloWorld;
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
/**
11+
* @author yihua.huang@dianping.com
12+
*/
13+
public class CompositeHelloWorld implements HelloWorld {
14+
15+
private List<HelloWorld> helloWorlds;
16+
17+
private String lineSeparetor = System.getProperty("line.separator");
18+
19+
public CompositeHelloWorld(HelloWorld... helloWorlds) {
20+
this.helloWorlds = Arrays.asList(helloWorlds);
21+
}
22+
23+
@Override
24+
public String helloWorld() {
25+
List<String> helloWorldOuts = new ArrayList<String>();
26+
for (HelloWorld helloWorld : helloWorlds) {
27+
helloWorldOuts.add(helloWorld.helloWorld());
28+
}
29+
return StringUtils.join(helloWorldOuts, lineSeparetor);
30+
}
31+
32+
public static class DefaultHelloWorld implements HelloWorld{
33+
34+
@Override
35+
public String helloWorld() {
36+
return "Hello Composite!";
37+
}
38+
}
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package helloworld.structural.bridge;
2+
3+
import helloworld.HelloWorld;
4+
import helloworld.structural.adapter.HelloAdapterDesignPattern;
5+
import org.junit.Test;
6+
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.hamcrest.Matchers.is;
9+
10+
/**
11+
* @author yihua.huang@dianping.com
12+
*/
13+
public class HelloWorldBridgeTest {
14+
15+
@Test
16+
public void testHelloWorldAdapter(){
17+
HelloWorld bridgeHelloWorld = new HelloWorldBridge(new JavaHelloWorldImpl());
18+
assertThat(bridgeHelloWorld.helloWorld(),is("Hello Java!"));
19+
bridgeHelloWorld = new HelloWorldBridge(new DesignPatternWorldImpl());
20+
assertThat(bridgeHelloWorld.helloWorld(),is("Hello Bridge!"));
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package helloworld.structural.composite;
2+
3+
import helloworld.HelloWorld;
4+
import helloworld.structural.bridge.DesignPatternWorldImpl;
5+
import helloworld.structural.bridge.HelloWorldBridge;
6+
import helloworld.structural.bridge.JavaHelloWorldImpl;
7+
import org.junit.Test;
8+
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.Matchers.is;
11+
import static org.hamcrest.Matchers.isEmptyString;
12+
13+
/**
14+
* @author yihua.huang@dianping.com
15+
*/
16+
public class CompositeHelloWorldTest {
17+
18+
@Test
19+
public void testCompositeHelloWorld(){
20+
HelloWorld emptyCompositeHelloWorld = new CompositeHelloWorld();
21+
assertThat(emptyCompositeHelloWorld.helloWorld(),isEmptyString());
22+
HelloWorld compositeHelloWorld = new CompositeHelloWorld(new CompositeHelloWorld.DefaultHelloWorld());
23+
assertThat(compositeHelloWorld.helloWorld(),is("Hello Composite!"));
24+
}
25+
}

0 commit comments

Comments
 (0)