Java的工厂模式(Factory)
来源:互联网 发布:linux vi替换文件内容 编辑:程序博客网 时间:2024/05/01 01:46
1 实例:
package com.javapatterns.factorymethod.farm;
public interface Fruit
...{
void grow();
void harvest();
void plant();
}
public interface Fruit
...{
void grow();
void harvest();
void plant();
}
package com.javapatterns.factorymethod.farm;
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;
}
}
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;
}
}
package com.javapatterns.factorymethod.farm;
public class StrawberryGardener implements FruitGardener
...{
public Fruit factory()
...{
return new Apple();
}
}
public class StrawberryGardener implements FruitGardener
...{
public Fruit factory()
...{
return new Apple();
}
}
package com.javapatterns.factorymethod.farm;
public class AppleGardener implements FruitGardener
...{
public Fruit factory()
...{
return new Apple();
}
}
public class AppleGardener implements FruitGardener
...{
public Fruit factory()
...{
return new Apple();
}
}
package com.javapatterns.factorymethod.farm;
public class GrapeGardener implements FruitGardener
...{
public Fruit factory()
...{
return new Apple();
}
}
public class GrapeGardener implements FruitGardener
...{
public Fruit factory()
...{
return new Apple();
}
}
package com.javapatterns.factorymethod.farm;
public interface FruitGardener
...{
public Fruit factory();
/** *//** @link dependency
* @label Creates*/
/**//*# Fruit lnkFruit; */
}
public interface FruitGardener
...{
public Fruit factory();
/** *//** @link dependency
* @label Creates*/
/**//*# Fruit lnkFruit; */
}
package com.javapatterns.factorymethod.farm;
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;
}
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;
}
package com.javapatterns.factorymethod.farm;
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 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.");
}
}
/**//*
* FruitFactory.java
*
* Created on 2008年3月1日, 上午11:33
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.javapatterns.factorymethod.farm;
/** *//**
*
* @author rulinma
*/
public class FruitFactory ...{
/** *//** Creates a new instance of FruitFactory */
public FruitFactory() ...{
}
public static Fruit factory(String type)
...{
if(type.equals("apple"))
...{
return new Apple();
}
else if (type.equals("grape"))
...{
return new Grape();
}
else
...{
return new Strawberry();
}
}
public static void main(String[] args)
...{
Fruit f = factory("apple");
f.plant();
f.grow();
f.harvest();
}
}
* FruitFactory.java
*
* Created on 2008年3月1日, 上午11:33
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.javapatterns.factorymethod.farm;
/** *//**
*
* @author rulinma
*/
public class FruitFactory ...{
/** *//** Creates a new instance of FruitFactory */
public FruitFactory() ...{
}
public static Fruit factory(String type)
...{
if(type.equals("apple"))
...{
return new Apple();
}
else if (type.equals("grape"))
...{
return new Grape();
}
else
...{
return new Strawberry();
}
}
public static void main(String[] args)
...{
Fruit f = factory("apple");
f.plant();
f.grow();
f.harvest();
}
}
init:
deps-jar:
compile-single:
run-single:
Apple has been planted.
Apple is growing...
Apple has been harvested.
生成成功(总时间:1 秒)
- Java的工厂模式(Factory)
- Java工厂模式 factory
- Java工厂模式 factory
- Java的工厂模式(Factory)之二
- Java工厂模式(Factory模式)
- Java Factory Pattern(工厂模式)
- 简单工厂(Simple Factory)模式的Java实现[00原创]
- 工厂方法(Factory Method)模式的Java实现[00原创]
- 抽象工厂(Abstract Factory)模式的Java实现[00原创]
- (Factory Method)工厂方法模式的Java实现
- (Factory Method)工厂方法模式的Java实现
- (Factory Method)工厂方法模式的Java实现
- Java设计模式:Factory(工厂模式)
- java设计模式之工厂模式(Factory)
- Java设计模式 - Factory(工厂模式)
- java设计模式之工厂模式(Factory)
- java设计模式之工厂模式Factory
- Java工厂模式(Factory模式)
- [转]均值聚类算法
- Hibernate中lazy的设置
- Hibernate中load和get的区别
- 整合多个配置文件
- 豆腐的几种家常菜
- Java的工厂模式(Factory)
- Hibernate+EhCache初级应用
- 有感而发
- HTML中A标签中的#号问题
- MySQL优化的8条经验
- JAVA中WebService的开发要点
- JAVA中使用Log4j日志工具
- Struts重复提交
- 如何记录自己各种账户,用户和密码?