设计模式之简单工厂模式

在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的,但是在一些情况下, new操作符直接生成对象会带来一些问题。举例来说, 许多类型对象的创造需要一系列的步骤: 你可能需要计算或取得对象的初始设置; 选择生成哪个子对象实例; 或在生成你需要的对象之前必须先生成一些辅助功能的对象。 在这些情况,新对象的建立就是一个 “过程”,不仅是一个操作,像一部大机器中的一个齿轮转动。

模式的问题:

如何能轻松方便地构造对象实例,而不必关心构造对象实例的细节和复杂过程呢?

解决方案:

建立一个工厂来创建对象

分类 

工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的。 

工厂模式可以分为三类: 

1)简单工厂模式(Simple Factory) 

2)工厂方法模式(Factory Method) 

3)抽象工厂模式(Abstract Factory) 

 这三种模式从上到下逐步抽象,并且更具一般性。 

        GOF在《设计模式》一书中将工厂模式分为两类:工厂方法模式(Factory Method)与抽象工厂模式(Abstract Factory)。

        将简单工厂模式(Simple Factory)看为工厂方法模式的一种特例,两者归为一类。 

什么是简单工厂模式

简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例,被创建的实例通常都具有共同的父类。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。

模式结构

简单工厂模式包含如下角色:

工厂角色(Factory)

是简单工厂模式的核心,它负责实现创建所有具体产品类的实例。工厂类可以被外界直接调用,创建所需的产品对象。

抽象产品角色(Product)

是所有具体产品角色的父类,它负责描述所有实例所共有的公共接口。

具体产品角色(Concrete Product)

继承自抽象产品角色,一般为多个,是简单工厂模式的创建目标。工厂类返回的都是该角色的某一具体产品。

UML图

有一个吃水果例子:要便于水果种类的扩展,要便于维护
1) 水果的种类很多(比如Apple、Banana等)
 

使用传统的方式来完成

在没有了解简单工厂之前,我们是直接new 对象创建实例的,假如我们现在有一个Apple类和一个Banana类

package com.dongguo.simplefactory.fruit1;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:10
 * @description:
 */
public  class Apple {
    public  void eat(){
        System.out.println("吃苹果");
    }
}
package com.dongguo.simplefactory.fruit1;
/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:11
 * @description:
 */
public  class Banana {
    public  void eat(){
        System.out.println("吃香蕉");
    }
}
package com.dongguo.simplefactory.fruit1;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:12
 * @description:
 */
public class SimpleFactory {
    public static void main(String[] args) {
        Apple apple = new Apple();
        Banana banana = new Banana();
        apple.eat();
        banana.eat();
    }
}
运行结果:
吃苹果
吃香蕉

优化一

显然我们可以抽象出一个接口类Fruit

package com.dongguo.simplefactory.fruit2;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:15
 * @description:
 */
public interface Fruit {
    public void eat();
}

这样Apple类和Banana类可以实现Fruit类

package com.dongguo.simplefactory.fruit2;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:15
 * @description:
 */
public  class Apple implements Fruit{
    public  void eat(){
        System.out.println("吃苹果");
    }
}
package com.dongguo.simplefactory.fruit2;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:16
 * @description:
 */
public class Banana implements Fruit {
    public void eat() {
        System.out.println("吃香蕉");
    }
}

 创建实例

package com.dongguo.simplefactory.fruit2;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:16
 * @description:
 */
public class SimpleFactory {
    public static void main(String[] args) {
        //用到了多态
        Fruit apple = new Apple();
        Fruit banana = new Banana();
        apple.eat();
        banana.eat();
    }
}
运行结果:
吃苹果
吃香蕉

优化二

使用简单工厂模式创建实例

使用简单工厂模式创建一个类来负责创建Apple和Banana类的实例

package com.dongguo.simplefactory.fruit3;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:15
 * @description:
 */
public interface Fruit {
    public void eat();
}
package com.dongguo.simplefactory.fruit3;


/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:23
 * @description:
 */
public  class Apple implements Fruit {
    public  void eat(){
        System.out.println("吃苹果");
    }
}

package com.dongguo.simplefactory.fruit3;


/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:16
 * @description:
 */
public class Banana implements Fruit {
    public void eat() {
        System.out.println("吃香蕉");
    }
}
package com.dongguo.simplefactory.fruit3;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:22
 * @description:
 */
public class FruitFactory {
    public Fruit eatApple(){
        return new Apple();
    }
    public Fruit eatBanana(){
        return new Banana();
    }
}

