多个工厂方法模式

是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。关系图:
在简单工程的上面修改
/**
 * @author Liufei
 * @date 2020/4/10 2:06 下午
 */
public class Factory {
    public Sender productMail() {
        return new MailSender();
    }
    public Sender productSms() {
        return new SmsSender();
    }
}
/**
 * @author Liufei
 * @date 2020/4/10 2:08 下午
 */
public class MoreFactoryTest {

    public static void main(String[] args) {
        Factory factory = new Factory();
        Sender sender = factory.productMail();
        sender.send();
    }
}





posted @ 2021-08-04 00:22  刘翊扬  阅读(81)  评论(0编辑  收藏  举报