工厂模式概述
什么是设计模式
设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。
应用设计模式有什么好处
设计模式是优秀的使用案例,使用设计模式可提高代码的重用性,让代码更容易被他人理解。保证代码可靠性。
什么是工厂模式
实例化对象,用工厂方法代替new操作。工厂模式包括工厂方法模式和抽象工厂模式
抽象工厂模式是工厂方法模式的扩展
工厂模式的意图
定义一个接口来创建对象,但是让子类来决定哪些类需要被实例化。工厂方法把实例化的工作推迟到子类中去实现。
什么情况下合适工厂模式
有一组类似的对象需要创建。在编码时不能预见需要创建哪种类的实例。
系统需要考虑扩展性,不应依赖于产品类实例如何被创建、组合和表达细节。
项目中的现状
在软件系统中经常面临着 “对象” 的创建工作,由于需求的变化,这个对象可能随之也会发生变化,但它却拥有比较稳定的接口。
为此,我们需要提供一种封装机制来隔离出这个易变对象的变化,从而保持系统中其他依赖该对象的对象不随着需求变化而变化。
基于项目现状将代码进行如下设计
尽量低耦合,一个对象的依赖对象的变化与本身无关。具体产品与客户端剥离,责任分割。
工厂方法模式类图
抽象工厂模式类图
工厂模式应用
工厂方法模式
接口:
/**
* 发型接口
*/
public interface HairInterface {
public void draw();
}
实现接口的类对象:
/**
* 制作左偏分发型类(对象)
*/
public class LeftHair implements HairInterface {
/**
* 实现左偏分发型
*/
public void draw() {
System.out.println("<----------左偏分发型---------->");
}
}
/**
* 制作右偏分发型类对象
*/
public class RightHair implements HairInterface {
/**
* 实现右偏分发型
*/
public void draw() {
System.out.println("<----------右偏分发型---------->");
}
}
客户端:提出需求
/**
* 客户端
*/
public class Client {
public static void main(String[] args) {
//客户说我要左偏分发型
HairInterface left = new LeftHair();
left.draw();
//客户说我要右偏分发型
HairInterface riht = new RightHair();
riht.draw();
}
}
缺陷:代码不利于维护和管理
这时,就需要建立一个发型工厂对象:
/**
* 发型工厂
*/
public class HairFactory {
public HairInterface getHair(String key){
if ("left".equals(key)) {
return new LeftHair();
}
else if ("right".equals(key)) {
return new RightHair();
}
return null;
}
}
改进后的客户端:
/**
* 客户端
*/
public class Client {
public static void main(String[] args) {
//客户说我要左偏分发型
// HairInterface left = new LeftHair();
// left.draw();
//客户说我要右偏分发型
// HairInterface riht = new RightHair();
// riht.draw();
HairFactory factory = new HairFactory();
HairInterface left = factory.getHair("left");
left.draw();
HairInterface right = factory.getHair("right");
right.draw();
}
}
优点:客户端与实现分离,缺点:不够智能
/**
* 发型工厂
*/
public class HairFactory {
public HairInterface getHair(String key){
if ("left".equals(key)) {
return new LeftHair();
}
else if ("right".equals(key)) {
return new RightHair();
}
return null;
}
/**
* 根据类对象的名称来生产对象
* @param className
* 类名称
* @return
*/
public HairInterface getHairByClass(String className) {
try {
HairInterface hair = (HairInterface) Class.forName(className).newInstance();
return hair;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
改进后的客户端:
/**
* 客户端
*/
public class Client {
public static void main(String[] args) {
//客户说我要左偏分发型
// HairInterface left = new LeftHair();
// left.draw();
//客户说我要右偏分发型
// HairInterface riht = new RightHair();
// riht.draw();
HairFactory factory = new HairFactory();
// HairInterface left = factory.getHair("left");
// left.draw();
//
// HairInterface right = factory.getHair("right");
// right.draw();
//传全路径
HairInterface left = factory.getHairByClass("com.lijy.factory.LeftHair");
left.draw();
}
}
当全路径过长,可以配置在properties文件里:
left=com.lijy.factory.LeftHair
right=com.lijy.factory.RightHair
然后创建读取properties文件的类将key-value放在map中:
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* Properties文件读取类
*/
public class PropertiesReader {
public Map<String, String> getProperties() {
Properties properties = new Properties();
Map<String, String> map = new HashMap<String, String>();
try {
InputStream inputStream = getClass().getResourceAsStream("type.properties");
properties.load(inputStream);
Enumeration en = properties.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = properties.getProperty(key);
map.put(key, value);
}
}
catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
在发型工厂类新增一个方法getHairByClassKey:
public HairInterface getHairByClassKey(String key) {
try {
Map<String, String> map = new PropertiesReader().getProperties();
HairInterface hair = (HairInterface) Class.forName(map.get(key)).newInstance();
return hair;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
客户端:
/**
* 客户端
*/
public class Client {
public static void main(String[] args) {
HairInterface hair = factory.getHairByClassKey("right");
hair.draw();
}
}
抽象工厂模式应用
Factory:
/** * 人物的实现接口 */ public interface PersonFactory { public Boy getBoy(); public Girl getGirl(); }
ConcreteFactory1:/** * 圣诞系列加工厂 */ public class MCFactory implements PersonFactory{ public Boy getBoy() { return new MCBoy(); }
ConcreteFactory2:public Girl getGirl() { return new MCGirl(); } }
/** * 新年系列工厂 */ public class HNFactory implements PersonFactory { public Boy getBoy() { return new HNBoy(); } public Girl getGirl() { return new HNGirl(); } }
AbstractProductA:
AbstractProductB:/** * 男孩接口 */ public interface Boy { public void drawMan(); }
ConcreteProductA1:/** * 女孩接口 */ public interface Girl { public void drawWomen(); }
ConcreteProductA2:/** * 圣诞系类男孩 */ public class MCBoy implements Boy { public void drawMan() { System.out.println("<--------圣诞系类男孩------->"); } }
ConcreteProductB1:/** * 元旦系类男孩 */ public class HNBoy implements Boy { public void drawMan() { System.out.println("<--------元旦系类男孩------->"); } }
ConcreteProductB2:/** * 圣诞系类女孩 */ public class MCGirl implements Girl { public void drawWomen() { System.out.println("<--------圣诞系类女孩------->"); } }
/** * 元旦系类女孩 */ public class HNGirl implements Girl { public void drawWomen() { System.out.println("<--------元旦系类女孩------->"); } }
Client:
/** * 客户端 */ public class Client { public static void main(String[] args) { PersonFactory factory2 = new MCFactory(); Girl girl = factory2.getGirl(); girl.drawWomen(); } }
常见应用:
JDBC
是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
![]()
spring beanfactory
BeanFactory , 作为Spring基础的IoC容器,是Spring的一个Bean工厂,如果单从工厂模式的角度思考,它就是用来"生产Bean",然后提供给客户端。
Bean的实例化过程如下:
- 调用Bean的默认构造方法,或指定的构造方法,生成bean实例(暂称instance1)
- 如果Bean的配置文件中注入了Bean属性值,则在instance1基础上进行属性注入形成instance2,这种注入是覆盖性的。
- 如果Bean实现了InitializingBean接口,则调用afterPropertiesSet()方法,来改变或操作instance2,得到instance3
- 如果Bean的配置文件中指定了init-method="init"属性,则会调用指定的初始化方法,则在instance3的基础上调用初始化方法init(),将对象最终初始化为instance4;当然,这个初始化的名字是任意的。
工厂方法模式和抽象工厂模式对比
- 工厂模式是一种极端情况的抽象工厂模式,而抽象工厂模式可以看成是工厂模式的推广
- 工厂模式用来创建一个产品的等级结构,而抽象工厂是用来创建多个产品的等级结构
- 工厂模式只有一个抽象产品类,而抽象工厂模式有多个抽象产品类
工厂模式的实现帮助我们
- 系统可以在不修改具体工厂角色的情况下引进新的产品
- 客户端不必关心对象如何创建,明确了指责
- 更好的理解面向对象的原则 面向接口编程,而不要面向实现编程
工厂模式适用于哪些场景
- 一个系统应当不依赖于产品类实例被创立,组成,和表示的细节。这对于所有形态的工厂模式都是重要的
- 这个系统的产品有至少一个的产品族
- 同属于同一产品族的产品是设计成在一起使用的。这一约束必须得在系统的设计中体现出来
- 不同的产品以一系列的接口的面貌出现,从而使系统不依赖与接口实现的细节
参考引用:
博客专栏:设计模式读书笔记
慕课网:模式的秘密--工厂模式