定义:
由一个工厂类决定生产哪一种产品的实例
应用场景
- 工厂要生产的产品种类不多
- 客户端对生产细节并不关心,只关注传入参数,拿到对象。
优点
- 根据正确的参数就能生产出产品实例
- 将生产对象的细节与客户端(应用层)解耦
源码应用的场景
- jdk中Calendar类的createCalendar
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {
public static Calendar getInstance()
{
return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
}
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) {
switch (caltype) {
case "buddhist":
cal = new BuddhistCalendar(zone, aLocale);
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case "gregory":
cal = new GregorianCalendar(zone, aLocale);
break;
}
}
}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;
}
}
demo
生产产品的父类
package creational.simplefactory;
public class Video {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Video{" +
"name='" + name + '\'' +
'}';
}
}
产品类①
package creational.simplefactory;
public class MySqlVideo extends Video{
public MySqlVideo(){
super.setName("MySqlVideo");
}
}
产品类②
package creational.simplefactory;
public class JavaVideo extends Video {
public JavaVideo(){
super.setName("JavaVideo");
}
}
工厂类
package creational.simplefactory;
import org.springframework.util.StringUtils;
public class VideoFactory {
/**
* if not a realNAME return null.else return a obj
* @param name
* @return
*/
public static Video createVideo(String name){
if(StringUtils.isEmpty(name)){
return null;
}
Video returnObj = null;
if(JavaVideo.class.getName().equals(name)){
returnObj = new JavaVideo();
}else if(MySqlVideo.class.getName().equals(name)){
returnObj = new MySqlVideo();
}
return returnObj;
}
}
测试类
package creational.simplefactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
Video video = VideoFactory.createVideo(JavaVideo.class.getName());
System.out.println(video);
video = VideoFactory.createVideo(MySqlVideo.class.getName());
System.out.println(video);
/**
* 获取日历对象方法的 根据地区获取不同的日历子类
*/
Calendar calendar =Calendar.getInstance();
Connection connection = null;
try {
DriverManager.getConnection("", "", "");
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(null != connection){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}