本文實例講述了java使用抽象工廠模式實現(xiàn)的肯德基消費案例。分享給大家供大家參考,具體如下:
一、模式定義
抽象工廠模式提供了一個接口,用于創(chuàng)建相關或者依賴對象的家族,而不需要指定具體實現(xiàn)類。
抽象工廠模式允許客戶使用抽象接口來創(chuàng)建一組相關的產品,客戶類和工廠類分開,客戶需要任何產品的時候,只需要向工廠請求即可,客戶無須修改就可以獲得新產品。
二、模式舉例
1 模式分析
我們借用爸爸和兒子到肯德基店消費這一場景來說明這一模式,進行抽象分析后的截圖如下
2 抽象工廠模式的靜態(tài)建模
3 代碼示例
3.1 抽象食物的建立
抽象食物——abstractbasefood
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.demo.factory.model; /** * * 食物基類 * * @author maofw * */ public abstract class abstractbasefood { // 類別 protected string kind; // 數量 protected int num; // 價格 protected float price; // 合計 public float totalprice() { return this .num * this .price; } } |
食物接口——ifood
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.demo.factory.model; /** * 抽象食物接口 * * @author maofw * */ public interface ifood { /** * 打印輸出食物信息 */ void printmesage(); } |
3.2 建立不同食物的抽象基類
漢堡基類——hamburg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.demo.factory.model; /** * 漢堡基類 * * @author maofw * */ public abstract class hamburg extends abstractbasefood implements ifood { public void printmesage() { system.out.println( "--" + this .kind + "風味漢堡,\t單價:" + this .price + ",\t數量:" + this .num + ",\t合計:" + this .totalprice()); } } |
雞翅基類——chickenwings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.demo.factory.model; /** * 雞翅基類 * * @author maofw * */ public abstract class chickenwings extends abstractbasefood implements ifood { public void printmesage() { system.out.println( "--" + this .kind + "風味雞翅,\t單價:" + this .price + ",\t數量:" + this .num + ",\t合計:" + this .totalprice()); } } |
薯條基類——frenchfries
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.demo.factory.model; /** * 薯條基類 * * @author maofw * */ public abstract class frenchfries extends abstractbasefood implements ifood { public void printmesage() { system.out.println( "--" + this .kind + "風味薯條,\t單價:" + this .price + ",\t數量:" + this .num + ",\t合計:" + this .totalprice()); } } |
飲料基類——beverage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.demo.factory.model; /** * 飲料基類 * * @author maofw * */ public abstract class beverage extends abstractbasefood implements ifood { public void printmesage() { system.out.println( "--" + this .kind + "飲料,\t單價:" + this .price + ",\t數量:" + this .num + ",\t合計:" + this .totalprice()); } } |
3.3 創(chuàng)建具體的食物
麻辣雞腿漢堡——chinahanburm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.demo.factory.model.kfc; import com.demo.factory.model.hamburg; /** * 中國風味的麻辣雞腿漢堡 * * @author maofw * */ public class chinahanburm extends hamburg { /** * 構造方法 * * @param kind * @param price * @param num */ public chinahanburm( int num) { this .kind = "麻辣" ; this .price = 14 .0f; this .num = num; } } |
奧爾良雞翅——chinachickenwings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.demo.factory.model.kfc; import com.demo.factory.model.chickenwings; /** * 雞翅實現(xiàn)類 * * @author maofw * */ public class chinachickenwings extends chickenwings { public chinachickenwings( int num) { this .kind = "奧爾良" ; this .price = 2 .5f; this .num = num; } } |
薯條——chinafrenchfries
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.demo.factory.model.kfc; import com.demo.factory.model.frenchfries; /** * 薯條實現(xiàn)類 * * @author maofw * */ public class chinafrenchfries extends frenchfries { public chinafrenchfries( int num) { this .kind = "普通" ; this .price = 8 .0f; this .num = num; } } |
可樂——chinabeverage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.demo.factory.model.kfc; import com.demo.factory.model.beverage; /** * 飲料實現(xiàn)類 * * @author maofw * */ public class chinabeverage extends beverage { public chinabeverage( int num) { this .kind = "可樂" ; this .price = 7 .0f; this .num = num; } } |
3.4 建立工廠
創(chuàng)建抽象肯德基工廠——ikfcfactory 生產抽象食物
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.demo.factory.itf; import com.demo.factory.model.beverage; import com.demo.factory.model.chickenwings; import com.demo.factory.model.frenchfries; import com.demo.factory.model.hamburg; /** * 肯德基抽象工廠基類 * * @author maofw * */ public interface ikfcfactory { // 生產漢堡 public hamburg createhamburg( int num); // 生產薯條 public frenchfries createfrenchfries( int num); // 生產雞翅 public chickenwings createchickenwings( int num); // 生產飲料 public beverage createbeverage( int num); } |
創(chuàng)建具體肯德基工廠——chinakfcfactory 生產具體食物
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
|
package com.demo.factory.itf; import com.demo.factory.model.beverage; import com.demo.factory.model.chickenwings; import com.demo.factory.model.frenchfries; import com.demo.factory.model.hamburg; import com.demo.factory.model.kfc.chinabeverage; import com.demo.factory.model.kfc.chinachickenwings; import com.demo.factory.model.kfc.chinafrenchfries; import com.demo.factory.model.kfc.chinahanburm; public class chinakfcfactory implements ikfcfactory { // 生產可樂 public beverage createbeverage( int num) { return new chinabeverage(num); } // 生產奧爾良烤雞翅 public chickenwings createchickenwings( int num) { return new chinachickenwings(num); } // 生產薯條 public frenchfries createfrenchfries( int num) { return new chinafrenchfries(num); } // 生產麻辣風味雞腿漢堡 public hamburg createhamburg( int num) { return new chinahanburm(num); } } |
3.5 創(chuàng)建客戶類——customer
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
|
package com.demo.factory.custom; import com.demo.factory.itf.ikfcfactory; import com.demo.factory.model.beverage; import com.demo.factory.model.chickenwings; import com.demo.factory.model.frenchfries; import com.demo.factory.model.hamburg; /** * 客戶類 * * @author maofw * */ public class customer { // 抽象工廠 private ikfcfactory kfcfactory; // 構造方法將抽象工廠作為參數傳入 public customer(ikfcfactory kfcfactory) { this .kfcfactory = kfcfactory; } /** * 訂購食物 */ // 訂購麻辣雞腿漢堡 public float orderhamburg( int num) { // 獲得麻辣雞腿漢堡 hamburg hamburg = kfcfactory.createhamburg(num); // 輸出訂購信息 hamburg.printmesage(); // 返回總價 return hamburg.totalprice(); } // 訂購奧爾良烤雞翅 public float orderchickenwings( int num) { // 獲得奧爾良烤雞翅 chickenwings chickenwings = kfcfactory.createchickenwings(num); // 輸出訂購信息 chickenwings.printmesage(); // 返回總價 return chickenwings.totalprice(); } // 訂購薯條 public float orderfrenchfries( int num) { // 獲得薯條 frenchfries frenchfries = kfcfactory.createfrenchfries(num); // 輸出訂購信息 frenchfries.printmesage(); // 返回總價 return frenchfries.totalprice(); } // 訂購可樂 public float orderbeverage( int num) { // 獲得可樂 beverage beverage = kfcfactory.createbeverage(num); // 輸出訂購信息 beverage.printmesage(); // 返回總價 return beverage.totalprice(); } } |
3.6 故事情節(jié)展現(xiàn)
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
|
package com.demo.factory; import java.lang.management.managementfactory; import java.net.httpurlconnection; import java.net.url; import java.net.urlconnection; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.sqlexception; import java.text.numberformat; import java.util.arrays; import java.util.calendar; import java.util.resourcebundle; import com.demo.factory.custom.customer; import com.demo.factory.itf.chinakfcfactory; import com.demo.factory.itf.ikfcfactory; public class mainapp { /** * 主應用程序方法 * * @param args */ public static void main(string[] args) { /** * 定義一個肯德基(ikfcfactory類型) */ ikfcfactory kfcfactory = new chinakfcfactory(); /** * 爸爸和兒子走進肯德基,準備點餐 */ customer customer = new customer(kfcfactory); /** * 開始點餐 */ // 一個麻辣雞腿漢堡 float hamhurgmoney = customer.orderhamburg( 1 ); // 四個奧爾良烤雞翅 float chickenwingsmoney = customer.orderchickenwings( 4 ); // 一包薯條 float frenchfriesmoney = customer.orderfrenchfries( 1 ); // 兩杯可樂 float beveragemoney = customer.orderbeverage( 2 ); system.out.println( "總計:" + (hamhurgmoney + chickenwingsmoney + frenchfriesmoney + beveragemoney)); } } |
運行結果:
--麻辣風味漢堡, 單價:14.0, 數量:1, 合計:14.0
--奧爾良風味雞翅, 單價:2.5, 數量:4, 合計:10.0
--普通風味薯條, 單價:8.0, 數量:1, 合計:8.0
--可樂飲料, 單價:7.0, 數量:2, 合計:14.0
總計:46.0
三、該模式的設計原則
1 多用對象組合,少用繼承
2 針對抽象編程,不針對實現(xiàn)編程
3 產品對象通過工廠暴露的方法創(chuàng)建
四、使用場合
1 創(chuàng)建產品家族,相關產品集合在一起使用的時候;
2 想要提供一個產品類庫,并只想顯示其接口而不是實現(xiàn)時;
3 通過組合的方式使用工廠時。
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/chengqiuming/article/details/70139260