• Spring设计模式_工厂模式


    先说下工厂模式的特性

      1.对于调用者来说,影藏了复杂的逻辑处理过程,调用者只关心执行结果。

      2.工厂要对结果负责,保证生产出符合规范的产品。

    Git代码地址  https://github.com/wujiachengSH/WjcFactoryDemo 

    下述的3个栗子分别为简单工厂,工厂方法,抽象工厂

    先来个栗子看看什么是工厂把

    首先是简单工厂模式

    声明一个动物工厂

     1 package com.wjc.Factory;
     2 
     3 public interface Animal {
     4 
     5     String eat();
     6     
     7     String dirnk();
     8     
     9     String run();
    10     
    11 }

    来2个实现类

     1 package com.wjc.Factory;
     2 
     3 public class Elephant implements Animal {
     4 
     5     @Override
     6     public String eat() {
     7         // TODO Auto-generated method stub
     8         return "Elephant e";
     9     }
    10 
    11     @Override
    12     public String dirnk() {
    13         // TODO Auto-generated method stub
    14         return "Elephant d";
    15     }
    16 
    17     @Override
    18     public String run() {
    19         // TODO Auto-generated method stub
    20         return "Elephant r";
    21     }
    22 
    23 }
     1 package com.wjc.Factory;
     2 
     3 public class Leopard implements Animal {
     4 
     5     @Override
     6     public String eat() {
     7         // TODO Auto-generated method stub
     8         return "Leopard e";
     9     }
    10 
    11     @Override
    12     public String dirnk() {
    13         // TODO Auto-generated method stub
    14         return "Leopard d";
    15     }
    16 
    17     @Override
    18     public String run() {
    19         // TODO Auto-generated method stub
    20         return  "Leopard r";
    21     }
    22 
    23     
    24     
    25 }

    然后我们来定义一个工厂

     1 package com.wjc.Factory.simple;
     2 
     3 import com.wjc.Factory.Animal;
     4 import com.wjc.Factory.Elephant;
     5 import com.wjc.Factory.Leopard;
     6 
     7 public class SimpleFactory {
     8 
     9     public Animal getAnimal(String name) {
    10         if ("Elephant".equals(name)) {
    11             return new Elephant();
    12         }else if ("Leopard".equals(name)) {
    13             return new Leopard();
    14         }else {
    15             return null;
    16         }        
    17     }
    18     
    19 }

    测试一下这段代码

     1 package com.wjc.Factory.simple;
     2 
     3 import com.wjc.Factory.Animal;
     4 
     5 public class Test {
     6 
     7     public static void main(String[] args) {
     8         SimpleFactory simpleFactory = new SimpleFactory();
     9         Animal animal = simpleFactory.getAnimal("Leopard");
    10         System.out.println(animal.eat());
    11         
    12     }
    13 }

    可以看到工厂模式的意义在于,当需要使用对象的时候,不再通过New对象的方式拿取对象实例,而是通过工厂来获取对象

    通过工厂来声明Bean最大的好处就在于

    可以在Bean工厂中控制Bean是单例的?原型模式的?被代理的等等等。

    不过上述简单工厂能力过于强大,一个工厂竟然可以生产多种动物,显然不符合原理。我们来看正宗的工厂

     工厂模式代码

    1.声明一个工厂接口

    1 package com.wjc.Factory.func;
    2 
    3 import com.wjc.Factory.Animal;
    4 
    5 public interface Factory {
    6 
    7     Animal getAnimal();
    8     
    9 }

    2.分别实现工厂接口

     1 package com.wjc.Factory.func;
     2 
     3 import com.wjc.Factory.Animal;
     4 import com.wjc.Factory.Elephant;
     5 
     6 public class ElephantFactory implements Factory {
     7 
     8     @Override
     9     public Animal getAnimal() {
    10         // TODO Auto-generated method stub
    11         return new Elephant();
    12     }
    13 
    14 }
     1 package com.wjc.Factory.func;
     2 
     3 
     4 import com.wjc.Factory.Animal;
     5 import com.wjc.Factory.Leopard;
     6 
     7 public class LeopardFactory implements Factory {
     8 
     9     //来个单例工厂好了
    10     private static class  LeopardBean {
    11         private static final Leopard INSTANCE = new Leopard();
    12     } 
    13     
    14     
    15     @Override
    16     public Animal getAnimal() {
    17         // TODO Auto-generated method stub
    18         return LeopardBean.INSTANCE;
    19     }
    20 
    21 }

    3.通过工厂生成Bean

     1 package com.wjc.Factory.func;
     2 
     3 public class Test {
     4 
     5     public static void main(String[] args) {
     6         ElephantFactory elephantFactory = new ElephantFactory();
     7         System.out.println(elephantFactory.getAnimal().eat());
     8         
     9         LeopardFactory leopardFactory = new LeopardFactory();
    10         System.out.println(leopardFactory.getAnimal().eat());
    11         
    12     }
    13     
    14 }

    可以看到标准的Bean工厂,可以在工厂中声明和配置Bean对象的实现特性,甚至可以把上一篇代理模式使用进去。

    https://www.cnblogs.com/wujc/p/10554933.html

    但是上述代码还是存在着一个问题,工厂很多的情况下,获取实例其实并不方便,我们再进行进一步的封装,来靠近IOC

    我们来定义一个默认工厂,调用刚才封装的几个工厂

    先写一个抽象方法

     1 package com.wjc.Factory.abstra;
     2 
     3 import com.wjc.Factory.Animal;
     4 import com.wjc.Factory.func.ElephantFactory;
     5 import com.wjc.Factory.func.LeopardFactory;
     6 
     7 public abstract class AbstarctFactory {
     8 
     9     protected abstract Animal getAnimal();
    10     
    11     public Animal getAnimal(String name) {
    12         if ("Elephant".equals(name)) {
    13             return new ElephantFactory().getAnimal();
    14         }else if ("Leopard".equals(name)) {
    15             return new LeopardFactory().getAnimal();
    16         }else {
    17             return null;
    18         }        
    19     }
    20     
    21 }

    来个实现方法,其中有一个默认的产生对象

     1 package com.wjc.Factory.abstra;
     2 
     3 import com.wjc.Factory.Animal;
     4 import com.wjc.Factory.func.LeopardFactory;
     5 
     6 public class Factory extends AbstarctFactory {
     7 
     8     private LeopardFactory defaultFactory = new LeopardFactory();
     9     
    10     @Override
    11     protected Animal getAnimal() {
    12         // TODO Auto-generated method stub
    13         return defaultFactory.getAnimal();
    14     }
    15 
    16 }

    测试一下性能

     1 package com.wjc.Factory.abstra;
     2 
     3 public class Test {
     4 
     5     public static void main(String[] args) {
     6         
     7         Factory factory = new Factory();
     8         System.out.println(factory.getAnimal("Elephant").eat());
     9         
    10         
    11     }
    12     
    13 }

    上述改造后的代码就是抽象工厂模式了

    小结一下工厂模式,特性是封装了创建Bean的过程。

  • 相关阅读:
    Python MySQL(SQL语句的补充3)
    Python MySQL(pymysql的使用day1)
    Python MySQL(SQL语句的补充2)
    Python MySQL(对外键使用方法的补充)
    Python MySQL(学习SQL语句)
    Python MySQL数据库的基本使用
    关于http响应内容压缩的一点小积累。
    同一账号,后一用户登录,前一个用户则被踢掉
    对序列化的学习。
    MySQL 函数积累
  • 原文地址:https://www.cnblogs.com/wujc/p/10585387.html
Copyright © 2020-2023  润新知