zoukankan      html  css  js  c++  java
  • 简单工厂模式

    简单工厂模式

    简单工厂模式又叫静态工厂方法模式,通过产品接口、具体产品类和工厂类实现。客户端(使用具体产品类的类)不需要知道产品类的详细信息,只需要知道产品类的对应参数,通过调用工厂类的静态方法来创建对象。

    解决的问题

    简单工厂模式、工厂方法模式和抽象工厂模式是工厂模式的三种实现,工厂模式将对象创建和对象使用分离开来,将软件解耦,使其满足单一职责原则。

    简单工厂模式类图

    以Fruit作为产品接口,Apple、Orange、Banana是具体产品类,Factory是工厂类,SimpleFactoryPattern是客户端。

    具体实现

    简单工厂模式的具体实现如下:

     1 public class SimpleFactoryPattern {
     2     public static void main(String[] args){
     3         //不需要知道类的详细信息,只需要向工厂方法传递一个与具体产品类对应的参数就可以得到对应的对象
     4         Fruit fruit1=Factory.createFactory("apple");
     5         fruit1.eat();
     6         Fruit fruit2=Factory.createFactory("banana");
     7         fruit2.eat();
     8         Fruit fruit3=Factory.createFactory("orange");
     9         fruit3.eat();
    10     }
    11 }
    12 
    13 interface Fruit{
    14     public abstract void eat();
    15 }
    16 
    17 class Apple implements Fruit{
    18     public void eat(){
    19         System.out.println("eat apple");
    20     }
    21 }
    22 class Orange implements Fruit{
    23     public void eat(){
    24         System.out.println("eat orange");
    25     }
    26 }
    27 class Banana implements Fruit{
    28     public void eat(){
    29         System.out.println("eat banana");
    30     }
    31 }
    32 class Factory{
    33     public static Fruit createFactory(String name){
    34         Fruit fruit=null;
    35         if(name.equals("apple")){
    36             fruit=new Apple();
    37         }
    38         else if(name.equals("orange")){
    39             fruit=new Orange();
    40         }
    41         else  if(name.equals("banana")){
    42             fruit=new Banana();
    43         }
    44         return fruit;
    45     }
    46 }
    View Code

    优点

    1)将对象创建和对象使用分离开来,减弱了代码的耦合性。

    2)提供了出new操作符之外的另外一种创建对象的形式,虽然静态工厂方法内部还是使用new操作符创建对象。

    3)客户端无需知道所创建具体产品类的类名,只需要知道此类对应的参数即可,对于一些复杂的类,通过简单工厂模式减少了使用者的记忆量。

    缺点

    1)静态工厂方法包含逻辑判断和创建对象两个功能,违反了单一职责原则。

    2)如果增加新的产品类,就需要在静态工厂方法中添加代码,违反了开闭原则(对扩展开发,对修改关闭)。

    通过反射改进简单工厂模式

    可以通过反射机制避免当增加新产品类,在静态工厂方法中添加代码的情况,使程序符合开闭原则。通过Class.forName(String className).newInstance()得到某个类的实例,这里只是调用类的默认构造函数(无参构造函数)创建对象,实现代码如下:

     1 /**
     2  * Created by hfz on 2016/10/8.
     3  */
     4 public class SimpleFactoryPattern {
     5     public static void main(String[] args){
     6         //不需要知道类的详细信息,只需要向工厂方法传递一个与具体产品类对应的参数就可以得到对应的对象,传递类名,通过反射机制创建对象
     7         Fruit fruit1=Factory.createFactory("Apple");
     8         fruit1.eat();
     9         Fruit fruit2=Factory.createFactory("Banana");
    10         fruit2.eat();
    11         Fruit fruit3=Factory.createFactory("Orange");
    12         fruit3.eat();
    13     }
    14 }
    15 
    16 interface Fruit{
    17     public abstract void eat();
    18 }
    19 
    20 class Apple implements Fruit{
    21     public void eat(){
    22         System.out.println("by reflection,eat apple");
    23     }
    24 }
    25 class Orange implements Fruit{
    26     public void eat(){
    27         System.out.println("by reflection,eat orange");
    28     }
    29 }
    30 class Banana implements Fruit{
    31     public void eat(){
    32         System.out.println("by reflection,eat banana");
    33     }
    34 }
    35 class Factory{
    36     public static Fruit createFactory (String name){
    37         Fruit fruit=null;
    38         try {
    39             fruit = (Fruit) Class.forName(name).newInstance();
    40         }
    41         catch (ClassNotFoundException ex){
    42             System.out.println();
    43         }
    44         catch (InstantiationException ex1){
    45             System.out.println();
    46         }
    47         catch (IllegalAccessException ex2){
    48             System.out.println();
    49         }
    50         return fruit;
    51     }
    52 }
    View Code

    缺点

    静态工厂方法负责创建所有具体产品类,一旦这个工厂方法出现问题,所有的客户端(不同客户端调用不同的具体产品类)都会受到牵连。

    参考:

    1)http://www.jasongj.com/design_pattern/simple_factory/

    2)http://www.hollischuang.com/archives/1401

    3)http://blog.csdn.net/fengyuzhengfan/article/details/38086743

    4)http://www.kancloud.cn/thinkphp/design-patterns/39766

    5)https://github.com/pzxwhc/MineKnowContainer/issues/73

  • 相关阅读:
    Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
    Spring Boot 如何给微信公众号返回消息
    Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
    Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
    Spring Boot 开发微信公众号后台
    Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
    Spring Boot2 系列教程(十六)定时任务的两种实现方式
    Spring Boot2 系列教程(十五)定义系统启动任务的两种方式
    Spring Boot2 系列教程(十四)CORS 解决跨域问题
    JavaScript二维数组
  • 原文地址:https://www.cnblogs.com/lz3018/p/5937929.html
Copyright © 2011-2022 走看看