File tree Expand file tree Collapse file tree
main/java/cn/com/nianduen
test/java/cn/com/nianduen Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments