| package com.atguigu.jdk; |
| import java.util.Calendar; |
| |
| public class Factory { |
| |
| public static void main(String[] args) { |
| |
| Calendar cal = Calendar.getInstance(); |
| |
| System.out.println("年:" + cal.get(Calendar.YEAR)); |
| System.out.println("月:" + (cal.get(Calendar.MONTH) + 1)); |
| System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH)); |
| System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY)); |
| System.out.println("分:" + cal.get(Calendar.MINUTE)); |
| System.out.println("秒:" + cal.get(Calendar.SECOND)); |
| } |
| |
| } |
| public static Calendar getInstance() |
| { |
| Locale aLocale = Locale.getDefault(Locale.Category.FORMAT); |
| return createCalendar(defaultTimeZone(aLocale), aLocale); |
| } |
| 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) { |
| |
| } |
| } |
| |
| 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 (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; |
| } |
| 将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性 |
| 创建对象实例时,不要直接 new 类, 而是把这个new 类的动作放在一个工厂的方法中,并返回。有的书上说,变量不要直接持有具体类的引用。 |
| 不要让类继承具体类,而是继承抽象类或者是实现interface(接口) |
| 不要覆盖基类中已经实现的方法 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类