/*
* 版权所有 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();
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/288997.html