本文主要是介绍彻底搞懂设计模式DesignPattern-工厂模式-简单工厂-工厂方法-静态工厂-抽象工厂,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简单工厂
普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。
最简单的总结就是一个工厂全负责,里面有判断,用于生产实例
- 简单工厂模式是属于创建型模式,是工厂模式的一种。 简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式
- 简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)
- 在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式.
首先,创建二者的共同接口:
public interface Sender { public void Send();
} 其次,创建实现类:
public class MailSender implements Sender { @Override public void Send() { System.out.println("this is mailsender!"); }
}
public class SmsSender implements Sender { @Override public void Send() { System.out.println("this is sms sender!"); }
} 最后,建工厂类:工厂自带判断生成实例
public class SendFactory { public Sender produce(String type) { if ("mail".equals(type)) { return new MailSender(); } else if ("sms".equals(type)) { return new SmsSender(); } else { System.out.println("请输入正确的类型!"); return null; } }
} 测试:
public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.produce("sms"); sender.Send(); }
}
输出:this is sms sender
工厂方法
多个工厂方法模式,是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。
定义了一个创建对象的抽象方法,由子类决定要实例化的类。工厂方
法模式将对象的实例化推迟到子类。
总结
顶级工厂做成抽象,子类工厂继承顶级工厂,由子类决定,推迟到子类实例化。
//改动下SendFactory类
public class SendFactory { //这里应该是子工厂去继承顶级工厂,子工厂干具体的活public Sender produceMail(){ return new MailSender(); } //这里是sms子工厂继承顶级工厂public Sender produceSms(){ return new SmsSender(); }
} 测试类如下:
public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.produceMail(); sender.Send(); }
} 输出:this is mailsender!
静态工厂
将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。
public class SendFactory { public static Sender produceMail(){ return new MailSender(); } public static Sender produceSms(){ return new SmsSender(); }
} public class FactoryTest { public static void main(String[] args) { Sender sender = SendFactory.produceMail(); sender.Send(); }
} 输出:this is mailsender!
抽象工厂
创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码
- 抽象工厂模式:定义了一个interface用于创建相关或有依赖关系的对象簇,而无需指明具体的类
- 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合。
- 从设计层面看,抽象工厂模式就是对简单工厂模式的改进(或者称为进一步的抽象)。
- 将工厂抽象成两层, AbsFactory(抽象工厂) 和 具体实现的工厂子类。程序员可以根据创建对象类型使用对应的工厂子类。 这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。
顶级工厂
public interface Sender { public void Send();
} 两个实现类:
public class MailSender implements Sender { @Override public void Send() { System.out.println("this is mailsender!"); }
} public class SmsSender implements Sender { @Override public void Send() { System.out.println("this is sms sender!"); }
} 两个工厂类:
public class SendMailFactory implements Provider { @Override public Sender produce(){ return new MailSender(); }
} public class SendSmsFactory implements Provider{ @Override public Sender produce() { return new SmsSender(); }
} 在提供一个接口:
public interface Provider { public Sender produce();
} 测试类:
public class Test { public static void main(String[] args) { Provider provider = new SendMailFactory(); Sender sender = provider.produce(); sender.Send(); }
}
不懂的再看看尚硅谷的或者马士兵的教程就行了。
Calendar
Calendar类的功能要比Date类强大很多,提供了一组对年月日时分秒星期等日期信息的操作的函数,并针对不同国家和地区的日历提供了相应的子类,即本地化。比如公历GregorianCalendar,佛历BuddhistCalendar,日本帝国历JapaneseImperialCalendar等。
Calendar类和Date类不同,它是一个抽象类,不能new
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar>
getInstance
getInstance方法有四种重载,两个参数的组合弄出来四种重载。
/*** Gets a calendar using the default time zone and locale. The* <code>Calendar</code> returned is based on the current time* in the default time zone with the default* {@link Locale.Category#FORMAT FORMAT} locale.** @return a Calendar.*/public static Calendar getInstance(){return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));}/*** Gets a calendar using the specified time zone and default locale.* The <code>Calendar</code> returned is based on the current time* in the given time zone with the default* {@link Locale.Category#FORMAT FORMAT} locale.** @param zone the time zone to use* @return a Calendar.*/public static Calendar getInstance(TimeZone zone){return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));}/*** Gets a calendar using the default time zone and specified locale.* The <code>Calendar</code> returned is based on the current time* in the default time zone with the given locale.** @param aLocale the locale for the week data* @return a Calendar.*/public static Calendar getInstance(Locale aLocale){return createCalendar(TimeZone.getDefault(), aLocale);}/*** Gets a calendar with the specified time zone and locale.* The <code>Calendar</code> returned is based on the current time* in the given time zone with the given locale.** @param zone the time zone to use* @param aLocale the locale for the week data* @return a Calendar.*/public static Calendar getInstance(TimeZone zone,Locale aLocale){return createCalendar(zone, aLocale);}
/*** Gets a calendar using the default time zone and locale. The* <code>Calendar</code> returned is based on the current time* in the default time zone with the default* {@link Locale.Category#FORMAT FORMAT} locale.** @return a 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;}
通过TimeZone(时区)和Locale(地区)来返回不同的Calendar子类对象。其实就是三种,如果是泰国地区就返回BuddhistCalendar(佛历),日本地区就返回JapaneseImperialCalendar(日本帝国历),其他国家和地区就返回GregorianCalendar(格里高利历,即公历)。
jdk8咋没有中国的农历呢,还做了日本帝国历,,,,,,
直接new他的子类还不行,这里没有public,还是用人家提供的getInstence就行了
调用的无参getInstance
没有走进下面的简单工厂,直接shanghaoi zh_CN 就返回了
代码示例
码云地址
微信
个人网站
http://www.51pro.top
公众号
这篇关于彻底搞懂设计模式DesignPattern-工厂模式-简单工厂-工厂方法-静态工厂-抽象工厂的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!