时间:2022-09-23 10:10:16 | 栏目:JAVA代码 | 点击:次
1.简单工厂模式
2.工厂方法模式
3.抽象工厂模式
根据蛋糕的不同口味,分别创建苹果味和香蕉味的蛋糕实例
定义蛋糕父类
/** * 蛋糕 * @author:liyajie * @createTime:2022/2/14 10:17 * @version:1.0 */ public class Cake { }
定义苹果味的蛋糕
/** * 苹果蛋糕 * @author:liyajie * @createTime:2022/2/14 10:17 * @version:1.0 */ public class AppleCake extends Cake{ public AppleCake(){ System.out.println("创建了苹果蛋糕实例"); } }
定义香蕉味的蛋糕
/** * 香蕉蛋糕 * @author:liyajie * @createTime:2022/2/14 10:17 * @version:1.0 */ public class BananaCake extends Cake{ public BananaCake(){ System.out.println("创建了香蕉蛋糕实例"); } }
定义蛋糕工厂类
/** * 蛋糕工厂 * @author:liyajie * @createTime:2022/2/14 10:16 * @version:1.0 */ public class CakeFactory { public Cake createCake(String type){ if("apple".equals(type)){ return new AppleCake(); }else if("banana".equals(type)){ return new BananaCake(); }else { return null; } } }
定义测试类
/** * 测试类 * @author:liyajie * @createTime:2022/2/14 10:25 * @version:1.0 */ public class Test { public static void main(String[] args) { CakeFactory cakeFactory = new CakeFactory(); cakeFactory.createCake("apple"); cakeFactory.createCake("banana"); } }
查看结果:
定义蛋糕父类
/** * 蛋糕父类 * @author:liyajie * @createTime:2022/2/14 10:44 * @version:1.0 */ public abstract class Cake { }
定义苹果蛋糕
/** * 苹果蛋糕 * @author:liyajie * @createTime:2022/2/14 10:17 * @version:1.0 */ public class AppleCake extends Cake { public AppleCake(){ System.out.println("创建了苹果蛋糕实例"); } }
定义香蕉蛋糕
/** * 香蕉蛋糕 * @author:liyajie * @createTime:2022/2/14 10:17 * @version:1.0 */ public class BananaCake extends Cake { public BananaCake(){ System.out.println("创建了香蕉蛋糕实例"); } }
定义蛋糕工厂类 该类主要是定义了createCake抽象方法,并且在无参构造函数中进行调用,具体的实现是在子类里进行实例化的
/** * 蛋糕工厂类 * @author:liyajie * @createTime:2022/2/14 11:55 * @version:1.0 */ public abstract class CakeFactory { abstract Cake createCake(); public CakeFactory() { createCake(); } }
定义苹果蛋糕工具类
/** * @desc: * @author:liyajie * @createTime:2022/2/14 11:58 * @version:1.0 */ public class AppleCakeFactory extends CakeFactory{ @Override public Cake createCake() { return new AppleCake(); } }
定义香蕉蛋糕工具类
/** * @desc: * @author:liyajie * @createTime:2022/2/14 11:58 * @version:1.0 */ public class BananaCakeFactory extends CakeFactory{ @Override public Cake createCake() { return new BananaCake(); } }
定义测试类
/** * 测试类 * @author:liyajie * @createTime:2022/2/14 12:02 * @version:1.0 */ public class Test { public static void main(String[] args) { AppleCakeFactory appleCakeFactory = new AppleCakeFactory(); BananaCakeFactory bananaCakeFactory = new BananaCakeFactory(); } }
查看结果
定义蛋糕父类
/** * @desc: * @author:liyajie * @createTime:2022/2/14 10:44 * @version:1.0 */ public abstract class Cake { }
定义苹果蛋糕
/** * 苹果蛋糕 * @author:liyajie * @createTime:2022/2/14 10:17 * @version:1.0 */ public class AppleCake extends Cake { public AppleCake(){ System.out.println("创建了苹果蛋糕实例"); } }
定义香蕉蛋糕
/** * 香蕉蛋糕 * @author:liyajie * @createTime:2022/2/14 10:17 * @version:1.0 */ public class BananaCake extends Cake { public BananaCake(){ System.out.println("创建了香蕉蛋糕实例"); } }
定义蛋糕工厂类
/** * @desc: * @author:liyajie * @createTime:2022/2/14 11:55 * @version:1.0 */ public interface CakeFactory { Cake createCake(); }
定义苹果蛋糕工厂类
/** * @desc: * @author:liyajie * @createTime:2022/2/14 11:58 * @version:1.0 */ public class AppleCakeFactory implements CakeFactory { @Override public Cake createCake() { return new AppleCake(); } }
定义香蕉蛋糕工厂类
/** * @desc: * @author:liyajie * @createTime:2022/2/14 11:58 * @version:1.0 */ public class BananaCakeFactory implements CakeFactory { @Override public Cake createCake() { return new BananaCake(); } }
定义蛋糕工厂生成器
/** * @desc: * @author:liyajie * @createTime:2022/2/14 17:46 * @version:1.0 */ public class FactoryProducter { CakeFactory cakeFactory; public FactoryProducter(CakeFactory cakeFactory){ setCakeFactory(cakeFactory); } public void setCakeFactory(CakeFactory cakeFactory){ this.cakeFactory = cakeFactory; cakeFactory.createCake(); } }
定义测试类
/** * 测试类 * @author:liyajie * @createTime:2022/2/14 12:02 * @version:1.0 */ public class Test { public static void main(String[] args) { new FactoryProducter(new AppleCakeFactory()); new FactoryProducter(new BananaCakeFactory()); } }
查看结果
通过以上三个案例,对比分析我们明确了三种工厂模式实现的方式,也对比分析了各个工厂模式的优点。但是切记,实际开发过程中,不要盲目的使用,一定要结合具体的业务,实际分析后才去使用。