工厂模式介绍
简单工厂
以下我们用支付场景演示简单工厂用法
1.定义支付接口
/**
 * 支付接口
 *
 * @author Lion Li
 */
public interface IPay {
    void payment();
}
 
2.编写工厂实例
/**
 * 付款方式工厂
 *
 * @author Lion Li
 */
public class PayFactory {
    /**
     * 创建付款实例
     * Class<? extends IPay> 单一原则 限制必须是 IPay 的子类
     */
    public IPay create(Class<? extends IPay> clazz) {
        if (null != clazz) {
            try {
                return clazz.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
 
3.编写具体的支付类 例如: 微信 支付宝
/**
 * 微信支付
 *
 * @author Lion Li
 */
public class WxPay implements IPay {
    public void payment() {
        System.out.println("微信支付");
    }
}
/**
 * 支付宝支付
 *
 * @author Lion Li
 */
public class ZfbPay implements IPay {
    public void payment() {
        System.out.println("支付宝支付");
    }
}
 
4.编写测试类并运行测试
/**
 * @author Lion Li
 */
public class Test {
    public static void main(String[] args) {
        IPay wx = new PayFactory().create(WxPay.class);
        IPay zfb = new PayFactory().create(ZfbPay.class);
        wx.payment();
        zfb.payment();
    }
}
 

工厂方法模式
以下我们用汽车场景工厂方法模式用法
1.定义汽车接口规范
/**
 * 汽车接口规范
 *
 * @author Lion Li
 */
public interface ICar {
    void name();
}
 
2.编写汽车工厂接口规范
/**
 * 汽车工厂接口规范
 *
 * @author Lion Li
 */
public interface ICarFactory {
    ICar create();
}
 
3.编写具体的汽车种类 例如: 宝马 大众
/**
 * 宝马汽车
 *
 * @author Lion Li
 */
public class BaoMaCar implements ICar {
    public void name() {
        System.out.println("我是宝马");
    }
}
/**
 * 大众汽车
 *
 * @author Lion Li
 */
public class DaZhongCar implements ICar {
    public void name() {
        System.out.println("我是大众");
    }
}
 
4.编写具体的汽车工厂 例如: 宝马工厂 大众工厂
/**
 * 宝马汽车工厂
 *
 * @author Lion Li
 */
public class BaoMaCarFactory implements ICarFactory {
    public ICar create() {
        return new BaoMaCar();
    }
}
/**
 * 大众汽车工厂
 *
 * @author Lion Li
 */
public class DaZhongCarFactory implements ICarFactory {
    public ICar create() {
        return new DaZhongCar();
    }
}
 
5.编写测试类并运行测试
/**
 * @author Lion Li
 */
public class Test {
    public static void main(String[] args){
        ICarFactory baomaFactory = new BaoMaCarFactory();
        ICar baoma = baomaFactory.create();
        baoma.name();
        ICarFactory dazhongFactory = new DaZhongCarFactory();
        ICar dazhong = dazhongFactory.create();
        dazhong.name();
    }
}
 

抽象工厂
以下我们用多种出行方式来演示抽象工厂用法
1.定义产品接口规范
/**
 * 汽车接口规范
 *
 * @author Lion Li
 */
public interface ICar {
    void name();
}
/**
 * 飞机接口规范
 *
 * @author Lion Li
 */
public interface IAircraft {
    void name();
}
 
2.定义产品工厂接口规范
/**
 * 汽车工厂接口规范
 *
 * @author Lion Li
 */
public interface ICarFactory {
    ICar create();
}
/**
 * 飞机工厂接口规范
 *
 * @author Lion Li
 */
public interface IAircraftFactory {
    IAircraft create();
}
 
3.定义所有产品实现
/**
 * 宝马汽车
 *
 * @author Lion Li
 */
public class BaoMaCar implements ICar {
    public void name() {
        System.out.println("我是宝马");
    }
}
/**
 * 大众汽车
 *
 * @author Lion Li
 */
public class DaZhongCar implements ICar {
    public void name() {
        System.out.println("我是大众");
    }
}
/**
 * 波音飞机
 *
 * @author Lion Li
 */
public class BoYinAircraft implements IAircraft {
    public void name() {
        System.out.println("我是波音");
    }
}
/**
 * 空客飞机
 *
 * @author Lion Li
 */
public class KongKeAircraft implements IAircraft {
    public void name() {
        System.out.println("我是空客");
    }
}
 
4.定义所有产品工厂实现
/**
 * 宝马汽车工厂
 *
 * @author Lion Li
 */
public class BaoMaCarFactory implements ICarFactory {
    public ICar create() {
        return new BaoMaCar();
    }
}
/**
 * 大众汽车工厂
 *
 * @author Lion Li
 */
public class DaZhongCarFactory implements ICarFactory {
    public ICar create() {
        return new DaZhongCar();
    }
}
/**
 * 波音飞机工厂
 *
 * @author Lion Li
 */
public class BoYinAircraftFactory implements IAircraftFactory {
    public IAircraft create() {
        return new BoYinAircraft();
    }
}
/**
 * 空客飞机工厂
 *
 * @author Lion Li
 */
public class KongKeAircraftFactory implements IAircraftFactory {
    public IAircraft create() {
        return new KongKeAircraft();
    }
}
 
5.定义交通工具抽象工厂
/**
 * 交通工具抽象工厂
 *
 * @author Lion Li
 */
public abstract class TransportationFactory {
    public void init() {
        System.out.println("初始化数据");
    }
    protected abstract ICar createCar(Class<? extends ICarFactory> clazz);
    protected abstract IAircraft createAircraft(Class<? extends IAircraftFactory> clazz);
}
 
6.定义出行方式工厂
/**
 * 出行方式工厂
 *
 * @author Lion Li
 */
public class TravelFactory extends TransportationFactory {
    @Override
    protected ICar createCar(Class<? extends ICarFactory> clazz) {
        super.init();
        if (null != clazz) {
            try {
                return clazz.newInstance().create();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
    @Override
    protected IAircraft createAircraft(Class<? extends IAircraftFactory> clazz) {
        super.init();
        if (null != clazz) {
            try {
                return clazz.newInstance().create();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
 
7.编写测试类并运行测试
/**
 * @author Lion Li
 */
public class Test {
    public static void main(String[] args){
        TransportationFactory factory = new TravelFactory();
        ICar baoma = factory.createCar(BaoMaCarFactory.class);
        baoma.name();
        ICar dazhong = factory.createCar(DaZhongCarFactory.class);
        dazhong.name();
        IAircraft boyin = factory.createAircraft(BoYinAircraftFactory.class);
        boyin.name();
        IAircraft kongke = factory.createAircraft(KongKeAircraftFactory.class);
        kongke.name();
    }
}
 

Spring 工厂 BeanFactory 解析
这里由于网上已经有很多成熟的解析文档 就不多做介绍了
推荐文章链接: Spring 的工厂模式 BeanFactory 是什么源码刨析
 