这样就可以通过创建FruitFactory去实例化Apple和Banana类

package com.dongguo.simplefactory.fruit3;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:24
 * @description:
 */
public class SimpleFactory {
    public static void main(String[] args) {
        Fruit apple = new FruitFactory().eatApple();
        Fruit banana = new FruitFactory().eatBanana();
        apple.eat();
        banana.eat();
    }
}
运行结果:
吃苹果
吃香蕉

优化三:

将FruitFactory工厂类中的方法改为静态的,这样可以直接调用它的方法,简单工厂模式也叫静态工厂模式

package com.dongguo.simplefactory.fruit4;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:27
 * @description:
 */
public class FruitFactory {
    public static Fruit eatApple(){
        return new Apple();
    }
    public static Fruit eatBanana(){
        return new Banana();
    }
}
package com.dongguo.simplefactory.fruit4;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:28
 * @description:
 */
public class SimpleFactory {
    public static void main(String[] args) {
        Fruit apple =  FruitFactory.eatApple();
        Fruit banana =  FruitFactory.eatBanana();
        apple.eat();
        banana.eat();
    }
}
运行结果:
吃苹果
吃香蕉

 这样就一步一步的完成了一个简单工厂的实现,

扩展:

但是由于举的例子比较简单,在工厂类中的两个方法功能类似,我们可以改造成一个吃水果的方法

 通过给出的苹果名称去获得对应的水果实例

package com.dongguo.simplefactory.fruit5;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:31
 * @description:
 */
public class FruitFactory {
    public static Fruit getFruit(String type){
        if (type.equalsIgnoreCase("apple")){
            return new Apple();
        }else if (type.equalsIgnoreCase("banana")){
            return new Banana();
        }else {
            System.out.println("找不到该水果");
            return null;
        }
    }
}

 创建实例

package com.dongguo.simplefactory.fruit5;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:32
 * @description:
 */
public class SimpleFactory {
    public static void main(String[] args) {
        Fruit apple = FruitFactory.getFruit("apple");
        Fruit banana = FruitFactory.getFruit("banana");
        apple.eat();
        banana.eat();
    }
}
运行结果:
吃苹果
吃香蕉

这种方式呢不易扩展,如果新加一种水果就要修改工厂类的代码,

那么也可以使用反射的方法去获取实例,这只是 创建实例的一种方式,事实上用户肯定不会通过反射的方法获得对象实例的。

package com.dongguo.simplefactory.fruit6;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:34
 * @description:
 */
public class FruitFactory {
    public static Fruit getFruit(String type) {
        try {

            Class fruit =  Class.forName(type);
            return (Fruit) fruit.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
package com.dongguo.simplefactory.fruit6;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-10:35
 * @description:
 */
public class SimpleFactory {
    public static void main(String[] args) {
        //1 Class.forName()注意:此处填完整类名
        Fruit apple = FruitFactory.getFruit("com.dongguo.simplefactory.fruit6.Apple");
        Fruit banana = FruitFactory.getFruit("com.dongguo.simplefactory.fruit6.Banana");
        apple.eat();
        banana.eat();
        //另外两种反射创建对象实例的方法就不介绍了
        //2 Class.class
        //3 class.getClass
    }
}
运行结果:
吃苹果
吃香蕉

设计模式都是一步一步推导验证而来的,从推导的过程中我们更能体会到设计模式的优缺点。

优点 

再简单工厂模式中,工厂类是整个模式的关键所在,它包含必要的判断逻辑,能够根据给定的信息去创建具体类的实例,用户在使用时可以根据工厂类去创建所需的实例,而无需了解该对象实例是如何创建的。

缺点 

当然由于工厂类集中了所有类实例的创建逻辑,当系统中的具体产品类不断增多时,工厂类也需要做相应的修改,扩展性不是很好

模式适用环境
在以下情况下可以使用简单工厂模式:
工厂类负责创建的对象比较少:由于创建的对象较少,不会造成工厂方法中的业务逻辑太过复杂。
客户端只知道传入工厂类的参数,对于如何创建对象不关心:客户端既不需要关心创建细节,甚至连类名都不需要记住,只需要知道类型所对应的参数。

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

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

部分源码

getInstance 是Calendar 静态方法

 public static Calendar getInstance()
    {
        return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
    }

   
    public static Calendar getInstance(TimeZone zone)
    {
        return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));
    }

    
    public static Calendar getInstance(Locale aLocale)
    {
        return createCalendar(TimeZone.getDefault(), aLocale);
    }

    
    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;
                }
            }
        }
        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;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失业找工作中

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值