我的GOF23之工厂模式

写在最前面

作为一个电气工程师,研究等离子体方向,最近在自学设计模式,此为整理博客。设计模式可以分为三大类,分别是创建型设计模式、行为型设计模式以及结构型设计模式。

工厂模式是创建型的设计模式的一种。
先看工厂方法模式,并不那么能“顾名思义”,诚然,我可以揣测是用工厂来创造对象,但是这难免会跟IoC等概念混淆,并且如果按较为官方的定义:Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses. 工厂方法模式是定义接口,而让子类来决定实例化哪个类;工厂方法让类的实例化延迟到其子类。那按这个指导思想,工厂方法好像没有那么多条条框框了,试实现之。

心法:
万事万物皆对象,万事万物皆产品!

工厂方法模式

不再new了!让工厂去new吧!使用工厂生产同一类产品!
图片描述

  • 定义产品接口

    public interface Product{
        void operation();
    }
  • 产品实现类

    public class ProductA implements Product{
        private String name;
        private int size;
        
        @Override
        void operation(){
            System.out.println("这是产品A");
        }
    }
public class ProductB implements Product{
    private String name;
    private int size;
    
    @Override
    void operation(){
        System.out.println("这是产品B");
    }
}
  • 抽象工厂方法类

    public abstract class AbstractFactory{
        public abstract <T extends Product> T createProduct(Class<T> c);
    }
    
  • 具体工厂实现类

    public class ConcreteFactory extends AbstractFactory{
        @Override
        public <T extends Product> T createProduct(Class c){
            Product product = null;
            try{
                product = (T) c.newInstance();
            } catch (Exception e){
                System.out.println("生产产品失败");
            }
            return (T)product;
        }
    }
  • 调用方Client类

    public class Client{
        public static void main(String[] args){
            AbstractFactory factory = new ConcreteFactory();
            Product productA = factory.createProduct(ProductA.class);
            productA.operation();
            
            Product productB = factory.createProduct(ProductB.class);
            productB.operation();
        }
    }

工厂方法的变形:简单工厂或静态工厂模式

  • 去掉抽象类AbstractFactory,在工厂类中提供一个静态方法返回产品

    public class SimpleFactory{
        public static <T extends Product> T createProduct(Class c){
            Product product = null;
            try {
                product = (T) c.newInstance();
            } catch (Exception e){
                System.out.println("生产产品失败");
                e.printStackTrace();
            }
            return (T) product;
        }
    }
  • 修改Client类中的调用方式

    public class Client{
        public static void main(String[] args){
            Product productA = SimpleFactory.createProduct(ProductA.class);
            productA.operation();
            
            Product productB = SimpleFactory.createProduct(ProductB.class);
            productB.operation();
        }
    }

升级为多个工厂类

  • 针对每一类产品提供一个工厂类来生产产品,uml图为:
    图片描述

  • 抽象工厂方法类

    public abstract class AbstractFactory{
        public abstract Product createProduct();
    }
  • ProductAFactory

    public class ProductAFactory extends AbstractFactory{
        @Override
        public Product createProduct(){
            return new ProductB();
        }
    }
  • ProductBFactory

    public class ProductBFactory extends AbstractFactory{
        @Override
        public Product createProduct(){
            return new ProductB();
        }
    }
  • 调用方Client

    public class Client{
        public static void main(String[] args){
            AbstractFactory factoryA = new ProductAFactory();
            AbstractFactory factoryB = new ProductBFactory();
            Product productA = factoryA.createProduct();
            Product productB = factoryB.createProduct();
            productA.operation();
            productB.operation();
        }
    }

抽象工厂

定义:Provide an interface for creating families of creating families of related or dependent objects without specifying their concrete classes.
uml图为图片描述
代码略

工厂模式与IoC的关系

http://stackoverflow.com/ques...

一些高分答案:

When using a factory your code is still actually responsible for creating objects. By DI you outsource that responsibility to another class or a framework, which is separate from your code.工厂模式中,代码中还要创建对象,但是DI中,创建对象的责任转交给了其他类或者框架,与代码已经解耦了。

I would suggest to keep the concepts plain and simple. Dependency Injection is more of a architectural pattern for loosely coupling software components. Factory pattern is just one way to separate the responsibility of creating objects of other classes to another entity. Factory pattern can be called as a tool to implement DI. Dependency injection can be implemented in many ways like DI using constructors, using mapping xml files etc. 认为DI更大程度上是一种将模块解耦的架构模式,而工厂模式可以看作一类实现了DI的工具,而DI可以通过多种方式实现。


菟潞寺沙弥
303 声望55 粉丝