简单工厂模式

简单工厂模式

1. 概念

  • 定义:由一个工厂对象决定创建出哪一种对象的实例
  • 优缺点:
    • 优点:只需要传入一个参数,就可以获取你所需要的对象,而无需知道细节
    • 缺点:工厂的职责过重,增加新的产品,需要修改工厂的判断逻辑,违反开闭原则

2. 代码

需求: 需要根据电影类型创建电影

2.1 版本1:常规写法

1
2
3
4
5
6
public class JavaVideo extends Video {
@Override
public void produce() {
System.out.println("录制Java课程视频");
}
}
1
2
3
4
5
6
public class PythonVideo extends Video {
@Override
public void produce() {
System.out.println("录制Python课程视频");
}
}
1
2
3
4
5
6
public class Test {
public static void main(String[] args) {
PythonVideo pythonVideo = new PythonVideo();
pythonVideo.produce();
}
}

2.2 版本2: 使用简单工厂模式改造

1
2
3
4
5
6
7
8
9
10
public class VideoFactory {
public Video getVideo(String type){
if("java".equalsIgnoreCase(type)){
return new JavaVideo();
}else if("python".equalsIgnoreCase(type)){
return new PythonVideo();
}
return null;
}
}
1
2
3
4
5
6
7
8
9
10
public class Test {
public static void main(String[] args) {
VideoFactory videoFactory = new VideoFactory();
Video video = videoFactory.getVideo("python");
if(video == null){
return;
}
video.produce();
}
}

2.3 版本3: 基于反射改造,避免工厂里逻辑太复杂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class VideoFactory {
public Video getVideo(Class c){
Video video = null;
try {
video = (Video) Class.forName(c.getName()).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return video;
}
}
1
2
3
4
5
6
7
8
9
10
11
public class Test {
public static void main(String[] args) {
VideoFactory videoFactory = new VideoFactory();
Video video = videoFactory.getVideo(JavaVideo.class);
if(video == null){
return;
}
video.produce();
}
}

3 JDK的应用

JDK1.8的应用:java.util.Calendar

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
private static Calendar createCalendar(TimeZone zone,
Locale aLocale)
{
CalendarProvider provider =
LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
.getCalendarProvider();
if (provider != null) {
try {
return provider.getInstance(zone, aLocale);
} catch (IllegalArgumentException iae) {
// fall back to the default instantiation
}
}

Calendar cal = null;

if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
cal = switch (caltype) {
case "buddhist" -> new BuddhistCalendar(zone, aLocale);
case "japanese" -> new JapaneseImperialCalendar(zone, aLocale);
case "gregory" -> new GregorianCalendar(zone, aLocale);
default -> null;
};
}
}
if (cal == null) {
// If no known calendar type is explicitly specified,
// perform the traditional way to create a Calendar:
// create a BuddhistCalendar for th_TH locale,
// a JapaneseImperialCalendar for ja_JP_JP locale, or
// a GregorianCalendar for any other locales.
// NOTE: The language, country and variant strings are interned.
if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale);
} else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
&& aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
return cal;
}

简单工厂模式
http://example.com/简单工厂模式/
作者
Panyurou
发布于
2023年9月11日
许可协议