Java编程开发设计模式之工厂模式Fatory Method

        Fatory Method 工厂模式根据其具体实现可细化分为下面三种


1. 普通工厂模式

         创建一个工厂Class,对实现同一Interface的一批Classes进行实例的创建。类图如下:


代码示例如下:

         首先建立一个共同接口

public interface Connector {
    public boolean connect(String url);
}
    接着建立两个上面接口的实现类
public class Bluetooch implements Connector {
    @Override
    public boolean connect(String url) {
        System.out.println(String.format("%s.%s called!",this.getClass().getName(),"connect()"));
        return false;
    }
}
public class Httprequest implements Connector {
    @Override
    public boolean connect(String url) {
        System.out.println(String.format("%s.%s called!",this.getClass().getName(),"connect()"));
        return false;
    }
}
之后建立工厂类
public class ConnectFactory {
    public enum ConnectType{
        HTTPREQUEST,BLUETOOCH
    }
    public Connector produce(ConnectType type){
        Connector production;
        switch (type){
            case BLUETOOCH:
                production = new Bluetooch();
                break;
            case HTTPREQUEST:
                production = new Httprequest();
                break;
            default:
                production = new Httprequest();
        }
        return production;
    }
}
   使用WorkClass 进行测试:
public class WorkClass {
    public void test()
    {
        ConnectFactory connectFactory=new ConnectFactory();
        Connector connectorHTTP =connectFactory.produce(ConnectFactory.ConnectType.HTTPREQUEST);
        connectorHTTP.connect("www.xx.com");
        Connector connectorBLUE=connectFactory.produce(ConnectFactory.ConnectType.BLUETOOCH);
        connectorBLUE.connect("bluetooch://");
    }
}

    输出结果:
designpattern.creator.Httprequest.connect() called!
designpattern.creator.Bluetooch.connect() called!

2. 多工厂方法模式

    是对普通工厂方法模式的改良,在普通工厂方法模式中,如果传递的类型不正确,则不能正确创建对象,且枚举也增加开销,而多个工厂方法模式是提供多个工厂方法,分别创建对象,代码如下

public class ConnectFactory {

public Connector produceBluetooch()
{
    return new Bluetooch();
}

public Connector produceHttprequest()
{
    return new Httprequest();
}
 } 

    测试代码:

public class WorkClass {
    public void test()
    {
        ConnectFactory connectFactory=new ConnectFactory();
Connector connectorHttp= connectFactory.produceHttprequest();  
        Connector connectorBLUETOOCH= connectFactory.produceBluetooch();       
        connectorHttp.connect("www.xx.com");        
        connectorBLUETOOCH.connect("bluetooch://");   
   }
}

输出结果:
designpattern.creator.Httprequest.connect() called!
designpattern.creator.Bluetooch.connect() called!
3.静态工厂方法模式
  将多工厂方法模式进行修改,对方法设置成静态,这样不用创建工厂类

public class ConnectFactory {

    public static  Connector SproduceBluetooch()
    {
        return new Bluetooch();
    }

    public static Connector SproduceHttprequest()
    {
        return new Httprequest();
    }
}
  测试类
public class WorkClass {
    public void test()
    {
        Connector connectorHTTPS= ConnectFactory.SproduceHttprequest();
        Connector connectorBLUETOOCHS= ConnectFactory.SproduceBluetooch();
        connectorHTTPS.connect("www.xx.com");
        connectorBLUETOOCHS.connect("bluetooch://");
    }
}
    输出结果:
designpattern.creator.Httprequest.connect() called!
designpattern.creator.Bluetooch.connect() called!

结语,工厂模式适合有大量的具有共同的功能接口产品类型需要创建时,可以通过工厂模式进行创建。
在以上的三种模式中,第一种如果传入的类型有误,不能正确创建对象,第三种相对于第二种,不需要实例化工厂类,所以,大多数情况下,我们会选用第三种——静态工厂方法模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值