Skip to content

Commit 31bb63e

Browse files
committed
strategy
1 parent 9a417d5 commit 31bb63e

File tree

7 files changed

+76
-17
lines changed

7 files changed

+76
-17
lines changed

src/main/java/helloworld/Main.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import helloworld.behavioral.observer.HelloWorldObserver;
1515
import helloworld.behavioral.observer.Subject;
1616
import helloworld.behavioral.state.HelloWorldStateContext;
17+
import helloworld.behavioral.strategy.DesignPatternHelloWorldStrategy;
18+
import helloworld.behavioral.strategy.HelloWorldStrategyContext;
1719
import helloworld.creational.abstract_factory.AbstractFactory;
1820
import helloworld.creational.abstract_factory.SplitHelloWorldFactory;
1921
import helloworld.creational.builder.HelloWorldBuilder;
@@ -124,5 +126,8 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
124126
HelloWorld stateHelloWorld = helloWorldStateContext.appendWord("Hello").appendWord("State");
125127
System.out.println(stateHelloWorld.helloWorld());
126128

129+
HelloWorldStrategyContext helloWorldStrategyContext = new HelloWorldStrategyContext(new DesignPatternHelloWorldStrategy());
130+
System.out.println(helloWorldStrategyContext.helloWorld());
131+
127132
}
128133
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package helloworld.behavioral.strategy;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class DesignPatternHelloWorldStrategy implements HelloWorldStrategy{
7+
@Override
8+
public String helloWorld() {
9+
return "Hello Strategy!";
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package helloworld.behavioral.strategy;
2+
3+
import helloworld.HelloWorld;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public interface HelloWorldStrategy extends HelloWorld {
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package helloworld.behavioral.strategy;
2+
3+
import helloworld.HelloWorld;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public class HelloWorldStrategyContext implements HelloWorld{
9+
10+
private HelloWorldStrategy helloWorldStrategy;
11+
12+
public HelloWorldStrategyContext(HelloWorldStrategy helloWorldStrategy) {
13+
this.helloWorldStrategy = helloWorldStrategy;
14+
}
15+
16+
@Override
17+
public String helloWorld() {
18+
return helloWorldStrategy.helloWorld();
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package helloworld.behavioral.strategy;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class JavaHelloWorldStrategy implements HelloWorldStrategy{
7+
@Override
8+
public String helloWorld() {
9+
return "Hello Java!";
10+
}
11+
}

src/main/java/helloworld/behavioral/strategy/StrategyHelloWorld.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package helloworld.behavioral.strategy;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.is;
7+
8+
/**
9+
* @author yihua.huang@dianping.com
10+
*/
11+
public class HelloWorldStrategyContextTest {
12+
13+
@Test
14+
public void testHelloWorldStrategyContext(){
15+
HelloWorldStrategyContext helloWorldStrategyContext = new HelloWorldStrategyContext(new JavaHelloWorldStrategy());
16+
assertThat(helloWorldStrategyContext.helloWorld(),is("Hello Java!"));
17+
helloWorldStrategyContext = new HelloWorldStrategyContext(new DesignPatternHelloWorldStrategy());
18+
assertThat(helloWorldStrategyContext.helloWorld(),is("Hello Strategy!"));
19+
}
20+
}

0 commit comments

Comments
 (0)