工厂模式

2019-08-12  本文已影响0人  天空在微笑
简单工厂模式(补充)

也称为静态工厂方法模式,由一个工厂对象决定创建出哪一种产品类的实例。

简单工厂模式中有如下角色:

简单示例

1、抽象产品类

public abstract class MobilePhone {
    public abstract String brand();
}

2、具体产品类

public class XiaoMiMobilePhone extends MobilePhone {
    @Override
    public String brand() {
        return "xiaomi";
    }
}

public class HuaWeiMobilePhone extends MobilePhone {
    @Override
    public String brand() {
        return "huawei";
    }
}


public class VivoMobilePhone extends MobilePhone {
    @Override
    public String brand() {
        return "vivo";
    }
}

3、工厂类

public class MobilePhoneFactory {
    public static MobilePhone createMobilePhone(String type) {
        MobilePhone mobilePhone = null;
        switch (type) {
            case "xiaomi":
                mobilePhone = new XiaoMiMobilePhone();
                break;
            case "huawei":
                mobilePhone = new HuaWeiMobilePhone();
                break;
            case "vivo":
                mobilePhone = new VivoMobilePhone();
                break;
        }
        return mobilePhone;
    }
}

测试

  @Test
    public void testSimpleFactory() {
        System.out.println("xiaomi:"+ MobilePhoneFactory.createMobilePhone("xiaomi").brand());
        System.out.println("huawei:"+ MobilePhoneFactory.createMobilePhone("huawei").brand());
        System.out.println("vivo:"+ MobilePhoneFactory.createMobilePhone("vivo").brand());
    }

测试结果

xiaomi:xiaomi
huawei:huawei
vivo:vivo

它需要知道所有工厂类型,因此只适合工厂类负责创建的对象比较少的情况。
避免直接实例化类,降低耦合性。
增加新产品需要修改工厂,违背开放封闭原则。

工厂方法模式

定义一个用于创建对象的接口,使类的实例化延迟到子类。

工厂方法有以下角色:

简单示例

抽象产品类和具体产品类同简单工厂一样。

1、抽象工厂类

public abstract class AbstractMobilePhoneFactory {
    public abstract  <T extends MobilePhone> T createMobilePhone(Class<T> clz);
}

4、具体工厂类

public class MobilePhoneFactoryImpl extends AbstractMobilePhoneFactory {
    @Override
    public   <T extends MobilePhone> T createMobilePhone(Class<T> clz) {
        MobilePhone mobilePhone = null;
        String classname = clz.getName();
        try {
            mobilePhone = (MobilePhone) Class.forName(classname).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (T) mobilePhone;
    }
}

获取工厂实例单例

//第一次调用getInstance方法时虚拟机才加载SingletonHolder并初始化sInstance,这样保证了线程安全和实例的唯一性。
public class MobilePhoneFactoryImplSingleton {
    private MobilePhoneFactoryImplSingleton() {
    }
    public static MobilePhoneFactoryImpl getInstance() {
        return SingletonHolder.sInstance;
    }
    private static class SingletonHolder {
        private static final MobilePhoneFactoryImpl sInstance = new MobilePhoneFactoryImpl();
    }
}
@Test
public void testAbstractFactory() {
        System.out.println("instance1:"+ MobilePhoneFactoryImplSingleton.getInstance());
        System.out.println("instance2:"+ MobilePhoneFactoryImplSingleton.getInstance());
        System.out.println("vivo:"+ MobilePhoneFactoryImplSingleton.getInstance().createMobilePhone(VivoMobilePhone.class).brand());
        System.out.println("huawei:"+MobilePhoneFactoryImplSingleton.getInstance().createMobilePhone(HuaWeiMobilePhone.class).brand());
        System.out.println("xiaomi:"+ MobilePhoneFactoryImplSingleton.getInstance().createMobilePhone(XiaoMiMobilePhone.class).brand());
    }

测试结果

instance1:sysshare.lq.com.demosapplication.designPattern.factory.abstractFactory.MobilePhoneFactoryImpl@2957fcb0
instance2:sysshare.lq.com.demosapplication.designPattern.factory.abstractFactory.MobilePhoneFactoryImpl@2957fcb0
vivo:vivo
huawei:huawei
xiaomi:xiaomi
上一篇 下一篇

猜你喜欢

热点阅读