简单工厂模式定义
簡單工廠模式(Simple Factory Pattern)是指由一個(gè)工廠對(duì)象決定創(chuàng)建出哪一種產(chǎn)品類的實(shí)例。
屬于創(chuàng)建型模式,但它不屬于GOF,23種設(shè)計(jì)模式。
public void setCurFormpublic void setCurForm(Gw_exammingForm curForm,String parameters)throws BaseException {try {JSONObject jsonObj = new JSONObject(parameters);ExamPaper examPaper = JSONObject.parseObject(parameters,ExamPaper.class);curForm = examPaper;}catch (Exception e){e.printStackTrace();}} public interface ICourse {/*** 錄制視頻* @return*/void record(); } public class JavaCourse implements ICourse {public void record() {System.out.println("錄制Java課程");} } ICourse course = new JavaCourse(); course.record(); public class CourseFactory {public ICourse create(String name){if("java".equals(name)){return new JavaCourse();}else if("python".equals(name)){return new PythonCourse();}else {return null;}}} public class CourseFactory {public ICourse create(String className){try {if (!(null == className || "".equals(className))) {return (ICourse) Class.forName(className).newInstance();}}catch (Exception e){e.printStackTrace();}return null;}} ICourseFactory factory = new ICourseFactory(); ICourse course = factory.create("com.leon.pattern.factory.JavaCourse"); course.record(); public ICourse create(Class<? extends ICourse> clazz){try {if (null != clazz) {return clazz.newInstance();}}catch (Exception e){e.printStackTrace();}return null; } CourseFactory factory = new CourseFactory(); ICourse course = factory.create(JavaCourse.class); course.record(); public class PythonCourse implements ICourse {public void record() {System.out.println("錄制Python課程");} } Calendar.getInstance(); 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; } LoggerFactory.getLogger(SimpleFactoryTest.class); public static Logger getLogger(Class clazz) {return getLogger(clazz.getName()); } public static Logger getLogger(String name) {ILoggerFactory iLoggerFactory = getILoggerFactory();return iLoggerFactory.getLogger(name); } public interface ILoggerFactory {Logger getLogger(String var1); } public Logger getLogger(String name) {Logger slf4jLogger = null;// protect against concurrent access of loggerMapsynchronized (this) {slf4jLogger = (Logger) loggerMap.get(name);if (slf4jLogger == null) {org.apache.log4j.Logger log4jLogger;if(name.equalsIgnoreCase(Logger.ROOT_LOGGER_NAME)) {log4jLogger = LogManager.getRootLogger();} else {log4jLogger = LogManager.getLogger(name);}slf4jLogger = new Log4jLoggerAdapter(log4jLogger);loggerMap.put(name, slf4jLogger);}}return slf4jLogger;}?
總結(jié)
- 上一篇: Spring四大模块和设计模式
- 下一篇: 简单工厂适用场景