Skip to content

Commit b86eb7b

Browse files
committed
add abstract factory patterns and singleton factory
1 parent c9090f8 commit b86eb7b

10 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.com.nianduen.absfactory;
2+
3+
/**
4+
* Created by rachoochen on 8/19/14.
5+
*/
6+
public abstract class AbsHumanFactory {
7+
8+
public abstract Human createHuman(Class<? extends Human> c);
9+
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.com.nianduen.absfactory;
2+
3+
/**
4+
* Created by rachoochen on 8/19/14.
5+
*/
6+
public class African implements Human{
7+
8+
@Override
9+
public void getColor() {
10+
System.out.println("I am black");
11+
}
12+
13+
@Override
14+
public void talk() {
15+
System.out.println("ji li gua la");
16+
}
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.com.nianduen.absfactory;
2+
3+
/**
4+
* Created by rachoochen on 8/19/14.
5+
*/
6+
public class Asians implements Human{
7+
8+
@Override
9+
public void getColor() {
10+
System.out.println("I am yellow");
11+
}
12+
13+
@Override
14+
public void talk() {
15+
System.out.println("i speak Chinese");
16+
}
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.com.nianduen.absfactory;
2+
3+
/**
4+
* Created by rachoochen on 8/19/14.
5+
*/
6+
public class Europeans implements Human{
7+
8+
@Override
9+
public void getColor() {
10+
System.out.println("i am white");
11+
}
12+
13+
@Override
14+
public void talk() {
15+
System.out.println("i speak English");
16+
}
17+
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.com.nianduen.absfactory;
2+
3+
/**
4+
* Created by rachoochen on 8/19/14.
5+
*/
6+
public interface Human {
7+
8+
public void getColor();
9+
10+
public void talk();
11+
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.com.nianduen.absfactory;
2+
3+
/**
4+
* Created by rachoochen on 8/19/14.
5+
*/
6+
public class HumanFactory extends AbsHumanFactory {
7+
8+
@Override
9+
public Human createHuman(Class<? extends Human> c) {
10+
Human human = null;
11+
try {
12+
human = (Human) Class.forName(c.getName()).newInstance();
13+
} catch (Exception e) {
14+
e.printStackTrace();
15+
}
16+
return human;
17+
}
18+
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.com.nianduen.singleton;
2+
3+
import java.lang.reflect.Constructor;
4+
5+
/**
6+
* Created by rachoochen on 8/19/14.
7+
*/
8+
public class SingletonFactory {
9+
10+
private static SingletonTarget singleton;
11+
12+
static {
13+
try {
14+
Class c = Class.forName(SingletonTarget.class.getName());
15+
Constructor constructor = c.getDeclaredConstructor();
16+
constructor.setAccessible(true);
17+
singleton = (SingletonTarget) constructor.newInstance();
18+
} catch (Exception e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
23+
public static SingletonTarget getInstance(){
24+
return singleton;
25+
}
26+
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.com.nianduen.singleton;
2+
3+
/**
4+
* Created by rachoochen on 8/19/14.
5+
*/
6+
public class SingletonTarget {
7+
8+
private SingletonTarget(){}
9+
10+
public void doMethod(){
11+
System.out.println("hello world, i am create by singleton factory");
12+
}
13+
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.com.nianduen.absfactory;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class HumanFactoryTest {
11+
12+
@Test
13+
public void testCreateHuman() throws Exception {
14+
15+
AbsHumanFactory humanFactory = new HumanFactory();
16+
Human african = humanFactory.createHuman(African.class);
17+
Human asians = humanFactory.createHuman(Asians.class);
18+
Human europeans = humanFactory.createHuman(Europeans.class);
19+
20+
List<Human> list = new ArrayList();
21+
list.add(african);
22+
list.add(asians);
23+
list.add(europeans);
24+
25+
for (Human human : list){
26+
human.getColor();
27+
human.talk();
28+
}
29+
30+
}
31+
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.com.nianduen.singleton;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class SingletonFactoryTest {
8+
9+
@Test
10+
public void testGetInstance() throws Exception {
11+
12+
SingletonTarget target1 = SingletonFactory.getInstance();
13+
SingletonTarget target2 = SingletonFactory.getInstance();
14+
System.out.println(target1);
15+
System.out.println(target2);
16+
17+
}
18+
}

0 commit comments

Comments
 (0)