【创建型】简单工厂模式详解
J.K Apr 4, 2013 2013-04-04T22:49:00+08:00
Apr 11, 2021 2021-04-11T21:54:27+08:00 4 min
【创建型】简单工厂模式
定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 简单工厂类又称为静态工厂类
简单工厂模式根据提供给的不同数据,返回不两只类的实例,但这些类有一个共同的父类。
简单工厂模式有三个要素:
1.产品接口。产品接口里定义产品的相关动作,产品接口里的定义直接决定代码的稳定性;
2.产品实现。产品实现决定了产品的具体的动作;
3.工厂类。通过传入工厂类的表态方法来决定实现化相关的产品实现;
简单工厂模式的优点;
1.工厂的作用是实例化类,调用方不需要知道调用的具体子类,降低耦合;
简单工厂模式的缺点;
1.新增加实例类型需要修改工厂,总之工厂需要知道要实例的类;
2.当子类过多,不适合使用;
|
代码
1
2
3
4
5
| public interface Fruit{
public void printName();
}
|
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
| class Apple implements Fruit{
public void printName(){
System.out.println( "My name's apple." );
}
}
class Orange implements Fruit{
public void printName(){
System.out.println( "My name's orange." );
}
}
public class Factory(){
public static Fruit getFruitInstance(String fruitName){
Fruit fruit = null;
switch(fruitName){
case "apple":
fruit = new Apple();
break;
case "orange":
fruit = new Orange();
break;
}
return fruit;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| public class Client{
public static void main(String[] args){
Fruit apple = Factory.getFruitInstance("apple");
apple.printName();
Fruit orange = Factory.getFruitInstance("orange");
orange.printName();
}
}
运行结果:
My name's apple.
My name's orange.
-----------------
|
应用
JDK中java.lang.Integer类中valueOf()就是静态工厂模式的的应用:
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
| package java.lang;
import java.util.Properties;
/**
* The {@code Integer} class wraps a value of the primitive type
* {@code int} in an object. An object of type {@code Integer}
* contains a single field whose type is {@code int}.
*
* <p>In addition, this class provides several methods for converting
* an {@code int} to a {@code String} and a {@code String} to an
* {@code int}, as well as other constants and methods useful when
* dealing with an {@code int}.
*
* <p>Implementation note: The implementations of the "bit twiddling"
* methods (such as {@link #highestOneBit(int) highestOneBit} and
* {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
* based on material from Henry S. Warren, Jr.'s <i>Hacker's
* Delight</i>, (Addison Wesley, 2002).
*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Josh Bloch
* @author Joseph D. Darcy
* @since JDK1.0
*/
public final class Integer extends Number implements Comparable<Integer> {
......
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
......
}
|
This post is licensed under
CC BY 4.0 by the author.