工厂模式

1 public class Main
2 {
3     public static void main(String[] args)
4     {
5         Itest testA=Factory.creator(1);
6         Itest testB=Factory.creator(2);
7     }
8 }
 1 public class Factory
 2 {
 3     public static Itest creator(int which)
 4     {
 5         if (which==1)
 6         {
 7             return new TestA();
 8         }
 9         else if (which==2)
10         {
11             return new TestA();
12         }
13         return null;    
14     }
15 }
1 public class TestA implements Itest
2 {
3     @Override
4     public void init()
5     {
6         
7     }
8 }
1 public class TestB implements Itest
2 {
3     @Override
4     public void init()
5     {
6         
7     }
8 }
1 public interface Itest
2 {
3     public void init(); 
4 }

 

posted @ 2018-01-01 15:30  明天会更好!!!!  阅读(85)  评论(0)    收藏  举报