Posted on 2008-12-20 18:19
魚躍于淵 閱讀(312)
評論(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
為每一個類設計一個工廠, 顯然這大的增加了代碼量, 不是好的設計!
--------------------------------------------------------------
第二種方法: 為實現了同一接口的類創(chuàng)建一個公共的工廠(而這里的各種車剛好都實現了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的類時)不需要修改工廠方法!