抽象工厂接口,也可以改成抽象类
/** * @description: 抽象工厂 * @author: lmc * @create: 2019-03-21 21:59 **/ public interface IAbstractFruitsFactory { /** * @description: 水果生产 * @return com.lmc.gp12380.pattern.factory.fruits.IFruits * @date 2019/5/24 8:33 * @author lmc */ IFruits productFruits(); /** * @description: 果酱生产 * @return com.lmc.gp12380.pattern.factory.fruits.IDriedFruits * @date 2019/5/24 8:34 * @author lmc */ IDriedFruits productIDriedFruits(); /** * @description: 果汁生产 * @return com.lmc.gp12380.pattern.factory.fruits.IJuice * @date 2019/5/24 8:34 * @author lmc */ IJuice productJuice(); } /** * @description: 苹果工厂 * @author: lmc * @create: 2019-03-21 22:02 **/ public class AppleFactory implements IAbstractFruitsFactory { /** * @description:苹果生成 * @return com.lmc.gp12380.pattern.factory.fruits.IFruits * @date 2019/5/23 15:14 * @author lmc */ @Override public IFruits productFruits() { return new Apple(); } /** * @description: 苹果酱生产 * @return com.lmc.gp12380.pattern.factory.fruits.IDriedFruits * @date 2019/5/23 15:15 * @author lmc */ @Override public IDriedFruits productIDriedFruits() { return new DriedApple(); } /** * @description: 苹果汁生产 * @return com.lmc.gp12380.pattern.factory.fruits.IJuice * @date 2019/5/23 15:15 * @author lmc */ @Override public IJuice productJuice() { return new AppleJuice(); } } /** * @description: 香蕉工厂 * @author: lmc * @create: 2019-05-24 08:35 **/ public class BananaFactory implements IAbstractFruitsFactory { @Override public IFruits productFruits() { return new Banana(); } @Override public IDriedFruits productIDriedFruits() { return new DriedBanana(); } @Override public IJuice productJuice() { return new BananaJuice(); } } /** * @description: 橙子工厂 * @author: lmc * @create: 2019-05-24 08:27 **/ public class OrangeFactory implements IAbstractFruitsFactory { @Override public IFruits productFruits() { return new Orange(); } @Override public IDriedFruits productIDriedFruits() { return new DriedOrange(); } @Override public IJuice productJuice() { return new OrangeJuice(); } }抽象工厂其实是一个产品族,具体的实现抽象工厂类生产的产品实例所产生同一产品类型,比如所有的水果是一个产品等级结构,所有的果酱也是一个产品等级结构,所有的果汁是一个产品等级结构。
/** * @description: 抽象工厂方法测试 * @author: lmc * @create: 2019-03-20 20:39 **/ public class AbstractFactoryTest { public static void main(String[] args) { AppleFactory appleFactory=new AppleFactory(); IFruits apple= appleFactory.productFruits(); apple.getName(); apple.getColor(); apple.getTaste(); IDriedFruits appleDriedFruits=appleFactory.productIDriedFruits(); appleDriedFruits.getColor(); appleDriedFruits.getName(); appleDriedFruits.getTaste(); IJuice appleJuice=appleFactory.productJuice(); appleJuice.getName(); appleJuice.getColor(); appleJuice.getTaste(); System.out.println("_________________________________________"); OrangeFactory orangeFactory=new OrangeFactory(); IFruits orange= orangeFactory.productFruits(); orange.getName(); orange.getColor(); orange.getTaste(); IDriedFruits orangeDriedFruits=orangeFactory.productIDriedFruits(); orangeDriedFruits.getColor(); orangeDriedFruits.getName(); orangeDriedFruits.getTaste(); IJuice orangeJuice=orangeFactory.productJuice(); orangeJuice.getName(); orangeJuice.getColor(); orangeJuice.getTaste(); System.out.println("_________________________________________"); BananaFactory bananaFactory=new BananaFactory(); IFruits banana= bananaFactory.productFruits(); banana.getName(); banana.getColor(); banana.getTaste(); IDriedFruits bananaDriedFruits=bananaFactory.productIDriedFruits(); bananaDriedFruits.getColor(); bananaDriedFruits.getName(); bananaDriedFruits.getTaste(); IJuice bananaJuice=appleFactory.productJuice(); bananaJuice.getName(); bananaJuice.getColor(); bananaJuice.getTaste(); } }