设计模式之---创建型模式(一)--工厂方法模式

工厂方法模式分为三种:普通工厂模式、多个工厂方法模式和静


态工厂方法模式。




普通工厂模式


就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。首先看下关系图:




创建发送接口(抽象):


package com.panther.demo.exercise7;

/**
 * 发送共同接口
 * Created by panther.dongdong on 2016/3/13.
 */
public interface Sender {
    public void send();
}

邮件的实现类:

package com.panther.demo.exercise7;

/**
 * 邮件发送实现类
 * Created by panther.dongdong on 2016/3/13.
 */
public class MailSender implements Sender {
    @Override
    public void send() {
        System.out.println("send mail");
    }
}

短信实现类:


package com.panther.demo.exercise7;

/**
 * 短信发送实现类
 * Created by panther.dongdong on 2016/3/13.
 */
public class SmsSender implements Sender {
    @Override
    public void send() {
        System.out.println("send short message");
    }
}

工厂类:

package com.panther.demo.exercise7;

/**
 * 普通工厂类
 * Created by panther.dongdong on 2016/3/13.
 */
public class SendFactory {
    private static final String MAIL_TYPE = "mail";
    private static final String SMS_TYPE = "sms";

    public Sender produce(String type) {
        if (MAIL_TYPE.equals(type)) {
            return new MailSender();
        } else if (SMS_TYPE.equals(type)) {
            return new SmsSender();
        } else {
            throw new RuntimeException("请输入正确的发送类型!!!");
        }
    }
}

普通工厂模式测试:

package com.panther.demo.exercise7;

/**
 * 工厂方法测试
 * Created by panther.dongdong on 2016/3/13.
 */
public class FactoryTest {
    public static void main(String[] args) {
        SendFactory sendFactory = new SendFactory();
        Sender sender = sendFactory.produce("mail");
        sender.send();
    }
}


运行结果:



多个工厂方法模式


是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。关系图:



将上面的代码做下修改,改动下SendFactory类就行,如下:


package com.panther.demo.exercise7;

/**
 * 普通工厂类
 * Created by panther.dongdong on 2016/3/13.
 */
public class SendFactory {
//    private static final String MAIL_TYPE = "mail";
//    private static final String SMS_TYPE = "sms";
//
//    public Sender produce(String type) {
//        if (MAIL_TYPE.equals(type)) {
//            return new MailSender();
//        } else if (SMS_TYPE.equals(type)) {
//            return new SmsSender();
//        } else {
//            throw new RuntimeException("请输入正确的发送类型!!!");
//        }
//    }

    public Sender produceMail() {
        return new MailSender();
    }

    public Sender produceSms() {
        return new SmsSender();
    }
<span style="font-size:18px;"><span style="font-size:14px;">}</span>
</span>

测试类做如下修改:


package com.panther.demo.exercise7;

/**
 * 工厂方法测试
 * Created by panther.dongdong on 2016/3/13.
 */
public class FactoryTest {
    public static void main(String[] args) {
        SendFactory sendFactory = new SendFactory();
//        Sender sender = sendFactory.produce("mail");
//        sender.send();
        Sender sender = sendFactory.produceMail();
        sender.send();
    }
}

输出结果相同


静态工厂方法模式


将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。


package com.panther.demo.exercise7;

/**
 * 普通工厂类
 * Created by panther.dongdong on 2016/3/13.
 */
public class SendFactory {
//    private static final String MAIL_TYPE = "mail";
//    private static final String SMS_TYPE = "sms";
//
//    public Sender produce(String type) {
//        if (MAIL_TYPE.equals(type)) {
//            return new MailSender();
//        } else if (SMS_TYPE.equals(type)) {
//            return new SmsSender();
//        } else {
//            throw new RuntimeException("请输入正确的发送类型!!!");
//        }
//    }

    public static Sender produceMail() {
        return new MailSender();
    }

    public static Sender produceSms() {
        return new SmsSender();
    }
}

package com.panther.demo.exercise7;

/**
 * 工厂方法测试
 * Created by panther.dongdong on 2016/3/13.
 */
public class FactoryTest {
    public static void main(String[] args) {
//        SendFactory sendFactory = new SendFactory();
//        Sender sender = sendFactory.produce("mail");
//        sender.send();
        Sender sender = SendFactory.produceMail();
        sender.send();
    }
}

输出结果也是一样的!!!!


总结:


总体来说,工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建。在以上的三种模式中,第一种如果传入的字符串有误,不能正确创建对象,第三种相对于第二种,不需要实例化工厂类,所以,大多数情况下,我们会选用第三种——静态工厂方法模式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值