一、概述
工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的。
实例化对象,用工厂方法代替new操作
工厂模式包括工厂方法模式和抽象工厂模式
二、工厂方法模式
这里我们以发型为例,运用工厂方法模式我们需要创建一个发型接口、发型工厂,以及左偏分发型和右偏分发型具体实现类
1.发型接口HairInterface.java
package com.czhappy.designpattern.factory;
/**
*
* @ClassName: HairInterface
* @Description: 发型接口
* @author chenzheng
* @date 2017-1-13 下午02:46:00
*/
public interface HairInterface {
public void draw();
}
2.发型工厂HairFactory.java
package com.czhappy.designpattern.factory;
import java.util.Map;
/**
*
* @ClassName: HairFactory
* @Description: 发型工厂
* @author chenzheng
* @date 2017-1-13 下午02:51:38
*/
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 HairInterface getHairByClass(String className){
try {
HairInterface hair = (HairInterface) Class.forName(className).newInstance();
return hair;
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public HairInterface getHairByClassKey(String key){
try {
Map map = new PropertiesReader().getProperties();
HairInterface hair = (HairInterface) Class.forName(map.get(key)).newInstance();
return hair;
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
3.左偏分发型LeftHair.java
package com.czhappy.designpattern.factory;
public class LeftHair implements HairInterface{
@Override
public void draw() {
System.out.println("实现左偏分发型");
}
}
4.右偏分发型RightHair.java
package com.czhappy.designpattern.factory;
public class RightHair implements HairInterface{
@Override
public void draw() {
System.out.println("实现右偏分发型");
}
}
5.测试类Test.java
package com.czhappy.designpattern.factory;
public class Test {
public static void main(String[] args) {
HairInterface left1 = new LeftHair();
left1.draw();
HairFactory factory2 = new HairFactory();
HairInterface left2 = factory2.getHair("left");
if(left2!=null){
left2.draw();
}
HairFactory factory3 = new HairFactory();
HairInterface left3 = factory3.getHairByClass("com.czhappy.designpattern.factory.LeftHair");
if(left3!=null){
left3.draw();
}
HairFactory factory4 = new HairFactory();
HairInterface left4 = factory4.getHairByClassKey("right");
if(left4!=null){
left4.draw();
}
}
}
这样使用工厂模式之后后面我们如果再有新的发型需求,直接添加相应的实现类即可,不需要再修改原有的代码。
三、抽象工厂模式
这里我们以生产男孩和女孩节日套装为例,我们需要定义男孩和女孩接口,并声明装扮方法,圣诞节就可以装扮圣诞的服装、新年就可以装扮新年的服装,然后我们就需要定义一个男孩女孩的工厂接口,来提供是要男孩还是要女孩,再定义新年和圣诞的节日工厂来获取是要哪个节日的男孩或女孩。
1.男孩接口Boy.java
package com.czhappy.designpattern.factory;
public interface Boy {
public void drawMan();
}
2.女孩接口Girl.java
package com.czhappy.designpattern.factory;
public interface Girl {
public void drawWoman();
}
3.圣诞男孩MCBoy.java
package com.czhappy.designpattern.factory;
public class MCBoy implements Boy {
@Override
public void drawMan() {
System.out.println("圣诞系列的男孩");
}
}
4.新年男孩HNBoy.java
package com.czhappy.designpattern.factory;
public class HNBoy implements Boy{
@Override
public void drawMan() {
System.out.println("新年系列的男孩");
}
}
5.圣诞女孩MCGirl.java
package com.czhappy.designpattern.factory;
public class MCGirl implements Girl {
@Override
public void drawWoman() {
System.out.println("圣诞系列的女孩");
}
}
6.新年女孩HNGirl.java
package com.czhappy.designpattern.factory;
public class HNGirl implements Girl{
@Override
public void drawWoman() {
System.out.println("新年系列的女孩");
}
}
7.工厂接口PersonFactory.java
package com.czhappy.designpattern.factory;
public interface PersonFactory {
public Boy getBoy();
public Girl getGirl();
}
8.圣诞服装加工厂MCFactory.java
package com.czhappy.designpattern.factory;
/**
*
* @ClassName: MCFactory
* @Description: 圣诞系列加工厂
* @author chenzheng
* @date 2017-1-13 下午06:05:37
*/
public class MCFactory implements PersonFactory{
@Override
public Boy getBoy() {
// TODO Auto-generated method stub
return new MCBoy();
}
@Override
public Girl getGirl() {
// TODO Auto-generated method stub
return new MCGirl();
}
}
9.新年服装加工厂HNFactory.java
package com.czhappy.designpattern.factory;
/**
*
* @ClassName: HNFactory
* @Description: 新年系列加工厂
* @author chenzheng
* @date 2017-1-13 下午05:45:18
*/
public class HNFactory implements PersonFactory{
@Override
public Boy getBoy() {
// TODO Auto-generated method stub
return new HNBoy();
}
@Override
public Girl getGirl() {
// TODO Auto-generated method stub
return new HNGirl();
}
}
10.测试类Test.java
package com.czhappy.designpattern.factory;
public class Test {
public static void main(String[] args) {
PersonFactory factory1 = new MCFactory();
Girl girl1 = factory1.getGirl();
girl1.drawWoman();
PersonFactory factory2 = new HNFactory();
Boy boy1 = factory2.getBoy();
boy1.drawMan();
}
}
四、总结