java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java工厂模式

Java设计模式之工厂模式案例详解

作者:温故知新之java

工厂模式(Factory Pattern)是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());
    }
}

查看结果

对比分析

总结

通过以上三个案例,对比分析我们明确了三种工厂模式实现的方式,也对比分析了各个工厂模式的优点。但是切记,实际开发过程中,不要盲目的使用,一定要结合具体的业务,实际分析后才去使用。

到此这篇关于Java设计模式之工厂模式案例详解的文章就介绍到这了,更多相关Java工厂模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文