彻底搞懂设计模式DesignPattern-工厂模式-简单工厂-工厂方法-静态工厂-抽象工厂

本文主要是介绍彻底搞懂设计模式DesignPattern-工厂模式-简单工厂-工厂方法-静态工厂-抽象工厂,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简单工厂

普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。
最简单的总结就是一个工厂全负责,里面有判断,用于生产实例

  1. 简单工厂模式是属于创建型模式,是工厂模式的一种。 简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式
  2. 简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)
  3. 在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式.
首先,创建二者的共同接口:
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!

抽象工厂

创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码

  1. 抽象工厂模式:定义了一个interface用于创建相关或有依赖关系的对象簇,而无需指明具体的类
  2. 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合。
  3. 从设计层面看,抽象工厂模式就是对简单工厂模式的改进(或者称为进一步的抽象)。
  4. 将工厂抽象成两层, 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-工厂模式-简单工厂-工厂方法-静态工厂-抽象工厂的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/361411

相关文章

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S