1、设计模式关系图
2、工厂方法模式(Factory Method)
工程目录结构图
工厂方法模式主要由三部分组成
2.1、普通工厂模式
类图
相关代码
接口代码:
package com.sun.design.factory.ordinary.api;
/**
* 工厂模式--发送消息的接口
* @author suntan
* @dade 2017年3月28日下午4:48:52
* @version v1.0
*/
public interface ISender {
/**
* 实现发送消息
* @return void
*/
void sendMsg();
}
实现类代码:
package com.sun.design.factory.ordinary.api.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.design.factory.ordinary.api.ISender;
/**
* 发送邮件消息
* @author suntan
* @dade 2017年3月28日下午5:01:18
* @version v1.0
*/
public class MailSendMsg implements ISender {
/**
* 日志
*/
private Logger logger = LoggerFactory.getLogger(MailSendMsg.class);
@Override
public void sendMsg() {
logger.info("=====MailSendMsg======>执行发送邮件消息");
}
}
package com.sun.design.factory.ordinary.api.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.design.factory.ordinary.api.ISender;
public class SmsSendMsg implements ISender {
/**
* 日志
*/
private Logger logger = LoggerFactory.getLogger(SmsSendMsg.class);
@Override
public void sendMsg() {
logger.info("======SmsSendMsg====>执行短信发送消息!");
}
}
工厂类代码:
package com.sun.design.factory.ordinary.util;
import com.sun.design.factory.ordinary.api.ISender;
import com.sun.design.factory.ordinary.api.impl.MailSendMsg;
import com.sun.design.factory.ordinary.api.impl.SmsSendMsg;
/**
* 多个工厂模式(more)
* 是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,
* @author suntan
* @dade 2017年3月29日上午9:46:53
* @version v1.0
*/
public class MSendMsgFactoryUtil {
/**
* 发送短信息
* @return
*/
public ISender structureSms(){
return new SmsSendMsg();
}
public ISender structureMail() {
return new MailSendMsg();
}
}
package com.sun.design.factory.ordinary.util;
import com.sun.design.factory.ordinary.api.ISender;
import com.sun.design.factory.ordinary.api.impl.MailSendMsg;
import com.sun.design.factory.ordinary.api.impl.SmsSendMsg;
/**
* 静态工厂,直接调用
* @author suntan
* @dade 2017年3月29日上午9:54:20
* @version v1.0
*/
public class StaticSendMsgFactoryUtil {
/**
* 发送短信息
* @return
*/
public static ISender structureSms() {
return new SmsSendMsg();
}
/**
* 发送邮件消息
* @return
*/
public static ISender structureMail() {
return new MailSendMsg();
}
}
package com.sun.design.factory.ordinary.util;
import org.apache.commons.lang3.StringUtils;
import com.sun.design.factory.ordinary.api.ISender;
import com.sun.design.factory.ordinary.api.impl.MailSendMsg;
import com.sun.design.factory.ordinary.api.impl.SmsSendMsg;
/**
* 发送消息的工厂
*
* @author suntan
* @dade 2017年3月29日上午9:02:11
* @version v1.0
*/
public class SendMsgFactoryUtil {
/**
* 构建消息
*
* @param type
* @return
*/
public ISender structure(String type) {
if(StringUtils.equals(type, "sms")) {
return new SmsSendMsg();
}if (StringUtils.equals(type, "mail")) {
return new MailSendMsg();
}else {
return null;
}
}
}
测试代码:
package com.sun.design.factory.ordinary.test;
import org.junit.Test;
import com.sun.design.factory.ordinary.api.ISender;
import com.sun.design.factory.ordinary.util.MSendMsgFactoryUtil;
/**
* 多个工厂模式
* @author suntan
* @dade 2017年3月29日上午9:49:15
* @version v1.0
*/
public class MSendMsgFactoryUtilTest {
@Test
public void testSendMsg() {
//初始化多例的工厂
MSendMsgFactoryUtil factory = new MSendMsgFactoryUtil();
ISender sender = null;
//sender = factory.structureMail();//通过工厂构造发邮件
sender = factory.structureSms();//通过工厂构造发短信息
sender.sendMsg();
}
}
package com.sun.design.factory.ordinary.test;
import org.junit.Test;
import com.sun.design.factory.ordinary.api.ISender;
import com.sun.design.factory.ordinary.util.SendMsgFactoryUtil;
/**
* 测试工厂发送消息
* @author suntan
* @dade 2017年3月29日上午9:16:18
* @version v1.0
*/
public class SendMsgFactoryUtilTest {
/**
* 测试构建消息
*/
@Test
public void testStructure() {
//初始化工厂实例
SendMsgFactoryUtil factory = new SendMsgFactoryUtil();
ISender sender = null;
//sender = factory.structure("sms");
sender = factory.structure("mail");
//调用方法
sender.sendMsg();
}
}
package com.sun.design.factory.ordinary.test;
import org.junit.Test;
import com.sun.design.factory.ordinary.api.ISender;
import com.sun.design.factory.ordinary.util.StaticSendMsgFactoryUtil;
/**
* 静态工厂测试方法
* @author suntan
* @dade 2017年3月29日上午9:56:36
* @version v1.0
*/
public class StaticSendMsgFactoryUtilTest {
@Test
public void test() {
//不用实例化工厂,直接调用
ISender sender = null;
//sender = StaticSendMsgFactoryUtil.structureMail();//发邮件
sender = StaticSendMsgFactoryUtil.structureSms();
sender.sendMsg();
}
}