• Java: Simple Factory(Static Factory Method Pattern)


    /*
     * 版权所有 2021 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     *简单工厂模式  simple factory
     * 历史版本: JDK 14.02
     * 2021-12-12 创建者 geovindu
     * 2021-12-15 添加 Lambda
     * 2021-12-15 修改:date
     * 接口类
     * 2021-12-15 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc Fruit.java
     *
     * */
    
    
    
    
    package com.javapatterns.simplefactory;
    
    /*
    
    * */
    public interface Fruit {
    
        /*
         *生长
         * */
        void grow();
        /*
        * 收获
        * */
        void harvest();
        /*
         *种植
         * */
        void plant();
    
    }
    

      

    /*
     * 版权所有 2021 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     *简单工厂模式  simple factory
     * 历史版本: JDK 14.02
     * 2021-12-12 创建者 geovindu
     * 2021-12-15 添加 Lambda
     * 2021-12-15 修改:date
     * 接口类
     * 2021-12-15 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc Apple.java
     *
     * */
    
    
    package com.javapatterns.simplefactory;
    
    
    /*
     *苹果
     * */
    public class Apple  implements Fruit
    {
        /*
         *生成
         * */
        public void grow()
        {
            System.out.println("Apple is growing...");
        }
        /*
         *收获
         * */
        public void harvest()
        {
            System.out.println("Apple has been harvested.");
        }
        /*
         *种植
         * */
        public void plant()
        {
            System.out.println("Apple has been planted.");
        }
        /*
        *日志
        * */
        public static void log(String msg)
        {
            System.out.println(msg);
        }
        /*
         *树龄取值方法
         * */
        public int getTreeAge(){ return treeAge; }
        /*
         *树龄的赋值方法
         * */
        public void setTreeAge(int treeAge){ this.treeAge = treeAge; }
    
        private int treeAge;
    
    }
    

      

    /*
     * 版权所有 2021 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     *简单工厂模式  simple factory
     * 历史版本: JDK 14.02
     * 2021-12-12 创建者 geovindu
     * 2021-12-15 添加 Lambda
     * 2021-12-15 修改:date
     * 接口类
     * 2021-12-15 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc Grape.java
     *
     * */
    
    
    package com.javapatterns.simplefactory;
    
    /*
    * 葡萄
    * */
    public class Grape implements Fruit{
    
    
        /*
         *生长
         * */
        public void grow()
        {
            System.out.println("Grape is growing...");
        }
        /*
        收获
        * */
        public void harvest()
        {
            System.out.println("Grape has been harvested.");
        }
        /*
        种植
         * */
        public void plant()
        {
            System.out.println("Grape has been planted.");
        }
    
        /*
         *日志
         * */
        public static void log(String msg)
        {
            System.out.println(msg);
        }
        /*
         有无籽取值方法
         * */
        public boolean getSeedless()
        {
            return seedless;
        }
        /*
          有无籽赋值方法
         * */
        public void setSeedless(boolean seedless)
        {
            this.seedless = seedless;
        }
        /*
         * */
        private boolean seedless;
    }
    

      

    /*
     * 版权所有 2021 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     *简单工厂模式  simple factory
     * 历史版本: JDK 14.02
     * 2021-12-12 创建者 geovindu
     * 2021-12-15 添加 Lambda
     * 2021-12-15 修改:date
     * 接口类
     * 2021-12-15 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc Strawberry.java
     *
     * */
    
    package com.javapatterns.simplefactory;
    /*
    * 草莓
    * */
    public class Strawberry implements Fruit{
    
    
        /*
         *生长
         * */
        public void grow()
        {
            System.out.println("Strawberry is growing...");
        }
        /*
         *   收获
        * */
        public void harvest()
        {
            System.out.println("Strawberry has been harvested.");
        }
        /*
        种植
         * */
        public void plant()
        {
            System.out.println("Strawberry has been planted.");
        }
    
        /*
         *日志
         * */
        public static void log(String msg)
        {
            System.out.println(msg);
        }
    
    
    }
    
    /*
     * 版权所有 2021 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     *简单工厂模式  simple factory
     * 历史版本: JDK 14.02
     * 2021-12-12 创建者 geovindu
     * 2021-12-15 添加 Lambda
     * 2021-12-15 修改:date
     * 接口类
     * 2021-12-15 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc FruitGardener.java
     *
     * */
    
    
    
    package com.javapatterns.simplefactory;
    
    public class FruitGardener {
    
    
        /*
        * 靜態工廠方法  塗聚文、涂聚文,Geovin Du,geovindu
        * */
        public static Fruit factory(String which) throws BadFruitException
        {
            if (which.equalsIgnoreCase("apple"))
            {
                return new Apple();
            }
            else if (which.equalsIgnoreCase("strawberry"))
            {
                return new Strawberry();
            }
            else if (which.equalsIgnoreCase("grape"))
            {
                return new Grape();
            }
            else
            {
                throw new BadFruitException("Bad fruit request");
            }
        }
    
    
    }
    

      

    /*
     * 版权所有 2021 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     *简单工厂模式  simple factory
     * 历史版本: JDK 14.02
     * 2021-12-12 创建者 geovindu
     * 2021-12-15 添加 Lambda
     * 2021-12-15 修改:date
     * 接口类
     * 2021-12-15 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc BadFruitException.java
     *
     * */
    
    
    
    package com.javapatterns.simplefactory;
    
    public class BadFruitException  extends Exception{
    
        /*
        * */
        public BadFruitException(String msg)
        {
            super(msg);
        }
    }
    

      

    调用:

            try
            {
                //靜態工廠方法
                Grape g=(Grape) FruitGardener.factory("grape");
                // System.out.println();
                g.grow();
                g.getlog("geovindu,hi.");
    
            }
            catch (BadFruitException exception)
            {
                exception.printStackTrace();
            }
            
    

      

    输出:

    Grape is growing...
    geovindu,hi.
    

      

     

  • 相关阅读:
    dom4j
    javase的一些基础(4)
    javase的一些基础(3)
    javase的一些基础(2)
    javase的一些基础(1)
    不要在乎形式
    世界观--我对这个世界的理解
    feign.FeignException: status 400 reading xxx 异常
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
    SpringBoot整合Junit测试时要注意@SpringBootTest注解需指明启动类
  • 原文地址:https://www.cnblogs.com/geovindu/p/16684871.html
Copyright © 2020-2023  润新知