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

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

浅谈主机加固,六种有效的主机加固方法

在数字化时代,数据的价值不言而喻,但随之而来的安全威胁也日益严峻。从勒索病毒到内部泄露,企业的数据安全面临着前所未有的挑战。为了应对这些挑战,一种全新的主机加固解决方案应运而生。 MCK主机加固解决方案,采用先进的安全容器中间件技术,构建起一套内核级的纵深立体防护体系。这一体系突破了传统安全防护的局限,即使在管理员权限被恶意利用的情况下,也能确保服务器的安全稳定运行。 普适主机加固措施:

webm怎么转换成mp4?这几种方法超多人在用!

webm怎么转换成mp4?WebM作为一种新兴的视频编码格式,近年来逐渐进入大众视野,其背后承载着诸多优势,但同时也伴随着不容忽视的局限性,首要挑战在于其兼容性边界,尽管WebM已广泛适应于众多网站与软件平台,但在特定应用环境或老旧设备上,其兼容难题依旧凸显,为用户体验带来不便,再者,WebM格式的非普适性也体现在编辑流程上,由于它并非行业内的通用标准,编辑过程中可能会遭遇格式不兼容的障碍,导致操

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验