Skip to content

Commit 3ffedbc

Browse files
committed
abstact factory
1 parent 0370be2 commit 3ffedbc

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-0
lines changed

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>us.codecraft</groupId>
8+
<artifactId>hello-design-pattern</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.11</version>
16+
<scope>test</scope>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.hamcrest</groupId>
20+
<artifactId>hamcrest-all</artifactId>
21+
<version>1.3</version>
22+
</dependency>
23+
</dependencies>
24+
25+
26+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package helloworld;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public interface HelloWorld {
7+
8+
public String helloWorld();
9+
}

src/main/java/helloworld/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package helloworld;
2+
3+
import helloworld.abstract_factory.AbstractFactory;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public class Main {
9+
10+
public static void main(String[] args) {
11+
HelloWorld helloWorld = AbstractFactory.factory().createHelloWorld();
12+
System.out.println(helloWorld.helloWorld());
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package helloworld.abstract_factory;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class AbstractFactory {
7+
8+
public static HelloWorldFactory factory(){
9+
return new DesignPatternHelloWorldFactory();
10+
}
11+
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package helloworld.abstract_factory;
2+
3+
import helloworld.HelloWorld;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public class DesignPatternHelloWorldFactory implements HelloWorldFactory{
9+
@Override
10+
public HelloWorld createHelloWorld() {
11+
return new DesignPatternHelloWorld();
12+
}
13+
14+
class DesignPatternHelloWorld implements HelloWorld{
15+
16+
@Override
17+
public String helloWorld() {
18+
return "Hello world, Abstract Factory!";
19+
}
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package helloworld.abstract_factory;
2+
3+
import helloworld.HelloWorld;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public interface HelloWorldFactory {
9+
10+
public HelloWorld createHelloWorld();
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package helloworld.abstract_factory;
2+
3+
import helloworld.HelloWorld;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public class JavaHelloWorldFactory implements HelloWorldFactory{
9+
@Override
10+
public HelloWorld createHelloWorld() {
11+
return new JavaHelloWorld();
12+
}
13+
14+
class JavaHelloWorld implements HelloWorld{
15+
16+
@Override
17+
public String helloWorld() {
18+
return "Hello world, Java!";
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package helloworld.abstract_factory;
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 AbstractFactoryTest {
12+
13+
@Test
14+
public void testHelloWorld(){
15+
HelloWorldFactory helloWorldFactory = new JavaHelloWorldFactory();
16+
assertThat(helloWorldFactory.createHelloWorld().helloWorld(), is("Hello world, Java!"));
17+
helloWorldFactory = new DesignPatternHelloWorldFactory();
18+
assertThat(helloWorldFactory.createHelloWorld().helloWorld(), is("Hello world, Abstract Factory!"));
19+
}
20+
21+
}

0 commit comments

Comments
 (0)