Java 之 23 种设计模式解析——创建模式、简单工厂模式
三、Java 的 23 中设计模式
A、创建模式
从这一块开始,我们详细介绍 Java 中 23 种设计模式的概念,应用场景等情况,并结合他们的特点及设计模式的原则进行分析。
首先,简单工厂模式不属于 23 中涉及模式,简单工厂一般分为:普通简单工厂、多方法简单工厂、静态方法简单工厂。
0、简单工厂模式
简单工厂模式模式分为三种:
01、普通
就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。首先看下关系图:
举例如下:(我们举一个发送邮件和短信的例子) 首先,创建二者的共同接口:
[java] view plaincopy |
|
|
1. public interface Sender { |
2. public void Send(); |
|
3. } |
其次,创建实现类:
[java] view plaincopy |
|
|
1. public class MailSender implements Sender { |
2. @Override |
|
3. public void Send() { |
|
4. System.out.println("this is mailsender!"); |
|
5. } |
|
|
6. } |
[java] view plaincopy |
|
|
1. public class SmsSender implements Sender { |
2. |
|
3. @Override |
|
4. public void Send() { |
|
5. System.out.println("this is sms sender!"); |
|
6. } |
|
7. } |
最后,建工厂类:
[java] view plaincopy |
|
|
1. public class SendFactory { |
2. |
|
3. public Sender produce(String type) { |
|
4. if ("mail".equals(type)) { |
|
5. return new MailSender(); |
|
6. } else if ("sms".equals(type)) { |
|
7. return new SmsSender(); |
|
8. } else { |
|
9. System.out.println("请输入正确的类型!"); |
|
10. return null; |
|
11. } |
|
12. } |
|
13. } |
我们来测试下:
1. public class FactoryTest { |
2. |
3. public static void main(String[] args) { |
4. SendFactory factory = new SendFactory(); |
5. Sender sender = factory.produce("sms"); |
6. sender.Send(); |
7. } |
8. } |
输出:this is sms sender!
02、多个方法
是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象, 而多个工厂方法模式是提供多个工厂方法,分别创建对象。关系图:
将上面的代码做下修改,改动下 SendFactory 类就行,如下:
测试类如下:
[java] view plaincopy |
|
|
1. public class FactoryTest { |
2. |
|
3. public static void main(String[] args) { |
|
4. SendFactory factory = new SendFactory(); |
|
5. Sender sender = factory.produceMail(); |
|
6. sender.Send(); |
|
7. } |
|
8. } |
输出:this is mailsender!
03、多个静态方法
将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。
|
1. public class SendFactory { |
||
2. |
|||
3. public static Sender produceMail(){ |
|||
4. return new MailSender(); |
|||
5. |
|
} |
|
6. |
|||
7. public static Sender produceSms(){ |
|||
8. return new SmsSender(); |
|||
9. |
|
} |
|
|
10. } |
||
[java] view plaincopy |
|||
|
1. public class FactoryTest { |
||
2. |
|||
3. public static void main(String[] args) { |
|||
4. Sender sender = SendFactory.produceMail(); |
|||
5. sender.Send(); |
|||
6. |
|
} |
|
7. |
} |
|
输出:this is mailsender!
总体来说,工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建。在以上的三种模式中,第一种如果传入的字符串有误,不能正确创建对象,第三种相对于第二种,不需要实例化工厂类,所以,大多数情况下,我们会选用第三种——静态工厂方法模式。