工厂模式

什么是设计模式?

  设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的代码设计经验的总结


工厂模式:

  实例化对象,用工厂方法代替new操作

  工厂模式包括工厂方法模式和抽象工厂模式

  抽象工厂模式是工厂方法模式的扩展

工厂模式的意图:

  定义一个接口来创建对象,但是让子类决定哪些类需要被实例化

  工厂方法把实例化的工作推迟到子类中去实现

什么情况适合工厂模式?

  有一组类似的对象需要创建

  在编码时不能预见需要创建哪种类的实例

  系统需要考虑扩展性,不依赖于产品实例如何被创建、组合和表达的细节

工厂模式的动机:

  在软件系统中经常面临着“对象”的创建工作,由于需求的变化,这个对象可能随之发生变化,但却拥有比较稳定的接口。为此,我们需要提供一种封装机制来隔离出这种易变对象的变化,从而保持系统中其他依赖该对象的对象不需要随着需求的变化而变化(Spring IOC)

  

工厂模式的代码实现步骤:

  1、创建接口

  2、子类实现接口

  3、工厂类根据参数返回不同的子类对象

  4、测试类

具体实现:

  接口

 1 package org.zln.design_pattern.factory;
 2 
 3 /**
 4  * Created by coolkid on 2015/7/18 0018.
 5  */
 6 public interface HairInterface {
 7     /**
 8      * 实现了绘制发型
 9      */
10     public void draw();
11 }

  子类

 1 package org.zln.design_pattern.factory;
 2 
 3 /**
 4  * Created by coolkid on 2015/7/18 0018.
 5  */
 6 public class LeftHair implements HairInterface {
 7     @Override
 8     public void draw() {
 9         System.out.println("绘制左偏分发型");
10     }
11 }
 1 package org.zln.design_pattern.factory;
 2 
 3 /**
 4  * Created by coolkid on 2015/7/18 0018.
 5  */
 6 public class RightHair implements HairInterface {
 7     @Override
 8     public void draw() {
 9         System.out.println("绘制右偏分发型");
10     }
11 }

  工厂类

 1 package org.zln.design_pattern.factory;
 2 
 3 /**
 4  * 发型工厂
 5  * Created by coolkid on 2015/7/18 0018.
 6  */
 7 public class HairFactory {
 8     public HairInterface getHair(String key){
 9         if (null == key){
10             return null;
11         }
12         switch (key){
13             case "left":return new LeftHair();
14             case "right":return new RightHair();
15             default:return null;
16         }
17     }
18 }

  测试类

 1 package org.zln.design_pattern.factory;
 2 
 3 /**
 4  * 模拟客户端
 5  * Created by coolkid on 2015/7/18 0018.
 6  */
 7 public class SunnyTest {
 8     public static void main(String[] args) {
 9         HairInterface hairInterface = new HairFactory().getHair("right");
10         hairInterface.draw();
11     }
12 }

在测试类中,只要指定不同的参数,就能够获取到不同的子类。和Spring中的IoC如此相似

这里的工厂类实现有一个缺陷,就是一旦增加一个子类实现,就需要修改工厂方法。为了避免,可以使用反射机制

1     public HairInterface getHairByClass(String className){
2         try {
3             HairInterface hairInterface = (HairInterface) Class.forName(className).newInstance();
4             return hairInterface;
5         } catch (Exception e) {
6             e.printStackTrace();
7         }
8         return null;
9     }

这里还有个缺点,就是客户端在调用的时候,传递的是类的权限定名,这太长了。为了简化,可以维护一个映射关系

 1 package org.zln.design_pattern.factory;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.Enumeration;
 6 import java.util.HashMap;
 7 import java.util.Map;
 8 import java.util.Properties;
 9 
10 /**
11  * Created by coolkid on 2015/7/18 0018.
12  */
13 public class PropertiesReader {
14     public Map<String,String> getProperties(){
15         Properties properties = new Properties();
16         Map<String,String> map = new HashMap<>();
17         try {
18             InputStream inputStream = getClass().getResourceAsStream("type.properties");
19             properties.load(inputStream);
20             Enumeration enumeration = properties.propertyNames();
21             while (enumeration.hasMoreElements()){
22                 String key = (String) enumeration.nextElement();
23                 String value = properties.getProperty(key);
24                 map.put(key,value);
25             }
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29         return map;
30     }
31 
32 }
left=org.zln.design_pattern.factory.LeftHair
right=org.zln.design_pattern.factory.RightHair

小结:Spring已经实现了工厂模式,所以在实际项目中,工厂模式见到的不多

所谓的抽象工厂模式,是因为子类会属于不同的体系,实现不同的接口。

这个时候在工厂方法中,就需要根据不同的体系,编写不同的对象创建方法

原文地址:https://www.cnblogs.com/sherrykid/p/4657190.html