在面向對象編程的程序設計中,我們最常見的操作就是new對象,但在創建一個新對象的過程中,會有一些問題,比如我們需要注意創建新對象的實現細節,初始化一些必要的參數等。這樣會讓我們在講更多的心思放在對象的創建上,而不是程序邏輯的實現上,嚴重拖延了我們的程序開發效率。工廠模式和抽象工廠模式的出現則完美解決了這個問題,讓我們不再關心對象的創建,更多的在重心放在業務的實現上。
特點:
1、程序員直接通過工廠方法創建對象,不再關注創建對象的細節。
2、隱藏對象的實現細節,也有利于程序的安全性。
3、降低程序耦合度。
企業級開發和常見框架中的應用:
Hibernate中的sessionfactory等
工廠模式分類:
簡單工廠模式,程序開發中最常用的形式,具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class Demo { /** * demo這個類就是我們平時的操作類,在這個類中我們不用去關心 創建汽車的實現細節 */ public static void main(String[] args) { Car car = CarFactory.createCar( "dz" ); car.run(); Car car2 = CarFactory.createCar( "at" ); car2.run(); } } interface Car{ public void run(); } class Dz implements Car{ public void run() { System.out.println( "大眾汽車在跑" ); } } class At implements Car{ public void run() { System.out.println( "奧拓汽車在跑" ); } } class CarFactory{ public static Car createCar(String type){ if ( "dz" .equals(type)){ System.out.println( "創建了一個大眾車" ); return new Dz(); } if ( "at" .equals(type)){ System.out.println( "創建了一個奧拓車" ); return new At(); } return null ; } } |
工廠方法模式,相比于簡單工廠模式,方便擴展,不必去修改以前的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
public class Demo { /** * demo這個類就是我們平時的操作類,在這個類中我們不用去關心 創建汽車的實現細節 */ public static void main(String[] args) { AtFactory atFactory = new AtFactory(); DzFactory dzFactory = new DzFactory(); Car at = atFactory.createCar(); Car dz = dzFactory.createCar(); at.run(); dz.run(); } } interface Car { public void run(); } class Dz implements Car { public void run() { System.out.println( "大眾汽車在跑" ); } } class At implements Car { public void run() { System.out.println( "奧拓汽車在跑" ); } } interface CarFactory { Car createCar(); } class DzFactory implements CarFactory { public Car createCar() { return new Dz(); } } class AtFactory implements CarFactory { public Car createCar() { return new At(); } } |
抽象工廠方法模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
public class Demo { public static void main(String[] args) { Car carFactory = new GDCarFactory(); FDZ fdz = carFactory.createFdz(); fdz.zhuansu(); } } interface FDZ { void zhuansu(); } class GDFDZ implements FDZ { public void zhuansu() { System.out.println( "高端發動機轉速快" ); } } class DDFDZ implements FDZ { public void zhuansu() { System.out.println( "低端發動機轉速慢" ); } } interface ZY { void shushidu(); } class GDZY implements ZY { public void shushidu() { System.out.println( "高端座椅很舒適" ); } } class DDZY implements ZY { public void shushidu() { System.out.println( "低端座椅不舒適" ); } } interface LT { void mosundu(); } class GDLT implements LT { public void mosundu() { System.out.println( "高端輪胎不磨損" ); } } class DDLT implements LT { public void mosundu() { System.out.println( "低端輪胎磨損快" ); } } interface Car { FDZ createFdz(); ZY createZy(); LT createLt(); } class GDCarFactory implements Car{ @Override public FDZ createFdz() { return new GDFDZ(); } @Override public ZY createZy() { return new GDZY(); } @Override public LT createLt() { return new GDLT(); } } class DDCarFactory implements Car{ @Override public FDZ createFdz() { return new DDFDZ(); } @Override public ZY createZy() { return new DDZY(); } @Override public LT createLt() { return new DDLT(); } } |
三種方法的比較:
1、簡單工廠模式:簡單工廠模式設計簡單,代碼量少,但是可擴展性卻很差,需要擴展時需要修改以前的代碼
2、工廠方法模式:擴展性強,但增加了代碼復雜度
3、抽象工廠模式:抽象工廠模式和工廠模式是不同,抽象工廠模式是對產品分等級,但工廠模式是對產品分類,舉個汽車的例子:工廠模式是生產不同品種的汽車,比如奧迪和大眾,而抽象工廠模式則是對同一款汽車進行等級劃分,比如同樣都是大眾汽車,我們分了高端車和低端車。從方法上講抽象工廠模式更像是工廠模式的細化。一個針對的不不同產品,一個針對的是同一個產品家族。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。