ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java:Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)

2022-09-12 12:30:08  阅读:234  来源: 互联网

标签:12 java Pattern void 09 2022 Polymorphic public


 

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Fruit.java
 *
 * */



package com.javapatterns.factorymethod;

/*
* 水果
* 抽象产品接口
* */

public interface Fruit {
    /*
     *生长
     * */
    void grow();
    /*
     * 收获
     * */
    void harvest();
    /*
     *种植
     * */
    void plant();

}

  

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc FruitGardener.java
 *
 * */



package com.javapatterns.factorymethod;


/*
 *水果园丁
 * */
public interface  FruitGardener {

    /*
     * 工厂方法
     * */
    public Fruit factory();
}

  

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Apple.java
 *
 * */


package com.javapatterns.factorymethod;
/*
 * 苹果
 * */
public class Apple  implements Fruit{

    private int treeAge;

    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 int getTreeAge()
    {
        return treeAge;
    }

    public void setTreeAge(int treeAge)
    {
        this.treeAge = treeAge;
    }


}

  

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc AppleGardener.java
 *
 * */


package com.javapatterns.factorymethod;

/*
 * 苹果园丁
 * */
public class AppleGardener  implements FruitGardener{


    /*
    * 工厂方法
    * */
    public Fruit factory()
    {
        return new Apple();
    }

}

  

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Grape.java
 *
 * */


package com.javapatterns.factorymethod;

/*
 * 葡萄
 * */
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 boolean getSeedless()
    {
        return seedless;
    }

    public void setSeedless(boolean seedless)
    {
        this.seedless = seedless;
    }

    private boolean seedless;

}

  

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc GrapeGardener.java
 *
 * */


package com.javapatterns.factorymethod;

/*
* 葡萄园丁
* */
public class GrapeGardener  implements FruitGardener{
    /*
     * 工厂方法
     * */
    public Fruit factory()
    {
        return new Grape();
    }
}

  

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Strawberry.java
 *
 * */


package com.javapatterns.factorymethod;


/*
 * 草莓
 * */
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.");
    }

}

  

 

 

/*
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 工厂方法模式  Method Factory(Polymorphic Facotry Pattern/Virtual Constructor Pattern)
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc StrawberryGardener.java
 *
 * */

package com.javapatterns.factorymethod;

/*
 * 草莓园丁
 * */
public class StrawberryGardener implements FruitGardener {

    public Fruit factory()
    {
        return new Strawberry();
    }
}

  

 调用:

            //工廠方法
            com.javapatterns.factorymethod.AppleGardener f=new AppleGardener();
            f.factory().grow();

  

标签:12,java,Pattern,void,09,2022,Polymorphic,public
来源: https://www.cnblogs.com/geovindu/p/16685904.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有