工厂模式 Idea

1,672 次浏览次阅读
没有评论

工厂模式 Factory

工厂模式可以根据传入的参数不同返回不同的实例

3.1 简单工厂模式

产品接口类

package com.learn.Factory.SimpleFactory;

public interface Product {}

具体的产品 A 类

package com.learn.Factory.SimpleFactory;

public class ProductA implements Product{}

具体的产品 B 类

package com.learn.Factory.SimpleFactory;

public class ProductB implements Product{
}

生产产品的简单工厂类 / 静态工厂类

package com.learn.Factory.SimpleFactory;

public class SimpleFactory {public static Product createProduct(String productType) {
        Product product = null;

        if ("A".equals(productType)) {ProductA productA = new ProductA();
            System.out.println(" 工厂创建了对象 productA: " + productA);
        } else if ("B".equals(productType)) {ProductB productB = new ProductB();
            System.out.println(" 工厂创建了对象 productB" + productB);
        } else {throw new IllegalArgumentException();
        }
        return product;
    }

}

客户端调用工厂类的静态方法创建产品

package com.learn.Factory.SimpleFactory;

public class Client {public static void main(String[] args) {SimpleFactory.createProduct("A");
        SimpleFactory.createProduct("B");
        SimpleFactory.createProduct("C");
    }
}

缺点:简单工厂负责了所有产品的创建逻辑,当我们需要引进一个新产品 时,就必须修改工厂类的厂品创建逻辑,如果产品类型较多时可能会造成工厂类的产品创建逻辑过于复杂,不利于维护性和扩展性。

适用场景: 一般适用于创建的对象比较少,不会造成工厂方法中的业务逻辑太过于复杂

3.2 工厂方法模式

工厂方法 (Factory Method) 模式,又称多态工厂 (Polymorphic Factory) 模式或虚拟构造器 (Virtual Constructor) 模式。工厂方法模式通过定义工厂抽象父类 (或接口) 负责定义创建对象的公共接口,而工厂子类 (或实现类) 则负责生成具体的对象

产品接口

package com.learn.Factory.FactoryMethod;

public interface Product {}

具体的产品 A

package com.learn.Factory.FactoryMethod;

public class ProductA implements Product{}

具体的产品 B

package com.learn.Factory.FactoryMethod;

public class ProductB implements Product{}

产品工厂接口 (抽象类也可以,但接口扩展性更好)

package com.learn.Factory.FactoryMethod;

public interface ProductFactoryInterface {Product create();
}

具体的产品 A 工厂与具体的产品 A 对应,调用产品工厂接口的 create 方法,创建具体的产品 A

package com.learn.Factory.FactoryMethod;

public class ProductFactoryA implements ProductFactoryInterface{

    @Override
    public Product create() {return new ProductA();
    }
}

具体的产品 B 工厂与具体的产品 B 对应,调用产品工厂接口的 create 方法,创建具体的产品 B

package com.learn.Factory.FactoryMethod;

public class ProductBFactory implements ProductFactoryInterface{
    @Override
    public Product create() {return new ProductB();
    }
}

客户端 通过 new 具体的产品工厂类调用.create 方法创建具体的产品对象

package com.learn.Factory.FactoryMethod;

public class Client {public static void main(String[] args) {Product productA = new ProductFactoryA().create();
        Product productB = new ProductBFactory().create();

        System.out.println(" 工厂创建了产品 A:" + productA);
        System.out.println(" 工厂创建了产品 b:" + productB);
    }
}

新增产品 C 时,只需要新增产品自身类和对用的工厂类即可

适用场景

  • 在工厂方法模式中,客户端不需要知道具体产品类的类名,只需要知道所对应的工厂即可,具体的产品对象由具体工厂类创建
  • 在工厂方法模式中,抽象工厂类只需提供一个创建产品的接口,而由其子类来确定具体要创建的对象。利用面向对象的多态性和里氏替换原则,程序运行时子类对象将覆盖父类对象,从而使得系统更容易扩展
  • 将创建对象的任务委托给多个工厂子类中的某一个,客户端在使用时可以无需关心是哪个工厂子类创建产品子类,需要时再动态指定,可将具体工厂类的类名存储在配置文件或数据库中

缺点:新增产品时除了新增自身的产品类之外,还要增加对应的具体的工厂类。

正文完
 0
评论(没有评论)