24种设计模式--工厂模式(Factory)创建型

1.简单工厂模式 simpleFactory

概述

工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

何时使用:我们明确地计划不同条件下创建不同实例时。

接口类

package com.dahan.simlpefactory;

/**
 * 大汉软件
 */
public interface Phone {
    void makeCall();
}

实现类

package com.dahan.simlpefactory.impl;

import com.dahan.simlpefactory.Phone;

/**
 * 大汉软件
 */
public class HuaweiPhoneImpl implements Phone {
    @Override
    public void makeCall() {
        System.out.println("用华为手机打电话");
    }
}
package com.dahan.simlpefactory.impl;

import com.dahan.simlpefactory.Phone;

/**
 * 大汉软件
 */
public class MiPhoneImpl implements Phone {
    @Override
    public void makeCall() {
        System.out.println("用小米手机打电话");
    }
}

简单工场类

package com.dahan.simlpefactory;

import com.dahan.simlpefactory.impl.HuaweiPhoneImpl;
import com.dahan.simlpefactory.impl.MiPhoneImpl;

/**
 * 大汉软件
 */
public class SimplePhoneFactory {
    public Phone getBean(String phoneType) {
        Phone phone;
        if ("mi".equals(phoneType)) {
            phone = new MiPhoneImpl();
        } else if ("huawei".equals(phoneType)) {
            phone = new HuaweiPhoneImpl();
        } else {
            return null;
        }
        return phone;
    }
}

测试类

package com.dahan.simlpefactory;

/**
 * 大汉软件
 */
public class Test {
    public static void main(String[] args) {
        SimplePhoneFactory factory = new SimplePhoneFactory();
        Phone mi = factory.getBean("mi");
        mi.makeCall();

        Phone huawei = factory.getBean("huawei");
        huawei.makeCall();
    }
}
测试结果:

用小米手机打电话
用华为手机打电话

2.抽象工厂模式 AbstractFactory

概述

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂

接口

package com.dahan.simlpefactory;

/**
 * 大汉软件
 */
public interface Phone {
    void makeCall();
}


package com.dahan.simlpefactory;

/**
 * 大汉软件
 */
public interface Watch {
    void LookTime();
}

实现类

package com.dahan.simlpefactory.impl;

import com.dahan.simlpefactory.Phone;

/**
 * 大汉软件
 */
public class HuaweiPhoneImpl implements Phone {
    @Override
    public void makeCall() {
        System.out.println("用华为手机打电话");
    }
}

package com.dahan.simlpefactory.impl;

import com.dahan.simlpefactory.Phone;

/**
 * 大汉软件
 */
public class MiPhoneImpl implements Phone {
    @Override
    public void makeCall() {
        System.out.println("用小米手机打电话");
    }
}

package com.dahan.simlpefactory.impl;

import com.dahan.simlpefactory.Watch;

/**
 * 大汉软件
 */
public class HuaweiWatchImpl implements Watch {
    @Override
    public void LookTime() {
        System.out.println("华为手环看时间");
    }
}

package com.dahan.simlpefactory.impl;

import com.dahan.simlpefactory.Watch;

/**
 * 大汉软件
 */
public class MiWatchImpl implements Watch {
    @Override
    public void LookTime() {
        System.out.println("小米手环看时间");
    }
}

抽象工厂类、抽象工厂实现类

package com.dahan.simlpefactory;

/**
 * 大汉软件
 */
public abstract class AbstractFactory {
    public abstract Phone getPhone(String phoneType);

    public abstract Watch getWatch(String watchType);
}
package com.dahan.simlpefactory;

import com.dahan.simlpefactory.impl.HuaweiPhoneImpl;
import com.dahan.simlpefactory.impl.MiPhoneImpl;

/**
 * 大汉软件
 */
public class PhoneFactory extends AbstractFactory {
    @Override
    public Phone getPhone(String phoneType) {
        if(phoneType == null){
            return null;
        }
        if(phoneType.equalsIgnoreCase("mi")){
            return new MiPhoneImpl();
        } else if(phoneType.equalsIgnoreCase("huawei")){
            return new HuaweiPhoneImpl();
        } else {
            return null;
        }
    }

    @Override
    public Watch getWatch(String watchType) {
        return null;
    }
}


package com.dahan.simlpefactory;

import com.dahan.simlpefactory.impl.HuaweiWatchImpl;
import com.dahan.simlpefactory.impl.MiWatchImpl;

/**
 * 大汉软件
 */
public class WatchFactory extends AbstractFactory {
    @Override
    public Phone getPhone(String watchType) {
        return null;
    }

    @Override
    public Watch getWatch(String watchType) {
        if (watchType == null) {
            return null;
        }
        if (watchType.equalsIgnoreCase("mi")) {
            return new MiWatchImpl();
        } else if (watchType.equalsIgnoreCase("huawei")) {
            return new HuaweiWatchImpl();
        } else {
            return null;
        }
    }
}

工厂创造器

package com.dahan.simlpefactory;

/**
 * 大汉软件
 */
public class FactoryProducer {
    public static AbstractFactory getFactory(String choice) {
        if (choice.equalsIgnoreCase("phone")) {
            return new PhoneFactory();
        } else if (choice.equalsIgnoreCase("watch")) {
            return new WatchFactory();
        }
        return null;
    }
}

测试类

package com.dahan.simlpefactory;

/**
 * 大汉软件
 */
public class Test {
    public static void main(String[] args) {
        AbstractFactory factory = FactoryProducer.getFactory("phone");
        assert factory != null;
        Phone miPhone = factory.getPhone("mi");
        Phone huaweiPhone = factory.getPhone("huawei");
        miPhone.makeCall();
        huaweiPhone.makeCall();

        factory = FactoryProducer.getFactory("watch");

        assert factory != null;
        Watch miWatch = factory.getWatch("mi");
        Watch huaweiWatch = factory.getWatch("huawei");
        miWatch.LookTime();
        huaweiWatch.LookTime();
    }
}
测试结果
用小米手机打电话
用华为手机打电话

小米手环看时间
华为手环看时间

参考链接

设计模式-菜鸟教程

posted @ 2023-01-30 23:56  土木转行的人才  阅读(68)  评论(0编辑  收藏  举报