- 代码不完全参照原书 , 借鉴书中相关例子和部分概念
- 顺序部分参考原书,部分引用原书 UML 类图,会结合实际应用描述优缺点
- 工厂模式(简单工厂模式,工厂方法模式,抽象工厂模式)
import java.util.Scanner;
public class SingleClassImpl {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter receiver name:");
String receiverName = in.next();
System.out.println("Please choose one type to send msg:[Enter numbers]");
System.out.println("1. Send by message app on your phone.");
System.out.println("2. Send by email");
System.out.println("3. Send by WeChat");
System.out.println();
int typeNo = 0;
try {
typeNo = in.nextInt();
} catch (Exception e) {
// handle exception
System.out.println("Invalid input!");
}
switch (typeNo) {
case 1:
System.out.println(String.format("Send to %s by message", receiverName));
break;
case 2:
System.out.println(String.format("Send to %s by email", receiverName));
break;
case 3:
System.out.println(String.format("Send to %s by WeChat", receiverName));
break;
default:
System.out.println("Invalid Input!");
break;
}
}
}
sendMsg()
方法。Factory
类来管理各种信息发送方式。Common Interface
和具体的 Sender
类// common interface
public interface CommonInterfaceSender {
void sendMsg(String receiver);
}
// SmsSender class(Message app in phone)
public class SmsSender implements CommonInterfaceSender {
@Override
public void sendMsg(String receiver) {
System.out.println(String.format("Send to %s by message", receiver));
}
}
// MailSender class
public class MailSender implements CommonInterfaceSender {
@Override
public void sendMsg(String receiver) {
System.out.println(String.format("Send to %s by email", receiver));
}
}
// WeChatSender class
public class WechatSender implements CommonInterfaceSender {
@Override
public void sendMsg(String receiver) {
System.out.println(String.format("Send to %s by WeChat", receiver));
}
}
Factory Class
和 Main Class
// Create different sender class instance according to param
public class OrdinaryFactory {
public CommonInterfaceSender sendMsg(int type) {
if (1 == type) {
return new SmsSender();
} else if (2 == type) {
return new MailSender();
} else if (3 == type) {
return new WechatSender();
} else {
System.out.println("Please check the input!");
return null;
}
}
}
public class Main {
public static void main(String[] args) {
OrdinaryFactory ordinaryFactory = new OrdinaryFactory();
Scanner in = new Scanner(System.in);
System.out.println("Please enter receiver name:");
String receiverName = in.next();
System.out.println("Please choose one type to send msg:[Enter numbers]");
System.out.println("1. Send by message app on your phone.");
System.out.println("2. Send by email");
System.out.println("3. Send by WeChat");
System.out.println();
int typeNo = 0;
try {
typeNo = in.nextInt();
} catch (Exception e) {
// handle exception
System.out.println("Invalid input!");
}
// Use parent interface to point the object.
CommonInterfaceSender sender = ordinaryFactory.sendMsg(typeNo);
// invoke parent interface method.
sender.sendMsg(receiverName);
}
}
Main
解耦,添加新的发送短信的方式时,只需要新建对应的 Sender
,并简单调整对应的 Factory
。Main
)和业务逻辑代码中仍然存在依赖,Factory
起了决定作用。同时对于传递的参数具有依赖性,参数的不合法导致程序运行的不合法。Factory
的判断逻辑,对修改开放。开放-封闭原则:软件实体应该是可扩展,而不可修改的。也就是说,对扩展是开放的,而对修改是封闭的。
Sender Class
创建Factory
类,来代替简单工厂模式中Factory Class
的作用,将判断的逻辑(实例化子类的逻辑)由Factory
转移到客户端。// common factory
public interface IMsgFactory {
CommonInterfaceSender sendMsg();
}
// Mail factory
public class SendMailFactory implements IMsgFactory {
@Override
public CommonInterfaceSender sendMsg() {
return new MailSender();
}
}
// Sms factory
public class SendSmsFactory implements IMsgFactory {
@Override
public CommonInterfaceSender sendMsg() {
return new SmsSender();
}
}
// WeChat factory
public class WechatFactory implements IMsgFactory {
@Override
public CommonInterfaceSender sendMsg() {
return new WechatSender();
}
}
public class FactoryMethodMain {
public static void main(String[] args) {
// Choose one specific class to create factory referring demand
IMsgFactory factory = new SendMailFactory();
//IMsgFactory factory = new SendSmsFactory();
//IMsgFactory factory = new WechatFactory();
CommonInterfaceSender sender = factory.createSender();
sender.sendMsg("Elvis");
}
}
public interface CommonInterfaceReceiver {
void receiveMsg(String from);
}
public class MailReceiver implements CommonInterfaceReceiver {
@Override
public void receiveMsg(String from) {
System.out.println(String.format("Receive message from %s by email", from));
}
}
public class SmsReceiver implements CommonInterfaceReceiver {
@Override
public void receiveMsg(String from) {
System.out.println(String.format("Receive message form %s by sms", from));
}
}
public class WechatReceiver implements CommonInterfaceReceiver {
@Override
public void receiveMsg(String from) {
System.out.println(String.format("Receive message from %s by wechat", from));
}
}
public abstract class AbstractCommunicationFactory {
public abstract CommonInterfaceSender getSender(int typeNum);
public abstract CommonInterfaceReceiver getReceiver(int typeNum);
}
public class ReceiverFactory extends AbstractCommunicationFactory {
@Override
public CommonInterfaceSender getSender(int typeNum) {
return null;
}
@Override
public CommonInterfaceReceiver getReceiver(int typeNum) {
switch (typeNum) {
case 0:
return new SmsReceiver();
case 1:
return new MailReceiver();
case 2:
return new WechatReceiver();
default:
return null;
}
}
}
public class SenderFactory extends AbstractCommunicationFactory {
@Override
public CommonInterfaceSender getSender(int typeNum) {
switch (typeNum) {
case 0:
return new SmsSender();
case 1:
return new MailSender();
case 2:
return new WechatSender();
default:
return null;
}
}
@Override
public CommonInterfaceReceiver getReceiver(int typeNum) {
return null;
}
}
ublic class FactoryProducer {
public static AbstractCommunicationFactory getFactory(String direction) {
if ("Send".equals(direction)) {
return new SenderFactory();
} else if ("Receive".equals(direction)) {
return new ReceiverFactory();
}
return null;
}
}
public class AbstractFactoryMain {
public static void main(String[] args) {
String to = "Elvis";
String from = "Shunzi";
AbstractCommunicationFactory senderFactory = FactoryProducer.getFactory("Send");
// sms sender
CommonInterfaceSender smsSender = senderFactory.getSender(0);
smsSender.sendMsg(to);
// MailSender
CommonInterfaceSender mailSender = senderFactory.getSender(1);
smsSender.sendMsg(to);
// WeChat Sender
CommonInterfaceSender wechatSender = senderFactory.getSender(2);
wechatSender.sendMsg(to);
AbstractCommunicationFactory receiverFactory = FactoryProducer.getFactory("Receive");
// sms receiver
CommonInterfaceReceiver smsReceiver = receiverFactory.getReceiver(0);
smsReceiver.receiveMsg(from);
CommonInterfaceReceiver mailReceiver = receiverFactory.getReceiver(1);
mailReceiver.receiveMsg(from);
CommonInterfaceReceiver wechatReceiver = receiverFactory.getReceiver(2);
wechatReceiver.receiveMsg(from);
}
}
> Send to Elvis by message
> Send to Elvis by message
> Send to Elvis by WeChat
> Receive message form Shunzi by sms
> Receive message from Shunzi by email
> Receive message from Shunzi by wechat
- 源码地址:Elvis Github
- 友情链接一(略有参考其他前辈的博客)
- 友情链接二(菜鸟教程设计模式大汇总)
- UML 图(感觉不怎么需要,例子比较简单。觉得之后有必要的时候再画吧…)
- 后续会结合实际开发以及相关框架中的应用进一步解释工厂模式带来的实际作用。留坑。。(逃