鱼跃于渊

First know how, Second know why !
posts - 0, comments - 1, trackbacks - 0, articles - 49

简单工厂模式

Posted on 2008-12-20 18:19 鱼跃于渊 阅读(300) 评论(0)  编辑  收藏 所属分类: 设计模式
工厂模式就是设计一个工厂类, 生产(返回类的实例)一组实现了一个共同接口的类的实例的方法.

第一种设计: 为每一个类设计一个工厂
 1 package com.givetop.singleton;
 2 
 3 public class Factory1 {
 4     public static void main(String[] args){
 5         Ford_Factory.getInstance().run();
 6         Benz_Factory.getInstance().stop();
 7     }
 8 }
 9 
10 interface Car{
11     public void run();
12     public void stop();
13 }
14 
15 class Ford implements Car{
16     public void run(){
17         System.out.println("Ford开始启动了!");
18     }
19     public void stop(){
20         System.out.println("Ford停止了!");
21     }
22 }
23 
24 class Benz implements Car{
25     public void run(){
26         System.out.println("Benz开始启动了!");
27     }
28     public void stop(){
29         System.out.println("Benz停止了!");
30     }
31 }
32 
33 class Ford_Factory{
34     public static Car getInstance(){
35         return new Ford();
36     }
37 }
38 
39 class Benz_Factory{
40     public static Car getInstance(){
41         return new Benz();
42     }
43 }
44 

为每一个类设计一个工厂, 显然这大的增加了代码量,  不是好的设计!

--------------------------------------------------------------
第二种方法:  为实现了同一接口的类创建一个公共的工厂(而这里的各种车刚好都实现了Car这个接口)
 1 package com.givetop.singleton;
 2 
 3 public class Factory2 {
 4     public static void main(String[] args){
 5         Factory.getInstance("Benz").run();
 6         /*
 7         Car car = Factory.getInstance("fsdf");
 8         if(car!=null){
 9             car.run();
10             car.stop();
11         }else{
12             System.out.println("不能造这种车!");
13         }
14         */
15     }
16 }
17 
18 interface Car1{
19     public void run();
20     public void stop();
21 }
22 
23 class Ford1 implements Car{
24     public void run(){
25         System.out.println("Ford1开始启动了!");
26     }
27     public void stop(){
28         System.out.println("Ford1停止了!");
29     }
30 }
31 
32 class Benz1 implements Car{
33     public void run(){
34         System.out.println("Benz1开始启动了!");
35     }
36     public void stop(){
37         System.out.println("Benz1停止了!");
38     }
39 }
40 
41 class Factory{
42     public static Car getInstance(String type){
43         Car c = null;
44         if("Ford".equals(type)){
45             c = new Ford1();
46         }
47         if("Benz".equals(type)){
48             c = new Benz1();
49         }
50         return c;
51     }
52 }
53 
54 

//这样仍然有一个不好的地方, 就是传入一个工厂不能造的东西会报错, 必须在客户端判断一下!
//在扩充子类的时候要修改工厂,用反射机制可以解决这一问题!

第三种运用反射机制 
 1 package com.givetop.singleton;
 2 
 3 public class Factory3 {
 4     public static void main(String[] args){
 5         Car_Factory.getInstance("Benz2").run();
 6         Car2 c = null ;
 7         c = Car_Factory.getInstance("Ford2");
 8         c.run();
 9         c.stop();
10         c=Car_Factory.getInstance("Honda");
11         if(c==null){
12             System.out.println("不能造Honda车!");
13         }
14     }
15 }
16 
17 interface Car2{
18     public void run();
19     public void stop();
20 }
21 
22 class Ford2 implements Car2{
23     public void run(){
24         System.out.println("Ford开始启动了!");
25     }
26     public void stop(){
27         System.out.println("Ford停止了!");
28     }
29 }
30 
31 class Benz2 implements Car2{
32     public void run(){
33         System.out.println("Benz开始启动了!");
34     }
35     public void stop(){
36         System.out.println("Benz停止了!");
37     }
38 }
39 
40 class Car_Factory{
41     public static Car2 getInstance(String type){
42         Car2 c = null;
43         try {
44             c = (Car2)Class.forName("com.givetop.singleton."+type).newInstance();
45         } catch (InstantiationException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         } catch (IllegalAccessException e) {
49             // TODO Auto-generated catch block
50             e.printStackTrace();
51         } catch (ClassNotFoundException e) {
52             // TODO Auto-generated catch block
53             //e.printStackTrace();
54             System.out.println("error here!");
55         }
56         return c;
57     }
58     
59 }
60 

显然这样在增加一个子类(如在此例中加一个Honda的类时)不需要修改工厂方法!

只有注册用户登录后才能发表评论。


网站导航: