彻底搞懂设计模式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

相关文章

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

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方