工厂方法模式(Factory Method Pattern)
来源:互联网 发布:ubuntu挂起怎么启动 编辑:程序博客网 时间:2024/05/23 12:49
1. 工厂方法模式
工厂模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。
总体来说,工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建。
在以下的三种模式中:
- 第一种如果传入的字符串有误,不能正确创建对象,
- 第三种相对于第二种,不需要实例化工厂类。
所以,大多数情况下,我们会选用第三种——静态工厂方法模式。
1.1 简单工厂模式
在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象。
public class FactoryMethodTest { public static void main(String[] args){ SendFactory factory=new SendFactory(); Sender sd1=factory.produce("email"); Sender sd2=factory.produce("msg"); Sender sd3=factory.produce("hehe"); sd1.send(); sd2.send(); //sd3.send(); }}//共同接口interface Sender{ public void send();}//其实现类class EmailSender implements Sender{ @Override public void send(){ System.out.println("this is an emailsender"); }}class MsgSender implements Sender{ @Override public void send(){ System.out.println("this is a msgsender"); }}//工厂类class SendFactory{ public Sender produce(String type){ if("email".equals(type)){ return new EmailSender(); }else if("msg".equals(type)){ return new MsgSender(); } System.out.println("type is not correct"); return null; }}
输出:
type is not correctthis is an emailsenderthis is a msgsender
1.2 多个工厂方法模式
多个工厂方法模式,是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。
//工厂类class SendFactory{ public Sender produceEmailSender(){ return new EmailSender(); } public Sender produceMsgSender(){ return new MsgSender(); }}//测试public class FactoryMethodTest2 { public static void main(String[] args){ SendFactory factory=new SendFactory(); Sender sd1=factory.produceEmailSender(); Sender sd2=factory.produceMsgSender(); sd1.send(); sd2.send(); }}
运行结果:
this is an emailsenderthis is a msgsender
1.3 静态工厂方法模式
将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。
//工厂类class SendFactory{ public static Sender produceEmailSender(){ return new EmailSender(); } public static Sender produceMsgSender(){ return new MsgSender(); }}//测试public class FactoryMethodTest3 { public static void main(String[] args){ //SendFactory factory=new SendFactory(); //Sender sd1=factory.produceEmailSender(); //Sender sd2=factory.produceMsgSender(); Sender sd1=SendFactory.produceEmailSender(); Sender sd2=SendFactory.produceMsgSender(); sd1.send(); sd2.send(); }}
0 0
- 工厂方法模式(factory method pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(factory method pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)。
- 设计模式2:Factory Method Pattern(工厂方法模式)
- 设计模式(2):工厂方法模式(Factory Method Pattern)
- Java设计模式:工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式(Factory Method Pattern)
- 工厂方法模式 Factory Method Pattern
- 二、工厂方法模式(Factory Method Pattern)
- 工厂方法模式 The Factory Method Pattern
- iOS从App跳转至系统设置菜单各功能项跳到系统设
- React Native Android Gradle 编译流程浅析(二)
- 深度学习有趣的应用
- word2vec笔记
- 现在完成时
- 工厂方法模式(Factory Method Pattern)
- 怎么通过浏览器的f12,捕获http请求?
- c++ 把TXT文件中的数据保存为二维数组
- 小甲鱼PE详解之区块描述、对齐值以及RVA详解(PE详解06)
- [知其然不知其所以然-40] Why pm_parepare_console takes more than 200ms - swicth vt during suspend
- 线程同步的几种实现方法
- 小甲鱼PE详解之输入表(导入表)详解2(PE详解08)
- NS3搭建网络拓扑图
- C语言中的一些关键字(十三)