Java设计模式之工厂方法模式(FactoryMethod)

一、 设计模式的分类

大体可以分为三类:

  • 创建型模式(5个)
    单例模式、原型模式、工厂方法模式、抽象工厂模式、建造者模式
  • 结构性模式(7个)
    适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式
  • 行为型模式(11个)
    观察者模式、策略模式、模板方法模式、迭代子模式、责任链模式、 命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式

###工厂方法模式
工厂方法分为3类:

  • 普通工厂模式
    创建一个工厂类,对实现了同一个接口的一些类进行实例的创建
    首先我们看一下关系图
    image

首先我们新建一个接口

1
2
3
public interface Sender {
void send();
}

然后创建实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class EmailSender implements Sender {

public void send() {
System.out.println("send a message by Email!!!");
}
}

public class MessageSender implements Sender {

public void send() {
System.out.println("send a message by MessageSender");

}

}

最后建工厂类

1
2
3
4
5
6
7
8
9
10
11
12
public class SendFactory {
public Sender produce(String type){
if("message".equals(type)){
return new MessageSender();
}else if("email".equals(type)){
return new EmailSender();
}else{
System.out.println("请输入正确的类型");
return null;
}
}
}

然后我们测试一下:

1
2
3
4
5
6
7
public class FactoryTest {
public static void main(String[] args) {
SendFactory factory = new SendFactory();
Sender sender = factory.produce("email");
sender.send();
}
}

输出结果

1
send a message by Email!!!

  • 多个工厂方法模式
    提供多个工厂方法,分别创建对象
    只需要修改工厂类部分代码即可:
    1
    2
    3
    4
    5
    6
    7
    8
    public class SendFactory {
    public Sender produceMail(){
    return new EmailSender();
    }
    public Sender produceMessage(){
    return new MessageSender();
    }
    }

测试类

1
2
3
4
5
6
7
8
public class FactoryTest {
public static void main(String[] args) {
SendFactory factory = new SendFactory();
// Sender sender = factory.produce("email");
Sender mail = factory.produceMessage();
mail.send();
}
}

输出结果

1
send a message by MessageSender

  • 静态工厂方法
    将工厂类中的方法设置为静态,工厂类不需要创建实例,直接调用方法创建实例
    1
    2
    3
    4
    5
    6
    7
    8
    public class SendFactory {
    public static Sender produceMail(){
    return new EmailSender();
    }
    public static Sender produceMessage(){
    return new MessageSender();
    }
    }

测试类:

1
2
3
4
5
6
public class FactoryTest {
public static void main(String[] args) {
Sender mail = SendFactory.produceMessage();
mail.send();
}
}

适用场景:有大量的产品需要创建实例,并且这些类实现了同一个接口

抽象工厂模式

工厂方法模式的一个缺陷是类的创建依赖于工厂类,这就意味着,如果想要拓展程序,需要改工厂类的代码,这就违背了闭包原则。抽象工厂模式就可以解决这个问题。创建多个工厂类,一旦需要添加新功能,只需要添加一个工厂类就行了。
image
一个接口

1
2
3
4
public interface Provider {
Sender produce();

}

两个工厂类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MessageFactory implements Provider {

public Sender produce() {
return new MessageSender();
}

}
public class MessageSender implements Sender {

public void send() {
System.out.println("send a message by MessageSender");

}

}

测试类

1
2
3
4
5
6
7
8
9
public class FactoryTest {
public static void main(String[] args) {
// Sender mail = SendFactory.produceMessage();
// mail.send();
MessageFactory messageFactory = new MessageFactory();
Sender sender = messageFactory.produce();
sender.send();
}
}

最后输出

1
send a message by MessageSender

待续,未完…