• 工厂模式总结


    1.工厂模式在 JDK-Calendar 应用的源码分析

    1) JDK 中的 Calendar 类中, 就使用了简单工厂模式

    package demo.factory.JDKSRC;
    
    import java.util.Calendar;
    
    public class CalenderDemo {
        public static void main(String[] args) {
               Calendar calendar = Calendar.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 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);
        }
    
        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;
                    }
                }
            }
    

      

    2.工厂模式小结

    1) 工厂模式的意义
    将实例化对象的代码提取出来, 放到一个类中统一管理和维护, 达到和主项目的依赖关系的解耦。 从而提高项
    目的扩展和维护性。
    2) 三种工厂模式 (简单工厂模式、 工厂方法模式、 抽象工厂模式)
    3) 设计模式的依赖抽象原则
    创建对象实例时, 不要直接 new , 而是把这个 new 类的动作放在一个工厂的方法中, 并返回。 有的书上说,
    变量不要直接持有具体类的引用。
    不要让类继承具体类, 而是继承抽象类或者是实现 interface(接口)
    不要覆盖基类中已经实现的方法


  • 相关阅读:
    工厂模式简介
    设计模式
    idea的安装与配置及基本用法
    软件架构设计的七大原则
    C#设计模式开启闯关之路
    基础知识详解系列目录
    .Net Core2.2 使用 AutoMapper进行实体转换
    通俗易懂设计模式解析——解释器模式
    通俗易懂设计模式解析——备忘录模式
    通俗易懂设计模式解析——访问者模式
  • 原文地址:https://www.cnblogs.com/aaaazzzz/p/14198225.html
Copyright © 2020-2023  润新知